-
Notifications
You must be signed in to change notification settings - Fork 41
Removes store caching during SSR #307
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/localStorageStore.test.ts → test/localStorageStore.jsdom.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // @vitest-environment node | ||
| import { persisted, writable } from '../index' | ||
| import { get } from 'svelte/store' | ||
| import { expect, vi, describe, test, it } from 'vitest' | ||
|
|
||
| describe('writable()', () => { | ||
| test('it works, but raises deprecation warning', () => { | ||
| console.warn = vi.fn() | ||
|
|
||
| const store = writable('myKey2', 'initial') | ||
| const value = get(store) | ||
|
|
||
| expect(value).toEqual('initial') | ||
| expect(console.warn).toHaveBeenCalledWith(expect.stringMatching(/deprecated/)) | ||
| }) | ||
| }) | ||
|
|
||
| describe('persisted()', () => { | ||
| test('uses initial value if nothing in local storage', () => { | ||
| const store = persisted('myKey', 123) | ||
| const value = get(store) | ||
|
|
||
| expect(value).toEqual(123) | ||
| }) | ||
|
|
||
| describe('set()', () => { | ||
| test('replaces old value', () => { | ||
| const store = persisted('myKey3', '') | ||
| store.set('new-value') | ||
| const value = get(store) | ||
|
|
||
| expect(value).toEqual('new-value') | ||
| }) | ||
|
|
||
| test('adds new value', () => { | ||
| const store = persisted('myKey4', '') | ||
| store.set('new-value') | ||
| const value = get(store) | ||
|
|
||
| expect(value).toEqual('new-value') | ||
| }) | ||
| }) | ||
|
|
||
| describe('update()', () => { | ||
| test('replaces old value', () => { | ||
| const store = persisted('myKey5', 123) | ||
| store.update(n => n + 1) | ||
| const value = get(store) | ||
|
|
||
| expect(value).toEqual(124) | ||
| }) | ||
|
|
||
| test('adds new value', () => { | ||
| const store = persisted('myKey6', 123) | ||
| store.update(n => n + 1) | ||
| const value = get(store) | ||
|
|
||
| expect(value).toEqual(124) | ||
| }) | ||
| }) | ||
|
|
||
| describe('reset', () => { | ||
| it('resets to initial value', () => { | ||
| const store = persisted('myKey14', 123); | ||
| store.set(456); | ||
| store.reset(); | ||
| const value = get(store); | ||
|
|
||
| expect(value).toEqual(123); | ||
| }); | ||
| }); | ||
|
|
||
| describe('subscribe()', () => { | ||
| it('publishes updates', () => { | ||
| const store = persisted('myKey7', 123) | ||
| const values: number[] = [] | ||
| const unsub = store.subscribe((value: number) => { | ||
| if (value !== undefined) values.push(value) | ||
| }) | ||
| store.set(456) | ||
| store.set(999) | ||
|
|
||
| expect(values).toEqual([123, 456, 999]) | ||
|
|
||
| unsub() | ||
| }) | ||
| }) | ||
|
|
||
| it("doesn't handle duplicate stores with the same key", () => { | ||
| const store1 = persisted('same-key', 1) | ||
| const values1: number[] = [] | ||
|
|
||
| const unsub1 = store1.subscribe(value => { | ||
| values1.push(value) | ||
| }) | ||
|
|
||
| store1.set(2) | ||
|
|
||
| const store2 = persisted('same-key', 99) | ||
| const values2: number[] = [] | ||
|
|
||
| const unsub2 = store2.subscribe(value => { | ||
| values2.push(value) | ||
| }) | ||
|
|
||
| store1.set(3) | ||
| store2.set(4) | ||
|
|
||
| expect(values1).toEqual([1, 2, 3]) | ||
| expect(values2).toEqual([99, 4]) | ||
| expect(get(store1)).not.toEqual(get(store2)) | ||
|
|
||
| expect(store1).not.toEqual(store2) | ||
|
|
||
| unsub1() | ||
| unsub2() | ||
| }) | ||
|
|
||
| it('allows custom serialize/deserialize functions', () => { | ||
| const serializer = { | ||
| stringify: (set: Set<number>) => JSON.stringify(Array.from(set)), | ||
| parse: (json: string) => new Set(JSON.parse(json)), | ||
| } | ||
|
|
||
| const testSet = new Set([1, 2, 3]) | ||
|
|
||
| const store = persisted('myKey11', testSet, { serializer }) | ||
| const value = get(store) | ||
|
|
||
| store.update(d => d.add(4)) | ||
|
|
||
| expect(value).toEqual(testSet) | ||
| }) | ||
|
|
||
| it('lets you switch storage type', () => { | ||
| const store = persisted('myKey12', 'foo', { | ||
| storage: 'session' | ||
| }) | ||
|
|
||
| store.set('bar') | ||
|
|
||
| expect(get(store)).toEqual('bar') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If event.newValue is falsy, are you not interpreting that as a value deletion? I don't remember if this NPM package deletes values or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
event.newValueis a JSON string, so if it's falsey, it's because it's undefined.If memory serves, it's undefined when the key was deleted in another tab.