Skip to content

Commit

Permalink
test: Refactor store test
Browse files Browse the repository at this point in the history
Generate random key for persistent store to avoid interference between
store tests.
  • Loading branch information
lasuillard committed Oct 6, 2023
1 parent 199f97e commit c5e2fcc
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions tests/lib/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { describe, expect, it } from 'vitest';

describe('persisted store', () => {
it('should conform to store interface and use browser local storage by default', () => {
const store = persisted('test', 'default-value');

const key = Math.random().toString();
const store = persisted(key, 'default-value');
expect(get(store)).toEqual('default-value');

let changed = '';
Expand All @@ -16,18 +16,18 @@ describe('persisted store', () => {

store.set('hello world');
expect(changed).toEqual('hello world');
expect(localStorage.getItem('test')).toEqual('"hello world"');
expect(localStorage.getItem(key)).toEqual('"hello world"');

store.update((value) => value + ', done');
expect(changed).toEqual('hello world, done');
expect(localStorage.getItem('test')).toEqual('"hello world, done"');
expect(localStorage.getItem(key)).toEqual('"hello world, done"');
});

it('should compatible with browser session storage', () => {
const store = persisted('test', 'default-value', {
const key = Math.random().toString();
const store = persisted(key, 'default-value', {
storage: sessionStorage
});

expect(get(store)).toEqual('default-value');

let changed = '';
Expand All @@ -37,10 +37,10 @@ describe('persisted store', () => {

store.set('hello world');
expect(changed).toEqual('hello world');
expect(sessionStorage.getItem('test')).toEqual('"hello world"');
expect(sessionStorage.getItem(key)).toEqual('"hello world"');

store.update((value) => value + ', done');
expect(changed).toEqual('hello world, done');
expect(sessionStorage.getItem('test')).toEqual('"hello world, done"');
expect(sessionStorage.getItem(key)).toEqual('"hello world, done"');
});
});

0 comments on commit c5e2fcc

Please sign in to comment.