Skip to content

Commit e7d542f

Browse files
committed
adds "Acceptance Criteria" for localStorage functionality. closes #47
1 parent e0d64de commit e7d542f

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

Diff for: examples/todo-list/elmish.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function() { //
1+
(function() { // scope elmish to prevent conflicts if used elsewhere
22
/**
33
* `empty` the contents of a given DOM element "node" (before re-rendering).
44
* This is the *fastest* way according to: stackoverflow.com/a/3955238/1148249

Diff for: test/elmish.test.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -388,18 +388,19 @@ test('elmish.mount sets model in localStorage', function (t) {
388388
elmish.mount(42, update, view, id); // model (42) should be ignored this time!
389389
t.equal(JSON.parse(localStorage.getItem('elmish_store')), 7,
390390
"elmish_store is 7 (as expected). initial state saved to localStorage.");
391-
// reset to zero:
391+
// increment the counter
392392
const btn = root.getElementsByClassName("inc")[0]; // click increment button
393393
btn.click(); // Click the Increment button!
394394
const state = parseInt(root.getElementsByClassName('count')[0]
395395
.textContent, 10);
396-
t.equal(state, 8, "State is 8 after increment."); // state incremented
396+
t.equal(state, 8, "State is 8 after increment.");
397397
// the "model" stored in localStorage should also be 8 now:
398398
t.equal(JSON.parse(localStorage.getItem('elmish_store')), 8,
399399
"elmish_store is 8 (as expected).");
400-
elmish.empty(root); // reset the DOM
400+
elmish.empty(root); // reset the DOM to simulate refreshing a browser window
401+
elmish.mount(5, update, view, id); // 5 ignored! read model from localStorage
401402
// clearing DOM does NOT clear the localStorage (this is desired behaviour!)
402403
t.equal(JSON.parse(localStorage.getItem('elmish_store')), 8,
403-
"elmish_store still 0 (zero)");
404+
"elmish_store still 8 from increment (above) saved in localStorage");
404405
t.end()
405406
});

Diff for: todo-list.md

+33-6
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,14 @@ localStorage.setItem('key', "World");
12851285
console.log("Hello " + localStorage.getItem('key')); // Hello World
12861286
```
12871287

1288+
#### Acceptance Criteria
1289+
1290+
+ [ ] `model` is ***retrieved*** from `localStorage`
1291+
if it has been (_previously_) set when `mount` is invoked
1292+
+ [ ] Initial `model` is ***saved*** to `localStorage` when `mount` is invoked
1293+
+ [ ] ***Updated*** `model` is ***saved*** to `localStorage`
1294+
when `update` is called. (_thus_ `localStorage` _always has the latest version_)
1295+
12881296
#### Try it!
12891297

12901298
As always, the best way to familiarise yourself with a DOM API
@@ -1343,13 +1351,14 @@ function mount(model, update, view, root_element_id) {
13431351
}
13441352
```
13451353

1346-
We are going to make 3 adjustments to this code to use `setItem` and `getItem`,
1354+
We are going to make **3 adjustments** to this code
1355+
to use `setItem` and `getItem`,
13471356
but _first_ let's write a ***test*** for the desired outcome!
13481357

1349-
Add the following _test_ to your `test/elmish.test.js` file: <br />:
1358+
Add the following _test code_ to your `test/elmish.test.js` file: <br />:
13501359

13511360
```js
1352-
// Testing localStorage requires "polyfil" because:
1361+
// Testing localStorage requires a "polyfil" because it's unavailable in JSDOM:
13531362
// https://github.com/jsdom/jsdom/issues/1137 ¯\_(ツ)_/¯
13541363
global.localStorage = { // globals are bad! but a "necessary evil" here ...
13551364
getItem: function(key) {
@@ -1385,7 +1394,7 @@ test.only('elmish.mount sets model in localStorage', function (t) {
13851394
elmish.mount(42, update, view, id); // model (42) should be ignored this time!
13861395
t.equal(JSON.parse(localStorage.getItem('elmish_store')), 7,
13871396
"elmish_store is 7 (as expected). initial state saved to localStorage.");
1388-
// reset to zero:
1397+
// increment the counter
13891398
const btn = root.getElementsByClassName("inc")[0]; // click increment button
13901399
btn.click(); // Click the Increment button!
13911400
const state = parseInt(root.getElementsByClassName('count')[0]
@@ -1394,14 +1403,32 @@ test.only('elmish.mount sets model in localStorage', function (t) {
13941403
// the "model" stored in localStorage should also be 8 now:
13951404
t.equal(JSON.parse(localStorage.getItem('elmish_store')), 8,
13961405
"elmish_store is 8 (as expected).");
1397-
elmish.empty(root); // reset the DOM
1406+
elmish.empty(root); // reset the DOM to simulate refreshing a browser window
1407+
elmish.mount(5, update, view, id); // 5 ignored! model read from localStorage.
13981408
// clearing DOM does NOT clear the localStorage (this is desired behaviour!)
13991409
t.equal(JSON.parse(localStorage.getItem('elmish_store')), 8,
1400-
"elmish_store still 0 (zero)");
1410+
"elmish_store still 8 from increment (above) saved in localStorage");
14011411
t.end()
14021412
});
14031413
```
14041414

1415+
There is quite a lot to "unpack" in this test but let's break it down:
1416+
1. Require the `view` and `update` from our counter reset example.
1417+
2. `mount` the counter reset app
1418+
3. ***test*** that the `model` (7) is being saved to `localStorage`
1419+
by `mount` function.
1420+
4. Attempt to "***re-mount***" the counter app with an initial model of `42`
1421+
should not work because `mount` will "read"
1422+
the initial model from `localStorage` if it is defined.
1423+
5. `update` the model using the `inc` (_increment_) action
1424+
6. ***test*** that `localStorage` was updated to reflect the increment (8)
1425+
7. ***`empty`*** the DOM. (_to simulate a page being refreshed_)
1426+
8. ***test*** that `localStorage` still contains our saved data.
1427+
1428+
Based on this test, try to add the necessary lines to the `mount` function,
1429+
to make the test pass.
1430+
1431+
14051432

14061433
<!--
14071434

0 commit comments

Comments
 (0)