import { cloneDeep } from 'lodash'; import { AlertModel, AlertTypes } from '../types/AlertStoreModel'; import RootState from '../types/RootStateModel'; import { CmsStore, storeConfig } from '../index'; jest.useFakeTimers(); describe('Alerts store', () => { function createLocalStore(): CmsStore { return new CmsStore(cloneDeep(storeConfig)); } it('should add and remove various alerts', () => { const store = createLocalStore(); expect(store.state.alerts.all).toEqual([]); store.dispatch('alerts/addSuccess', 'Success message'); store.dispatch('alerts/addWarning', 'Warning message'); store.dispatch('alerts/addInfo', 'Info message'); store.dispatch('alerts/addError', 'Danger message'); expect(store.state.alerts.all).toEqual([ { html: 'Success message', type: AlertTypes.Success }, { html: 'Warning message', type: AlertTypes.Warning }, { html: 'Info message', type: AlertTypes.Info }, { html: 'Danger message', type: AlertTypes.Danger }, ]); store.dispatch('alerts/remove', (store.state.alerts.all.filter((alert: AlertModel) => alert.type === AlertTypes.Warning).shift())); expect(store.state.alerts.all).toEqual([ { html: 'Success message', type: AlertTypes.Success }, { html: 'Info message', type: AlertTypes.Info }, { html: 'Danger message', type: AlertTypes.Danger }, ]); }); it('should auto remove messages every 10 seconds', () => { const store = createLocalStore(); expect(store.state.alerts.all).toEqual([]); store.dispatch('alerts/addSuccess', 'Success message'); store.dispatch('alerts/addWarning', 'Warning message'); store.dispatch('alerts/addInfo', 'Info message'); store.dispatch('alerts/addError', 'Danger message'); expect(store.state.alerts.all).toEqual([ { html: 'Success message', type: AlertTypes.Success }, { html: 'Warning message', type: AlertTypes.Warning }, { html: 'Info message', type: AlertTypes.Info }, { html: 'Danger message', type: AlertTypes.Danger }, ]); expect(setTimeout).toHaveBeenCalledTimes(4); expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 10 * 1000); jest.runAllTimers(); expect(store.state.alerts.all).toEqual([]); }); });