1
1
# ` Elm ` (_ ish_ )
2
2
3
- ### (How to Build a Front-end Micro-Framework _ From Scratch_ )
3
+ ## (How to Build a Front-end Micro-Framework _ From Scratch_ )
4
4
5
5
![ elmlogo-ish] ( https://user-images.githubusercontent.com/194400/43213139-b70a4c68-902d-11e8-8162-3c7cb56b6360.png )
6
6
<!-- the colors are deliberately "a bit off" to emphasize that
@@ -45,42 +45,44 @@ By the end of this exercise you will _understand_
45
45
The Elm Architecture (TEA) _ much better_
46
46
because we will be analysing, documenting, testing
47
47
and writing each function required
48
- to architect and render the our Todo List (TodoMVC) App.
48
+ to architect and render our Todo List (TodoMVC) App.
49
49
50
50
<br /><br />
51
51
52
52
## _ Who?_
53
53
54
- People who wants to gain an _ in-depth_ understanding
54
+ People who want to gain an _ in-depth_ understanding
55
55
of The Elm Architecture ("TEA")
56
- and thus _ intrinsically_ grok Redux/React JavaScript apps.
56
+ and thus _ intrinsically_
57
+ [ grok] ( https://en.wikipedia.org/wiki/Grok ) Redux/React JavaScript apps.
57
58
58
- This tutorial is intended for _ beginners_ who have _ modest_
59
- JavaScript knowledge (_ variables, functions, DOM methods and TDD_ ). <br />
59
+ This tutorial is intended for _ beginners_ with _ modest_
60
+ JavaScript knowledge (_ variables, functions, DOM methods & TDD_ ). <br />
60
61
If you have any questions or get "stuck",
61
62
please open an issue:
62
63
https://github.com/dwyl/learn-elm-architecture-in-javascript/issues <br />
63
- @dwyl is a "safe space" and we are all here to help don't be shy/afraid.
64
+ @dwyl is a "safe space" and we are all here to help don't be shy/afraid; <br />
65
+ the _ more_ questions you ask, the more you are helping yourself and _ others_ !
64
66
65
67
<br /><br />
66
68
67
69
## _ How_ ?
68
70
69
71
_ Before_ diving into _ writing functions_ for ` Elm ` (_ ish_ ),
70
- we need to consider how we are going to _ test_ it.
71
- By ensuring that we follow ** TDD** from the _ start_ of an App ,
72
+ we need to consider how we are going to _ test_ it. < br />
73
+ By ensuring that we follow ** TDD** from the _ start_ of an project ,
72
74
we _ avoid_ having to "correct" any "** bad habits** " later.
73
75
74
- We will be using ** Tape** and ** ` JSDOM ` ** for testing the functions.
75
- Tape is a _ minimalist_ testing framework
76
- that is fast and has everything we need .
76
+ We will be using ** Tape** & ** ` JSDOM ` ** for testing the functions.
77
+ Tape is a _ minimalist_ testing library
78
+ that is _ fast _ and has _ everything we need _ .
77
79
** ` JSDOM ` ** is a JavaScript implementation of the
78
- WHATWG DOM and HTML standards, for use with node.js. <br />
80
+ WHATWG DOM & HTML standards, for use with node.js. <br />
79
81
If _ either_ of these tools is _ unfamiliar_ to you,
80
82
please see:
81
83
[ https://github.com/dwyl/**learn-tape ** ] ( https://github.com/dwyl/learn-tape )
82
84
and
83
- [ front-end-with-tape.md] ( https://github.com/dwyl/learn-tape/blob/master/front-end-with-tape.md )
85
+ [ ** front-end** -with-tape.md] ( https://github.com/dwyl/learn-tape/blob/master/front-end-with-tape.md )
84
86
85
87
86
88
### What _ Can_ We _ Generalise_ ?
@@ -107,20 +109,21 @@ which is the most _widely used_ "software architecture pattern".
107
109
"how code is ** organised** " and/or how "data ** flows** " through a system.
108
110
Whenever you see the word "** pattern** " it just means
109
111
"a bunch of experienced people have concluded that this works well,
110
- so as beginners, we don't have to think too hard."
112
+ so as beginners, we don't have to think too hard (up-front) ."
111
113
112
114
The _ reason_ Elm refers to the "** Controller** " as "*** Update*** " is because
113
115
this name _ more accurately_ reflects what the function _ does_ :
114
116
it _ updates_ the _ state_ (Model) of the application.
115
117
116
118
Our ` update ` and ` view ` functions will form
117
- the "** domain logic** " of our Todo List App,
119
+ the "** domain logic** " of our Todo List App, < br />
118
120
(_ i.e. they are "** specific** " to the Todo List_ )
119
121
so we cannot abstract them. <br />
120
- The ` model ` will be a JavaScript ` Object ` where the App's data will be stored.
122
+ The ` model ` will be a JavaScript ` Object ` where the App's
123
+ data (todo list items) will be stored.
121
124
122
125
The ` update ` function is a simple ` switch ` statement
123
- that "decides" how to to _ update _ the app's ` model `
126
+ that "decides" how to to _ ` update ` _ the app's ` model `
124
127
each ` case ` will call a function
125
128
that _ belongs_ to the Todo List App. <br />
126
129
@@ -145,7 +148,7 @@ The answer is: create **two** new files:
145
148
### Test Setup
146
149
147
150
In order to run our test, we need some "setup" code
148
- that "requires" the libraries so we can _ execute_ the functions.
151
+ that "requires" the libraries/files so we can _ execute_ the functions.
149
152
150
153
In the ` test/elmish.test.js ` file, type the following code:
151
154
``` js
@@ -156,7 +159,6 @@ const html = fs.readFileSync(path.resolve(__dirname,
156
159
' ../examples/todo-list/index.html' )); // sample HTML file to initialise JSDOM.
157
160
require (' jsdom-global' )(html); // https://github.com/rstacruz/jsdom-global
158
161
const elmish = require (' ../examples/todo-list/elmish.js' ); // functions to test
159
- elmish .init (document ); // pass JSDOM into elmish for DOM functions
160
162
const id = ' test-app' ; // all tests use 'test-app' as root element
161
163
```
162
164
@@ -166,61 +168,7 @@ if you have followed previous tutorials.
166
168
https://github.com/dwyl/learn-tape
167
169
and
168
170
169
- If you attempt to run this code using the command:
170
- ``` sh
171
- node test/elmish.test.js
172
- ```
173
-
174
- you will see something like the following:
175
- ![ no-init-function] ( https://user-images.githubusercontent.com/194400/43359605-b8cc9418-929c-11e8-92d6-97feb8c67596.png )
176
-
177
- This is because we do not have anything in the ` elmish.js ` , yet.
178
- Let's address that now!
179
-
180
-
181
- ### Add ` init ` function
182
-
183
- Open the ` examples/todo-list/elmish.js ` file and add the following code:
184
-
185
- ``` js
186
-
187
-
188
- /**
189
- * `init` initialises the document (Global) variable for DOM operations.
190
- * @param {Object} doc window.document in browser and JSDOM.document in tests.
191
- * @return {Object} document returns whatever is passed in.
192
- */
193
- function init (doc ){
194
- document = doc; // this is used for instantiating JSDOM for testing.
195
- return document ;
196
- }
197
- ```
198
-
199
- > If the ** comment syntax** above the function definition is _ unfamiliar_ ,
200
- please see:
201
- [ https://github.com/dwyl/**learn-jsdoc ** ] ( https://github.com/dwyl/learn-jsdoc )
202
-
203
- This code is simply to allow us to "initialise" ` Elm ` (_ ish_ )
204
- with a "simulated" browser ` document ` (` JSDOM ` ) so that we can _ test_ the code.
205
-
206
-
207
- ### Add ` module.exports ` to "export" the ` init ` function
208
-
209
- Adding the function to the ` elmish.js ` file is a good _ start_ ,
210
- but we need to *** ` export ` *** it to be able to _ invoke_ it in our test. <br />
211
- Add the following code at the end of ` examples/todo-list/elmish.js ` :
212
-
213
- ``` js
214
- /* module.exports is needed to run the functions using Node.js for testing! */
215
- /* istanbul ignore next */
216
- if (typeof module !== ' undefined' && module .exports ) {
217
- module .exports = {
218
- init: init
219
- }
220
- } else { init (document );
221
- ` ` `
222
-
223
- Now if you attempt to run the test file: ` node test/ elmish .test .js `
171
+ If you attempt to run the test file: ` node test/elmish.test.js `
224
172
you should see no output.
225
173
(_ this is expected as we haven't written any tests yet!_ )
226
174
@@ -332,12 +280,21 @@ function empty(node) {
332
280
}
333
281
}
334
282
```
335
- remember to add a line in the ` module .exports ` Object at the end of the file:
283
+
284
+ #### Add ` module.exports ` statement to "export" the ` empty ` function
285
+
286
+ Remember to add a line in the ` module.exports ` Object at the end of the file:
287
+
288
+ Adding the function to the ` elmish.js ` file is a good _ start_ ,
289
+ but we need to *** ` export ` *** it to be able to _ invoke_ it in our test. <br />
290
+ Add the following code at the end of ` examples/todo-list/elmish.js ` :
291
+
336
292
``` js
293
+ /* module.exports is needed to run the functions using Node.js for testing! */
294
+ /* istanbul ignore next */
337
295
if (typeof module !== ' undefined' && module .exports ) {
338
296
module .exports = {
339
- empty: empty, // add this line to export the `empty` function
340
- init: init,
297
+ empty: empty // export the `empty` function so we can test it.
341
298
}
342
299
} else { init (document ); }
343
300
```
0 commit comments