UNPKG

bonsai-analyzer

Version:
166 lines (159 loc) 5.76 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _reducer = _interopRequireWildcard(require("../reducer")); var _actions = require("../actions"); expect.extend({ toCloneState(action) { const state = { ..._reducer.INITIAL_STATE }; const newState = (0, _reducer.default)(state, action); const pass = !Object.is(state, newState); const message = pass ? () => `expected ${action} not to reduce into a new state object` : () => `expected ${action} to reduce into a new state object`; return { actual: action, message, pass }; } }); const mockDispatch = action => action; describe('reducer', () => { describe('DiscoveredDataPaths action', () => { it('should clone the state when reduced', () => { expect((0, _actions.DiscoveredDataPaths)(mockDispatch)([])).toCloneState(); }); it('should append to the list of found dataPaths', () => { const state = { ..._reducer.INITIAL_STATE }; const action = (0, _actions.DiscoveredDataPaths)(mockDispatch)(['test-file.json', 'another-file.json']); const newState = (0, _reducer.default)(state, action); expect(newState).toEqual(expect.objectContaining({ dataPaths: { 'test-file.json': 'unknown', 'another-file.json': 'unknown' } })); }); it('should not override status for previously known files', () => { const state = { ..._reducer.INITIAL_STATE, dataPaths: { 'test-file.json': 'ready' } }; const action = (0, _actions.DiscoveredDataPaths)(mockDispatch)(['test-file.json', 'another-file.json']); const newState = (0, _reducer.default)(state, action); expect(newState).toEqual(expect.objectContaining({ dataPaths: { 'test-file.json': 'ready', 'another-file.json': 'unknown' } })); }); }); describe('PickDataPath action', () => { it('should clone the state when reduced', () => { expect((0, _actions.PickDataPath)(mockDispatch)('test-file.json')).toCloneState(); }); it('should ensure the filename is in dataPaths', () => { const state = { ..._reducer.INITIAL_STATE }; const action = (0, _actions.PickDataPath)(mockDispatch)('test-file.json'); const newState = (0, _reducer.default)(state, action); expect(newState).toEqual(expect.objectContaining({ dataPaths: { 'test-file.json': 'unknown' }, selectedFilename: 'test-file.json' })); }); it('should not munge dataPaths when the path exists', () => { const state = { ..._reducer.INITIAL_STATE, dataPaths: { 'test-file.json': 'ready' } }; const action = (0, _actions.PickDataPath)(mockDispatch)('test-file.json'); const newState = (0, _reducer.default)(state, action); expect(newState).toEqual(expect.objectContaining({ dataPaths: { 'test-file.json': 'ready' }, selectedFilename: 'test-file.json' })); }); }); describe('RequestedDataAtPath action', () => { it('should clone the state when reduced', () => { expect((0, _actions.RequestedDataAtPath)(mockDispatch)('test-file.json')).toCloneState(); }); it('should ensure the filename is loading in dataPaths', () => { const state = { ..._reducer.INITIAL_STATE }; const action = (0, _actions.RequestedDataAtPath)(mockDispatch)('test-file.json'); const newState = (0, _reducer.default)(state, action); expect(newState).toEqual(expect.objectContaining({ dataPaths: { 'test-file.json': 'loading' } })); }); it('should not munge dataPaths when the path is ready', () => { const state = { ..._reducer.INITIAL_STATE, dataPaths: { 'test-file.json': 'ready' } }; const action = (0, _actions.RequestedDataAtPath)(mockDispatch)('test-file.json'); const newState = (0, _reducer.default)(state, action); expect(newState).toEqual(expect.objectContaining({ dataPaths: { 'test-file.json': 'ready' } })); }); }); describe('LoadedStatsAtPath action', () => { it('should clone the state when reduced', () => { expect((0, _actions.LoadedStatsAtPath)(mockDispatch)('test-file.json', { chunks: [], modules: [] })).toCloneState(); }); it('should ensure the filename is ready in dataPaths', () => { const state = { ..._reducer.INITIAL_STATE }; const action = (0, _actions.LoadedStatsAtPath)(mockDispatch)('test-file.json', { chunks: [], modules: [] }); const newState = (0, _reducer.default)(state, action); expect(newState).toEqual(expect.objectContaining({ dataPaths: { 'test-file.json': 'ready' }, jsonChildren: { 'test-file.json': expect.arrayContaining([expect.objectContaining({ chunks: [], modules: [] })]) } })); }); }); describe('ErroredAtPath action', () => { it('should clone the state when reduced', () => { expect((0, _actions.ErroredAtPath)(mockDispatch)('test-file.json')).toCloneState(); }); it('should ensure the filename is errored in dataPaths', () => { const state = { ..._reducer.INITIAL_STATE }; const action = (0, _actions.ErroredAtPath)(mockDispatch)('test-file.json', 'Bad things'); const newState = (0, _reducer.default)(state, action); expect(newState).toEqual(expect.objectContaining({ dataPaths: { 'test-file.json': 'error' } })); }); }); });