| OLD | NEW |
| (Empty) | |
| 1 <!doctype html> |
| 2 <!-- |
| 3 @license |
| 4 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 5 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 6 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 7 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 8 Code distributed by Google as part of the polymer project is also |
| 9 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 10 --> |
| 11 <html> |
| 12 <head> |
| 13 <meta charset="UTF-8"> |
| 14 <title>iron-localstorage-basic</title> |
| 15 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-
scale=1.0"> |
| 16 |
| 17 <script src="../../webcomponentsjs/webcomponents.js"></script> |
| 18 <script src="../../web-component-tester/browser.js"></script> |
| 19 <script src="../../test-fixture/test-fixture-mocha.js"></script> |
| 20 |
| 21 <link rel="import" href="../../test-fixture/test-fixture.html"> |
| 22 <link rel="import" href="../iron-localstorage.html"> |
| 23 |
| 24 </head> |
| 25 <body> |
| 26 |
| 27 <test-fixture id="fixture"> |
| 28 <template> |
| 29 <iron-localstorage id="localstorage" name="iron-localstorage-test"></iron-
localstorage> |
| 30 </template> |
| 31 </test-fixture> |
| 32 |
| 33 <template id="boundTemplate" is="dom-bind"> |
| 34 <iron-localstorage id="boundLocal" name="iron-localstorage-test" value="{{va
lue}}"></iron-localstorage> |
| 35 </template> |
| 36 |
| 37 <script> |
| 38 var storage; |
| 39 |
| 40 suite('basic', function() { |
| 41 |
| 42 setup(function() { |
| 43 window.localStorage.setItem('iron-localstorage-test', '{"foo":"bar"}'); |
| 44 storage = document.getElementById('fixture').create(); |
| 45 storage.flushDebouncer('reload'); |
| 46 }); |
| 47 |
| 48 teardown(function() { |
| 49 window.localStorage.removeItem('iron-localstorage-test'); |
| 50 }); |
| 51 |
| 52 test('load', function() { |
| 53 assert.isNotNull(storage.value); |
| 54 assert.equal(storage.value.foo, 'bar'); |
| 55 }); |
| 56 |
| 57 test('save', function() { |
| 58 var newValue = {'foo': 'zot'}; |
| 59 storage.value = newValue; |
| 60 storage.flushDebouncer('save'); |
| 61 var v = window.localStorage.getItem(storage.name); |
| 62 v = JSON.parse(v); |
| 63 assert.equal(v.foo, newValue.foo); |
| 64 }); |
| 65 |
| 66 test('delete', function() { |
| 67 storage.value = null; |
| 68 storage.flushDebouncer('save'); |
| 69 var v = window.localStorage.getItem(storage.name); |
| 70 assert.isNull(v); |
| 71 }); |
| 72 |
| 73 test('event iron-localstorage-load', function(done) { |
| 74 var ls = document.createElement('iron-localstorage'); |
| 75 ls.addEventListener('iron-localstorage-load', function() { |
| 76 done(); |
| 77 }); |
| 78 ls.name = 'iron-localstorage-test'; |
| 79 }); |
| 80 |
| 81 test('event iron-localstorage-load-empty', function(done) { |
| 82 window.localStorage.removeItem('iron-localstorage-test'); |
| 83 |
| 84 var ls = document.createElement('iron-localstorage'); |
| 85 ls.addEventListener('iron-localstorage-load-empty', function() { |
| 86 // testing recommended way to initialize localstorage |
| 87 ls.value = "Yo"; |
| 88 ls.flushDebouncer('save'); |
| 89 assert.equal("Yo", JSON.parse( window.localStorage.getItem('iron-local
storage-test'))); |
| 90 done(); |
| 91 }); |
| 92 ls.name = 'iron-localstorage-test'; |
| 93 }); |
| 94 |
| 95 test('auto-save sub-properties', function() { |
| 96 var t = document.querySelector('#boundTemplate'); |
| 97 var ls = document.querySelector('#boundLocal'); |
| 98 var value = { foo: 'FOO', bar: 'BAR' }; |
| 99 t.value = value; |
| 100 assert.equal('FOO', ls.value.foo); // value has propagated from template
to storage |
| 101 |
| 102 ls.flushDebouncer('save'); |
| 103 t.value.foo = "Yo"; |
| 104 ls.flushDebouncer('save'); |
| 105 var item = JSON.parse( window.localStorage.getItem('iron-localstorage-te
st')); |
| 106 assert.notEqual('Yo', item.foo); // did not propagate because did not us
e setters |
| 107 |
| 108 t.set('value.foo', 'BAZ!'); |
| 109 ls.flushDebouncer('save'); |
| 110 var item = JSON.parse( window.localStorage.getItem('iron-localstorage-te
st')); |
| 111 assert.equal('BAZ!', item.foo); // did propagate |
| 112 ls.value = null; |
| 113 }); |
| 114 |
| 115 }); |
| 116 |
| 117 </script> |
| 118 |
| 119 </body> |
| 120 </html> |
| OLD | NEW |