From 43f827cfc9c5f59cfdff76261610e43e7eea9ffb Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sat, 23 Nov 2019 13:55:58 +0100 Subject: [PATCH] test: refactor store test --- __tests__/createStore.spec.ts | 20 -------------- __tests__/store.spec.ts | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 20 deletions(-) delete mode 100644 __tests__/createStore.spec.ts create mode 100644 __tests__/store.spec.ts diff --git a/__tests__/createStore.spec.ts b/__tests__/createStore.spec.ts deleted file mode 100644 index 8a6c0434..00000000 --- a/__tests__/createStore.spec.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { createStore } from '../src' - -describe('createStore', () => { - it('sets the initial state', () => { - const state = () => ({ - a: true, - nested: { - a: { b: 'string' }, - }, - }) - - const store = createStore('main', state) - expect(store.state).toEqual({ - a: true, - nested: { - a: { b: 'string' }, - }, - }) - }) -}) diff --git a/__tests__/store.spec.ts b/__tests__/store.spec.ts new file mode 100644 index 00000000..6e91a94b --- /dev/null +++ b/__tests__/store.spec.ts @@ -0,0 +1,52 @@ +import { createStore } from '../src' + +describe('Store', () => { + function buildStore() { + return createStore('main', () => ({ + a: true as boolean, + nested: { + foo: 'foo', + a: { b: 'string' }, + }, + })) + } + + it('sets the initial state', () => { + const store = buildStore() + expect(store.state).toEqual({ + a: true, + nested: { + foo: 'foo', + a: { b: 'string' }, + }, + }) + }) + + it('can replace its state', () => { + const store = buildStore() + store.replaceState({ + a: false, + nested: { + foo: 'bar', + a: { + b: 'hey', + }, + }, + }) + expect(store.state).toEqual({ + a: false, + nested: { + foo: 'bar', + a: { b: 'hey' }, + }, + }) + }) + + it('do not share the state between same id store', () => { + const store = buildStore() + const store2 = buildStore() + expect(store.state).not.toBe(store2.state) + store.state.nested.a.b = 'hey' + expect(store2.state.nested.a.b).toBe('string') + }) +}) -- 2.47.2