Skip to content

Commit

Permalink
fix: recover stored value on init
Browse files Browse the repository at this point in the history
Closes #25
  • Loading branch information
jorgecasar committed Feb 5, 2024
1 parent 326ddeb commit 279318f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/_internal/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export abstract class StorageController<T> implements ReactiveController {
this.__storage = storage;
this.__key = key;
this.__initialValue = defaultValue;
this.__value = this.__readValueFromStorage();

host.addController(this);
}
Expand Down
39 changes: 39 additions & 0 deletions src/test/controllers/localStorage_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,43 @@ suite('LocalStorageController', () => {
assert.is(storageValue, '[5,6]');
});
});

suite('with saved value', () => {
setup(() => {
window.localStorage.setItem('test-key', JSON.stringify([7, 8]));
element = document.createElement('test-element') as TestElement;
element.template = () => html`${JSON.stringify(controller.value)}`;
document.body.appendChild(element);
});

suite('without default value', () => {
setup(async () => {
controller = new LocalStorageController(element, 'test-key');
element.controllers.push(controller);
await element.updateComplete;
});

test('initialises to saved value', () => {
const storageValue = window.localStorage.getItem('test-key');
assert.equal(controller.value, [7, 8]);
assert.equal(element.shadowRoot!.textContent, '[7,8]');
assert.is(storageValue, '[7,8]');
});

suite('with default value', () => {
setup(async () => {
controller = new LocalStorageController(element, 'test-key', [5, 6]);
element.controllers.push(controller);
await element.updateComplete;
});

test('initialises to saved value', () => {
const storageValue = window.localStorage.getItem('test-key');
assert.equal(controller.value, [7, 8]);
assert.equal(element.shadowRoot!.textContent, '[7,8]');
assert.is(storageValue, '[7,8]');
});
});
});
});
});
43 changes: 43 additions & 0 deletions src/test/controllers/sessionStorage_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,48 @@ suite('SessionStorageController', () => {
assert.equal(element.shadowRoot!.textContent, '[5,6]');
assert.is(storageValue, '[5,6]');
});

suite('with saved value', () => {
setup(() => {
window.sessionStorage.setItem('test-key', JSON.stringify([7, 8]));
element = document.createElement('test-element') as TestElement;
element.template = () => html`${JSON.stringify(controller.value)}`;
document.body.appendChild(element);
});

suite('without default value', () => {
setup(async () => {
controller = new SessionStorageController(element, 'test-key');
element.controllers.push(controller);
await element.updateComplete;
});

test('initialises to saved value', () => {
const storageValue = window.sessionStorage.getItem('test-key');
assert.equal(controller.value, [7, 8]);
assert.equal(element.shadowRoot!.textContent, '[7,8]');
assert.is(storageValue, '[7,8]');
});

suite('with default value', () => {
setup(async () => {
controller = new SessionStorageController(
element,
'test-key',
[5, 6]
);
element.controllers.push(controller);
await element.updateComplete;
});

test('initialises to saved value', () => {
const storageValue = window.sessionStorage.getItem('test-key');
assert.equal(controller.value, [7, 8]);
assert.equal(element.shadowRoot!.textContent, '[7,8]');
assert.is(storageValue, '[7,8]');
});
});
});
});
});
});

0 comments on commit 279318f

Please sign in to comment.