bonsai-analyzer
Version:
Trim your dependency tree.
187 lines (158 loc) • 5.35 kB
JavaScript
/**
* @flow
*/
import reducer, { INITIAL_STATE } from '../reducer';
import {
DiscoveredDataPaths,
PickDataPath,
RequestedDataAtPath,
LoadedStatsAtPath,
ErroredAtPath,
} from '../actions';
expect.extend({
toCloneState(action) {
const state = {...INITIAL_STATE};
const newState = reducer(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(DiscoveredDataPaths(mockDispatch)([])).toCloneState();
});
it('should append to the list of found dataPaths', () => {
const state = {...INITIAL_STATE};
const action = DiscoveredDataPaths(mockDispatch)(
['test-file.json', 'another-file.json'],
);
const newState = reducer(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 = {
...INITIAL_STATE,
dataPaths: {
'test-file.json': 'ready',
},
};
const action = DiscoveredDataPaths(mockDispatch)(
['test-file.json', 'another-file.json'],
);
const newState = reducer(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(PickDataPath(mockDispatch)('test-file.json')).toCloneState();
});
it('should ensure the filename is in dataPaths', () => {
const state = {...INITIAL_STATE};
const action = PickDataPath(mockDispatch)('test-file.json');
const newState = reducer(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 = {
...INITIAL_STATE,
dataPaths: {
'test-file.json': 'ready',
},
};
const action = PickDataPath(mockDispatch)('test-file.json');
const newState = reducer(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(RequestedDataAtPath(mockDispatch)('test-file.json')).toCloneState();
});
it('should ensure the filename is loading in dataPaths', () => {
const state = {...INITIAL_STATE};
const action = RequestedDataAtPath(mockDispatch)('test-file.json');
const newState = reducer(state, action);
expect(newState).toEqual(expect.objectContaining({
dataPaths: {
'test-file.json': 'loading',
},
}));
});
it('should not munge dataPaths when the path is ready', () => {
const state = {
...INITIAL_STATE,
dataPaths: {
'test-file.json': 'ready',
},
};
const action = RequestedDataAtPath(mockDispatch)('test-file.json');
const newState = reducer(state, action);
expect(newState).toEqual(expect.objectContaining({
dataPaths: {
'test-file.json': 'ready',
},
}));
});
});
describe('LoadedStatsAtPath action', () => {
it('should clone the state when reduced', () => {
expect(LoadedStatsAtPath(mockDispatch)('test-file.json', {chunks:[], modules: []})).toCloneState();
});
it('should ensure the filename is ready in dataPaths', () => {
const state = {...INITIAL_STATE};
const action = LoadedStatsAtPath(mockDispatch)('test-file.json', {chunks:[], modules: []});
const newState = reducer(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(ErroredAtPath(mockDispatch)('test-file.json')).toCloneState();
});
it('should ensure the filename is errored in dataPaths', () => {
const state = {...INITIAL_STATE};
const action = ErroredAtPath(mockDispatch)('test-file.json', 'Bad things');
const newState = reducer(state, action);
expect(newState).toEqual(expect.objectContaining({
dataPaths: {
'test-file.json': 'error',
},
}));
});
});
});