Files
dspace-angular/src/app/core/index/index.reducer.spec.ts
2018-12-06 15:50:12 +01:00

61 lines
2.0 KiB
TypeScript

import * as deepFreeze from 'deep-freeze';
import { IndexName, indexReducer, IndexState } from './index.reducer';
import { AddToIndexAction, RemoveFromIndexByValueAction } from './index.actions';
class NullAction extends AddToIndexAction {
type = null;
payload = null;
constructor() {
super(null, null, null);
}
}
describe('requestReducer', () => {
const key1 = '567a639f-f5ff-4126-807c-b7d0910808c8';
const key2 = '1911e8a4-6939-490c-b58b-a5d70f8d91fb';
const val1 = 'https://dspace7.4science.it/dspace-spring-rest/api/core/items/567a639f-f5ff-4126-807c-b7d0910808c8';
const val2 = 'https://dspace7.4science.it/dspace-spring-rest/api/core/items/1911e8a4-6939-490c-b58b-a5d70f8d91fb';
const testState: IndexState = {
[IndexName.OBJECT]: {
[key1]: val1
},
[IndexName.REQUEST]: {
}
};
deepFreeze(testState);
it('should return the current state when no valid actions have been made', () => {
const action = new NullAction();
const newState = indexReducer(testState, action);
expect(newState).toEqual(testState);
});
it('should start with an empty state', () => {
const action = new NullAction();
const initialState = indexReducer(undefined, action);
expect(initialState).toEqual(Object.create(null));
});
it('should add the \'key\' with the corresponding \'value\' to the correct substate, in response to an ADD action', () => {
const state = testState;
const action = new AddToIndexAction(IndexName.REQUEST, key2, val2);
const newState = indexReducer(state, action);
expect(newState[IndexName.REQUEST][key2]).toEqual(val2);
});
it('should remove the given \'value\' from its corresponding \'key\' in the correct substate, in response to a REMOVE_BY_VALUE action', () => {
const state = testState;
const action = new RemoveFromIndexByValueAction(IndexName.OBJECT, val1);
const newState = indexReducer(state, action);
expect(newState[IndexName.OBJECT][key1]).toBeUndefined();
});
});