jeudi 7 juillet 2016

Writing unit test for tiny javascript app

So, usually one should first learn the basic of unit test. I have read a few times about unit test. But Im probably lacking of good knowledges. The usual way would be to first learn the basics and then apply to your app. Unfortunately I dont have so much time. This is of course not your problem and there is the risk for downvote here, but I take the chance.

This is about a very tiny javascript app. What Im looking for is to relate the theory of unit testing with this tiny project and understand how to structure the testing, basicallly what to test.

One javascript-file (app.js) calls a function in another file (funcs.js) to get results from a resource, in json format. app.js will then verify if the decoded json as length > 0. In that case it iterates among the posts in this json structure. And for each iteration app.js will call a function in funcs.js to extract data from the json for a post and at the same time build html-structure, which is returned to app.js.

At the end, app.js will fill an html element (in app.html) with the html-structure that funcs.js delivered.

So I need now to create a few unit tests. I guess the best part to test is func.js? That file contains a few functions. For example the function for opening, parsing and returning the json resuorce. Or the function for getting the fields (during iteration) for the json structure and build an html element. And then returning it.

Am I right on that? And how am I supposed to test? Can this be a good strategy:

I create a test.js file. I pick up a few functions in funcs.js (the relevant ones). I tried to call them a few times, passing both good and bad arguments (for example first an existing resource, then an empty argument resource for the function that requests json resource).

Im still a little bit unsure about how to test the parts that will return html-elements. If you could give some advice it would be great.

Aucun commentaire:

Enregistrer un commentaire