Skip to content

Commit

Permalink
Set serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
aubergene authored and joshnuss committed Aug 2, 2022
1 parent dfe6479 commit 7d1af48
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions test/localStorageStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,19 @@ describe('writable()', () => {
})

it('allows custom serialize/deserialize functions', () => {
const serializer = JSON
const serializer = {
stringify: (set: Set<number>) => JSON.stringify(Array.from(set)),
parse: (json: string) => new Set(JSON.parse(json)),
}

const s = new Set([1, 2, 3])
const testSet = new Set([1, 2, 3])

const store = writable('myKey11', s, { serializer })
const store = writable('myKey11', testSet, { serializer })
const value = get(store)

store.update(d => d.add(4))

expect(value).toEqual(s)
expect(localStorage.myKey11).toEqual(serializer.stringify(s))
expect(value).toEqual(testSet)
expect(localStorage.myKey11).toEqual(serializer.stringify(testSet))
})
})

0 comments on commit 7d1af48

Please sign in to comment.