@@ -829,6 +829,12 @@ you will see something like this:
829
829
Given the test above, I added the following code to my ` todo-app.js ` file:
830
830
831
831
``` js
832
+ /* if require is available, it means we are in Node.js Land i.e. testing! */
833
+ /* istanbul ignore next */
834
+ const { a , button , div , empty , footer , input , h1 , header , label , li , mount ,
835
+ route , section , span , strong , text , ul } =
836
+ (typeof require !== ' undefined' ) ? require (' ./elmish.js' ) : {};
837
+
832
838
/**
833
839
* `render_item` creates an DOM "tree" with a single Todo List Item
834
840
* using the "elmish" DOM functions (`li`, `div`, `input`, `label` and `button`)
@@ -880,6 +886,8 @@ that renders a _single_ `<li>` (_todo list item_),
880
886
we can create another function which _ uses_ the ` render_item ` in a "loop",
881
887
to create _ several_ ` <li> ` nested in a ` <ul> ` .
882
888
889
+ #### ` render_main ` Test
890
+
883
891
Append following test code to your ` test/todo-app.test.js ` file:
884
892
885
893
``` js
@@ -894,14 +902,16 @@ test('render "main" view using (elmish) HTML DOM functions', function (t) {
894
902
};
895
903
// render the "main" view and append it to the DOM inside the `test-app` node:
896
904
document .getElementById (id).appendChild (app .render_main (model));
905
+ // test that the title text in the model.todos was rendered to <label> nodes:
906
+ document .querySelectorAll (' .view' ).forEach (function (item , index ) {
907
+ t .equal (item .textContent , model .todos [index].title ,
908
+ " index #" + index + " <label> text: " + item .textContent )
909
+ })
897
910
898
- const done = document .querySelectorAll (' .completed' )[0 ].textContent ;
899
- t .equal (done, ' Learn Elm Architecture' , ' Done: Learn "TEA"' );
900
- const todo = document .querySelectorAll (' .view' )[1 ].textContent ;
901
- t .equal (todo, ' Build Todo List App' , ' Todo: Build Todo List App' );
902
- const todos = document .querySelectorAll (' .toggle' );
911
+ const inputs = document .querySelectorAll (' input' ); // todo items are 1,2,3
903
912
[true , false , false ].forEach (function (state , index ){
904
- t .equal (todos .checked , state, " Todo #" + index + " is done=" + state)
913
+ t .equal (inputs[index + 1 ].checked , state,
914
+ " Todo #" + index + " is done=" + state)
905
915
})
906
916
elmish .empty (document .getElementById (id)); // clear DOM ready for next test
907
917
t .end ();
@@ -917,6 +927,21 @@ you will see something like this:
917
927
![ main-test-failing] ( https://user-images.githubusercontent.com/194400/43741630-f03f1fe8-99c6-11e8-8b7b-e44ee397b38e.png )
918
928
919
929
930
+ Given your knowledge of implementing the ` render_item ` function above,
931
+ and your skills with JavaScript loops, create your ` render_main ` function,
932
+ to make the tests pass.
933
+
934
+ > If you get "stuck" there is a _ reference_ implementation in:
935
+ [ ** ` todo-app.js ` ** ] ( https://github.com/dwyl/learn-elm-architecture-in-javascript/pull/45/commits/b6607478c3dbed048781932261af2981f4c6c405#diff-6be3e16fe7cfb4c00788d4d587374afdR76 )
936
+
937
+ All our tests pass _ and_ we have ** 100% test coverage** :
938
+
939
+ ![ render_main-tests-pass-100-coverage] ( https://user-images.githubusercontent.com/194400/43766409-4189ce4e-9a2a-11e8-8d73-3ea636b22928.png )
940
+
941
+ This means we are writing the "_ bare minimum_ " code necessary
942
+ to meet all acceptance criteria (requirements),
943
+ which is both faster and more maintainable! <br />
944
+ Onwards!
920
945
921
946
922
947
0 commit comments