Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fire iron-localstorage-load-empty event when localStorage is cleared from another window #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion iron-localstorage.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
},

_handleStorage: function(ev) {
if (ev.key == this.name) {
if (ev.key == this.name || !ev.key) {
this._load(true);
}
},
Expand Down
118 changes: 118 additions & 0 deletions test/external-change.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<!doctype html>
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<meta charset="UTF-8">
<title>iron-localstorage-external-change</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

<script src="../../webcomponentsjs/webcomponents.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<link rel="import" href="../iron-localstorage.html">

</head>
<body>

<dom-module id="test-element" is="dom-bind">
<template>
<iron-localstorage id="localstorage" autoSaveDisabled name="iron-localstorage-test" value="{{value}}"></iron-localstorage>
</template>
</dom-module>

<iframe id="myframe" style="display:none"></iframe>

<script>

var testElement;
var externalWindow;
var setLoadEmptyListener;

suite('external-change', function() {

suiteSetup(function() {
Polymer({
is: 'test-element',
properties: {
'value': {
type: Object,
notify: true
}
}
});

// In order to fire the 'storage' event, create a separate window.
externalWindow = document.querySelector('iframe');

// listener function
setLoadEmptyListener = function(done) {
// Any external window that manipulates the localStorage will fire a 'storage' event.
// iron-localstorage listens for this, handles the event and fires iron-localstorage-load-empty
testElement.$.localstorage.addEventListener('iron-localstorage-load-empty', function() {
assert.isNull(testElement.value);
assert.equal(window.localStorage.length, 0);
done();

});
};

});

suiteTeardown(function() {
window.localStorage.clear();
});

setup(function(done) {
// Each test should have its own clean elements.
if (testElement) {
// remove <test-element>
testElement.parentNode.removeChild(testElement);
}

// start with empty local storage
window.localStorage.clear();

// Set test value
window.localStorage.setItem('iron-localstorage-test', '{"foo":"bar"}');

// create (and load) <test-element>
testElement = document.createElement('test-element');
document.body.appendChild(testElement);

// listen for load event and make sure everything is loaded correctly
testElement.$.localstorage.addEventListener('iron-localstorage-load', function () {
var storageValue = JSON.parse(window.localStorage.getItem('iron-localstorage-test'));
assert.equal(testElement.value.foo, storageValue.foo);
assert.equal(testElement.value.foo, 'bar');
done();
});

});


test('clear', function(done) {
setLoadEmptyListener(done);

// clear localStorage initiated by external window
externalWindow.contentWindow.localStorage.clear();
});

test('remove item', function(done) {
setLoadEmptyListener(done);

// remove item initiated by window
externalWindow.contentWindow.localStorage.removeItem('iron-localstorage-test');
});
});

</script>

</body>
</html>
4 changes: 3 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
'basic.html',
'raw.html',
'value-binding.html',
'external-change.html',
'basic.html?dom=shadow',
'raw.html?dom=shadow',
'value-binding.html?dom=shadow'
'value-binding.html?dom=shadow',
'external-change.html?dom=shadow'
]);
</script>

Expand Down