Skip to content

Commit 6ec9868

Browse files
committed
remove init function from elmish.js & elmish.md see #49 https://youtu.be/aJOTlE1K90k
1 parent f9ba7aa commit 6ec9868

File tree

3 files changed

+36
-91
lines changed

3 files changed

+36
-91
lines changed

elmish.md

+35-78
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `Elm`(_ish_)
22

3-
### (How to Build a Front-end Micro-Framework _From Scratch_)
3+
## (How to Build a Front-end Micro-Framework _From Scratch_)
44

55
![elmlogo-ish](https://user-images.githubusercontent.com/194400/43213139-b70a4c68-902d-11e8-8162-3c7cb56b6360.png)
66
<!-- the colors are deliberately "a bit off" to emphasize that
@@ -45,42 +45,44 @@ By the end of this exercise you will _understand_
4545
The Elm Architecture (TEA) _much better_
4646
because we will be analysing, documenting, testing
4747
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.
4949

5050
<br /><br />
5151

5252
## _Who?_
5353

54-
People who wants to gain an _in-depth_ understanding
54+
People who want to gain an _in-depth_ understanding
5555
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.
5758

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 />
6061
If you have any questions or get "stuck",
6162
please open an issue:
6263
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_!
6466

6567
<br /><br />
6668

6769
## _How_?
6870

6971
_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,
7274
we _avoid_ having to "correct" any "**bad habits**" later.
7375

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_.
7779
**`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 />
7981
If _either_ of these tools is _unfamiliar_ to you,
8082
please see:
8183
[https://github.com/dwyl/**learn-tape**](https://github.com/dwyl/learn-tape)
8284
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)
8486

8587

8688
### What _Can_ We _Generalise_ ?
@@ -107,20 +109,21 @@ which is the most _widely used_ "software architecture pattern".
107109
"how code is **organised**" and/or how "data **flows**" through a system.
108110
Whenever you see the word "**pattern**" it just means
109111
"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)."
111113

112114
The _reason_ Elm refers to the "**Controller**" as "***Update***" is because
113115
this name _more accurately_ reflects what the function _does_:
114116
it _updates_ the _state_ (Model) of the application.
115117

116118
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 />
118120
(_i.e. they are "**specific**" to the Todo List_)
119121
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.
121124

122125
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`
124127
each `case` will call a function
125128
that _belongs_ to the Todo List App. <br />
126129

@@ -145,7 +148,7 @@ The answer is: create **two** new files:
145148
### Test Setup
146149

147150
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.
149152

150153
In the `test/elmish.test.js` file, type the following code:
151154
```js
@@ -156,7 +159,6 @@ const html = fs.readFileSync(path.resolve(__dirname,
156159
'../examples/todo-list/index.html')); // sample HTML file to initialise JSDOM.
157160
require('jsdom-global')(html); // https://github.com/rstacruz/jsdom-global
158161
const elmish = require('../examples/todo-list/elmish.js'); // functions to test
159-
elmish.init(document); // pass JSDOM into elmish for DOM functions
160162
const id = 'test-app'; // all tests use 'test-app' as root element
161163
```
162164

@@ -166,61 +168,7 @@ if you have followed previous tutorials.
166168
https://github.com/dwyl/learn-tape
167169
and
168170

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`
224172
you should see no output.
225173
(_this is expected as we haven't written any tests yet!_)
226174

@@ -332,12 +280,21 @@ function empty(node) {
332280
}
333281
}
334282
```
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+
336292
```js
293+
/* module.exports is needed to run the functions using Node.js for testing! */
294+
/* istanbul ignore next */
337295
if (typeof module !== 'undefined' && module.exports) {
338296
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.
341298
}
342299
} else { init(document); }
343300
```

examples/todo-list/elmish.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,6 @@ function mount(model, update, view, root_element_id) {
207207
// return new_state;
208208
// }
209209

210-
/**
211-
* `init` initialises the document (Global) variable for DOM operations.
212-
* @param {Object} doc window.document in browser and JSDOM.document in tests.
213-
* @return {Object} document returns whatever is passed in.
214-
*/
215-
function init(doc){
216-
document = doc; // this is used for instantiating JSDOM for testing.
217-
return document;
218-
}
219-
220210
/* module.exports is needed to run the functions using Node.js for testing! */
221211
/* istanbul ignore next */
222212
if (typeof module !== 'undefined' && module.exports) {
@@ -228,7 +218,6 @@ if (typeof module !== 'undefined' && module.exports) {
228218
// div: div,
229219
empty: empty,
230220
// footer: footer,
231-
init: init,
232221
// input: input,
233222
// h1, h1,
234223
// header: header,
@@ -242,6 +231,6 @@ if (typeof module !== 'undefined' && module.exports) {
242231
// text: text,
243232
// ul: ul
244233
}
245-
} else { init(document); }
234+
}
246235

247236
})();

test/elmish.test.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const elmish = require('../examples/todo-list/elmish.js');
55
const html = fs.readFileSync(path.resolve(__dirname,
66
'../examples/todo-list/index.html'));
77
require('jsdom-global')(html); // https://github.com/rstacruz/jsdom-global
8-
elmish.init(document); // pass JSDOM into elmish for DOM functions
98
const id = 'test-app'; // all tests use separate root element
109

1110
test('elmish.empty("root") removes DOM elements from container', function (t) {

0 commit comments

Comments
 (0)