import { expect } from 'chai'; import { createStore } from '../store'; import { StoreInternal, topDown } from '.'; interface IMyState { text: string; } type IMyActions = IFooAction; interface IFooAction { type: 'FOO'; payload: {}; } type MyStoreInternal = StoreInternal; function createTestHierarchy() { const root = createStore({ text: 'root', }); const child = createStore({ text: 'child' }); const grandchild = createStore({ text: 'grandchild', }); root.add('child', child); child.add('grandchild', grandchild); return { rootInternal: root as MyStoreInternal, root, child, grandchild, }; } /** * TESTS */ describe('dispatcher (helpers)', () => { it('topDown', () => { const { rootInternal, root, child, grandchild } = createTestHierarchy(); const calls: Array<{ level: MyStoreInternal; levelIndex: number }> = []; topDown(rootInternal, (level: MyStoreInternal, levelIndex) => { calls.push({ level, levelIndex }); }); expect(calls.length).to.eql(3); expect(calls[0].level).to.eql(root); expect(calls[1].level).to.eql(child); expect(calls[2].level).to.eql(grandchild); expect(calls[0].levelIndex).to.eql(0); expect(calls[1].levelIndex).to.eql(1); expect(calls[2].levelIndex).to.eql(2); }); });