@@ -1285,6 +1285,14 @@ localStorage.setItem('key', "World");
1285
1285
console .log (" Hello " + localStorage .getItem (' key' )); // Hello World
1286
1286
```
1287
1287
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
+
1288
1296
#### Try it!
1289
1297
1290
1298
As always, the best way to familiarise yourself with a DOM API
@@ -1343,13 +1351,14 @@ function mount(model, update, view, root_element_id) {
1343
1351
}
1344
1352
```
1345
1353
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 ` ,
1347
1356
but _ first_ let's write a *** test*** for the desired outcome!
1348
1357
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 />:
1350
1359
1351
1360
``` js
1352
- // Testing localStorage requires "polyfil" because:
1361
+ // Testing localStorage requires a "polyfil" because it's unavailable in JSDOM :
1353
1362
// https://github.com/jsdom/jsdom/issues/1137 ¯\_(ツ)_/¯
1354
1363
global .localStorage = { // globals are bad! but a "necessary evil" here ...
1355
1364
getItem : function (key ) {
@@ -1385,7 +1394,7 @@ test.only('elmish.mount sets model in localStorage', function (t) {
1385
1394
elmish .mount (42 , update, view, id); // model (42) should be ignored this time!
1386
1395
t .equal (JSON .parse (localStorage .getItem (' elmish_store' )), 7 ,
1387
1396
" elmish_store is 7 (as expected). initial state saved to localStorage." );
1388
- // reset to zero:
1397
+ // increment the counter
1389
1398
const btn = root .getElementsByClassName (" inc" )[0 ]; // click increment button
1390
1399
btn .click (); // Click the Increment button!
1391
1400
const state = parseInt (root .getElementsByClassName (' count' )[0 ]
@@ -1394,14 +1403,32 @@ test.only('elmish.mount sets model in localStorage', function (t) {
1394
1403
// the "model" stored in localStorage should also be 8 now:
1395
1404
t .equal (JSON .parse (localStorage .getItem (' elmish_store' )), 8 ,
1396
1405
" 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.
1398
1408
// clearing DOM does NOT clear the localStorage (this is desired behaviour!)
1399
1409
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 " );
1401
1411
t .end ()
1402
1412
});
1403
1413
```
1404
1414
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
+
1405
1432
1406
1433
<!--
1407
1434
0 commit comments