@@ -77,12 +77,115 @@ please see:
77
77
and
78
78
[ front-end-with-tape.md] ( https://github.com/dwyl/learn-tape/blob/master/front-end-with-tape.md )
79
79
80
+ It's "OK" to ask: "_ Where do I ** start** (my ** TDD** quest)?_ " <br />
81
+ The answer is: create ** two** new files:
82
+ ` examples/todo-list/elmish.js ` and ` test/elmish.test.js `
80
83
84
+ We will create a couple of tests and their corresponding functions _ next_ !
81
85
86
+ ## Elm(_ ish_ )
87
+
88
+ Our ** first step** is to _ abstract_ and _ generalise_
89
+ the Elm Architecture (` mount ` ) and HTML ("DOM") functions
90
+ we used in the "counter" example.
91
+
92
+ Recall that there are ** 3 parts** to "TEA": ` model ` , ` update ` and ` view ` . <br />
93
+ These correspond to the ` M ` odel, ` C ` ontroller and ` V ` iew of "** MVC** ".
94
+ The _ reason_ Elm refers to the "Controller" as "Update" is because
95
+ this name _ more accurately_ reflects what the function _ does_ :
96
+ it updates the _ state_ of the application.
82
97
83
- ### _ Analyse_
98
+ Our ` update ` and ` view ` functions form
99
+ the "business logic" of our Todo List App,
100
+ so we cannot abstract these. <br />
101
+ The ` update ` function is a simple ` switch ` statement
102
+ that "decides" how to to _ update_ the app's ` model `
103
+ each ` case ` is functionality that is _ specific_ to the Todo List App. <br />
104
+ The ` view ` function _ invokes_ several "helper" functions
105
+ which create HTML elements e.g: ` div ` & ` <button> ` ;
106
+ these _ can_ (_ will_ ) be generalised (_ below_ ).
107
+
108
+ Let's kick-off with a couple of "_ familiar_ " _ generic_ functions:
109
+ ` empty ` and ` mount ` .
110
+
111
+
112
+ #### ` empty ` the DOM
113
+
114
+ Given that we _ know_ we are going to use the ` empty `
115
+ function we used previously in our ` counter ` ,
116
+ ` counter-reset ` and ` multiple-counters ` examples (_ in the "basic" TEA tutorial_ )
117
+ we can write a _ test_ for the ` empty ` function quite easily.
118
+
119
+ In the ` test/elmish.test.js ` file, type the following code:
120
+ ``` js
121
+ const test = require (' tape' ); // https://github.com/dwyl/learn-tape
122
+ const fs = require (' fs' ); // read html files (see below)
123
+ const path = require (' path' ); // so we can open files cross-platform
124
+ const elmish = require (' ../examples/todo-list/elmish.js' );
125
+ const html = fs .readFileSync (path .resolve (__dirname ,
126
+ ' ../examples/todo-list/index.html' ));
127
+ require (' jsdom-global' )(html); // https://github.com/rstacruz/jsdom-global
128
+ elmish .init (document ); // pass JSDOM into elmish for DOM functions
129
+ const id = ' test-app' ; // all tests use separate root element
130
+
131
+ test (' empty("root") removes DOM elements from container' , function (t ) {
132
+ // setup the test div:
133
+ const text = ' Hello World!'
134
+ const root = document .getElementById (id);
135
+ const div = document .createElement (' div' );
136
+ div .id = ' mydiv' ;
137
+ const txt = document .createTextNode (text);
138
+ div .appendChild (txt);
139
+ root .appendChild (div);
140
+ // check text of the div:
141
+ const actual = document .getElementById (' mydiv' ).textContent ;
142
+ t .equal (actual, text, " Contents of mydiv is: " + actual + ' == ' + text);
143
+ t .equal (root .childElementCount , 1 , " Root element " + id + " has 1 child el" );
144
+ // empty the root DOM node:
145
+ elmish .empty (root); // exercise the `empty` function!
146
+ t .equal (root .childElementCount , 0 , " After empty(root) has 0 child elements!" )
147
+ t .end ();
148
+ });
149
+ ```
150
+
151
+ > _ ** Note** : if any line in this file is ** unfamiliar** to you,
152
+ please ** first** go back over the previous example(s),
153
+ ** then** do bit of "googling" for any words or functions you don't recognise
154
+ e.g: ` childElementCount ` ,
155
+ and if you are ** still** "** stuck** "_ ,
156
+ [ *** please open an
157
+ issue*** !] ( https://github.com/dwyl/learn-elm-architecture-in-javascript/issues )
158
+ _ It's ** essential** that you ** understand** each ** character**
159
+ in the code ** before** continuing to ** avoid** "** confusion** " later._
160
+
161
+ Now that we have the ** test** for our ` empty ` function written,
162
+ we can add the ` empty ` function to ` examples/todo-list/elmish.js ` :
163
+ ``` js
164
+ /**
165
+ * `empty` the contents of a given DOM element "node" (before re-rendering).
166
+ * This is the *fastest* way according to: stackoverflow.com/a/3955238/1148249
167
+ * @param {Object} node the exact DOM node you want to empty
168
+ * @example
169
+ * // returns true (once the 'app' node is emptied)
170
+ * const node = document .getElementById (' app' );
171
+ * empty (node);
172
+ */
173
+ function empty (node ) {
174
+ while (node .lastChild ) {
175
+ node .removeChild (node .lastChild );
176
+ }
177
+ }
178
+ ```
179
+
180
+ If the comment syntax above the function definition is _ unfamiliar_ ,
181
+ please see:
182
+ [ https://github.com/dwyl/**learn-jsdoc ** ] ( https://github.com/dwyl/learn-jsdoc )
84
183
85
- Our _ first_ step is to _ analyse_ the required functionality of a Todo List.
184
+
185
+ ### _ Analyse_ the TodoMVC App to "Gather Requirements"
186
+
187
+ Once we have a
188
+ Our _ next_ step is to _ analyse_ the required functionality of a Todo List.
86
189
87
190
### Todo List _ Basic_ Functionality
88
191
@@ -156,27 +259,6 @@ _so don't expect to **understand** it all the first time without study._
156
259
_ Don't worry, we will walk through building each feature in detail._
157
260
158
261
159
- ## Elm(_ ish_ )
160
-
161
- Our ** first step** is to _ abstract_ and _ generalise_
162
- the Elm Architecture (` mount ` ) and HTML ("DOM") functions
163
- we used in the "counter" example.
164
-
165
- Recall that there are ** 3 parts** to "TEA": ` model ` , ` update ` and ` view ` . <br />
166
- These correspond to the ` M ` odel, ` C ` ontroller and ` V ` iew of "** MVC** ".
167
- The _ reason_ Elm refers to the "Controller" as "Update" is because
168
- this name _ more accurately_ reflects what the function _ does_ :
169
- it updates the _ state_ of the application.
170
-
171
- Our ` update ` and ` view ` functions form
172
- the "business logic" of our Todo List App,
173
- so we cannot abstract these. <br />
174
- The ` update ` function is a simple ` switch ` statement
175
- that "decides" how to to _ update_ the app's ` model `
176
- each ` case ` is functionality that is _ specific_ to the Todo List App. <br />
177
- The ` view ` function _ invokes_ several "helper" functions
178
- which create HTML elements e.g: ` div ` & ` <button> ` ; these _ can_ be generalised.
179
-
180
262
181
263
### HTML Elements (Functions)
182
264
0 commit comments