import { createEntityAdapter, EntityAdapter } from '@ngrx/entity'; import { AddTabAction, CloseTabAction, SetActiveTabAction, } from '../action/tabs.action'; import { TabEntity } from '../models/tab-entity'; import { reducer, TabState } from './tabs.reducer'; import { LocalState, initialState } from '../state/tabs.state'; import { TabsModule } from '../tabs.module'; describe('Tabs Reducer', () => { let adapter: EntityAdapter; beforeEach(() => { adapter = createEntityAdapter({ selectId: (entity: TabEntity) => entity.tabName, sortComparer: false, }); }); it('should let you get the initial state', () => { const testInitialState: LocalState = adapter.getInitialState(initialState); expect(testInitialState).toEqual({ errors: undefined, loading: false, activeTabIndex: 0, ids: [], entities: {}, }); }); describe('actions', () => { it('should Add Tab', () => { const tab: TabEntity = { label: 'Case Summary', tabName: 'caseSummary', route: '/case-summary', canClose: false, position: 0, }; const entities = { caseSummary: { label: 'Case Summary', tabName: 'caseSummary', route: '/case-summary', canClose: false, position: 0, }, }; const action: AddTabAction = new AddTabAction( tab ); const updatedState: LocalState = reducer(initialState, action); expect(updatedState.entities).toEqual(entities); expect(updatedState.activeTabIndex).toEqual(0); }); it('should Close Tab', () => { const tabState: LocalState = { errors: undefined, loading: false, activeTabIndex: 0, ids: [], entities: { 0: { label: 'Case Summary', tabName: 'caseSummary', route: '/case-summary', canClose: false, position: 0, }, }, }; const action: CloseTabAction = new CloseTabAction( 0 ); const updatedState: LocalState = reducer(tabState, action); expect(updatedState.entities).toEqual({}); expect(updatedState.activeTabIndex).toEqual(0); }); it('should SetActive', () => { const tab: TabEntity = { label: 'Case Summary', tabName: 'caseSummary', route: '/case-summary', canClose: false, position: 0, }; const action: SetActiveTabAction = new SetActiveTabAction( tab ); const updatedState: LocalState = reducer(initialState, action); expect(updatedState.activeTabIndex).toEqual(0); }); }); });