diff --git a/src/-private/create-store.ts b/src/-private/create-store.ts index 01c6d2b..ea97f74 100644 --- a/src/-private/create-store.ts +++ b/src/-private/create-store.ts @@ -31,20 +31,23 @@ export default function createStore( ); const originalGetState = store.getState.bind(store); - - let rootNode; - - store.getState = (): S => { + const ensureRootNode = (): void => { if (rootNode === undefined) { rootNode = createNode({ state: originalGetState(), }); } + } + let rootNode; + + store.getState = (): S => { + ensureRootNode(); return rootNode.proxy.state; }; store.subscribe(() => { + ensureRootNode(); updateNode(rootNode, { state: originalGetState(), }); diff --git a/tests/unit/basic-test.js b/tests/unit/basic-test.js index 4b58d09..8f799fa 100644 --- a/tests/unit/basic-test.js +++ b/tests/unit/basic-test.js @@ -25,7 +25,15 @@ function createCache(fn) { module('Unit | basic', () => { module('primitive root', () => { - test('it works', (assert) => { + test('redux works', (assert) => { + let store = createStore((state = 0) => ++state); + + store.dispatch({ type: 'INCREMENT' }); + + assert.equal(store.getState(), 2, 'value is correct'); + }); + + test('caching works', (assert) => { let store = createStore((state = 0) => ++state); let cache = createCache(() => store.getState());