Skip to content

Commit

Permalink
chore: add store tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzianis Dashkevich committed Oct 5, 2024
1 parent 99e634d commit a26b2ec
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/playback/test/utils/store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { describe, expect, it } from 'vitest';
import { Store, StoreNode } from '../../src/lib/utils/store';

describe('Store', () => {
it('should initialize with a snapshot matching the initial state', () => {
const initialState = { key: 'value' };
const init = (): StoreNode<{ key: string }> => new StoreNode(initialState);
const store = new Store(init);

expect(store.getSnapshot()).toEqual(initialState);
});

it('should return the current state accurately when called', () => {
const initialState = { key: { inner: 'innerValue' } };

const innerStoreNode = new StoreNode(initialState.key);
const init = (): StoreNode<{ key: { inner: string } }> => new StoreNode({ key: innerStoreNode });
const store = new Store(init);

const snapshot = store.getSnapshot();

expect(snapshot).toEqual(initialState);
expect(snapshot === initialState).toBe(false);
expect(snapshot.key === initialState.key).toBe(false);
});

it("should update store's state correctly when called", () => {
const initialState = { key: { inner: 'innerValue', inner2: 'inner2Value' } };

const innerStoreNode = new StoreNode(initialState.key);
const init = (): StoreNode<{ key: { inner: string; inner2: string } }> => new StoreNode({ key: innerStoreNode });
const store = new Store(init);

store.update({ key: { inner2: 'inner2Value-updated' } });

const snapshot = store.getSnapshot();
expect(snapshot).toEqual({ key: { inner: 'innerValue', inner2: 'inner2Value-updated' } });
});

it('should return expected results when reading based on current state', () => {
const initialState = { key: { inner: 'innerValue', inner2: 'inner2Value' } };

const innerStoreNode = new StoreNode(initialState.key);
const init = (): StoreNode<{ key: { inner: string; inner2: string } }> => new StoreNode({ key: innerStoreNode });
const store = new Store(init);

expect(store.read((state) => state.key.inner)).toBe('innerValue');
});

it('read should not affect internal store data', () => {
const initialState = { key: { inner: 'innerValue', inner2: 'inner2Value' } };

const innerStoreNode = new StoreNode(initialState.key);
const init = (): StoreNode<{ key: { inner: string; inner2: string } }> => new StoreNode({ key: innerStoreNode });
const store = new Store(init);

expect(
store.read((state) => {
state.key.inner2 = 'inner2Value-updated';

return state.key.inner;
})
).toBe('innerValue');

expect(store.getSnapshot()).toEqual(initialState);
});

// Reset method reverts store to initial state
it('should reset store to initial state when reset is called', () => {
const initialState = { key: { inner: 'innerValue', inner2: 'inner2Value' } };

const innerStoreNode = new StoreNode(initialState.key);
const init = (): StoreNode<{ key: { inner: string; inner2: string } }> => new StoreNode({ key: innerStoreNode });
const store = new Store(init);

store.update({ key: { inner: 'innerValue', inner2: 'inner2Value-updated' } });
store.reset();

expect(store.getSnapshot()).toEqual(initialState);
});
});

0 comments on commit a26b2ec

Please sign in to comment.