blob: aea6fae1ea2800a5cf91b2c9d702ad9c443872db [file] [log] [blame]
import {ContactsReducer, INITIAL_STATE, getData, getLoading, getLoaded, getFailed, reducer} from '@shared/store/reducers/contacts/contacts.reducer';
import * as contactsActions from '@shared/store/actions/contacts.action';
import { Contact } from '@shared/models';
describe('Contacts reducer', () => {
it('should return the initial state', () => {
const action = { type: 'NOOP' } as any;
const result = reducer(undefined, action);
expect(result).toBe(INITIAL_STATE);
});
it('should return initial state in case of missing action', () => {
const result = reducer(undefined, null);
expect(result).toBe(INITIAL_STATE);
});
it('should trigger loading state', () => {
const action = contactsActions.loadContacts();
const result = ContactsReducer(INITIAL_STATE, action);
expect(result).toEqual({
...INITIAL_STATE,
loading: true,
});
});
it('should trigger loaded state', () => {
const contacts: contactsActions.ILoadContactsSuccess = {payload: [new Contact()]};
contacts.payload[0].name = 'testme';
const action = contactsActions.loadContactsSuccess(contacts);
const result = ContactsReducer(INITIAL_STATE, action);
expect(result.loaded).toBe(true);
});
it('should trigger failed state', () => {
const error: contactsActions.ILoadContactsFail = {payload: 'err_msg'};
const action = contactsActions.loadContactsFail(error);
const result = ContactsReducer(INITIAL_STATE, action);
expect(result).toEqual({
...INITIAL_STATE,
failed: true,
});
});
it('getData return state.data', () => {
const state = { ...INITIAL_STATE };
const result = getData(state);
expect(result).toBe(state.data);
});
it('getLoading return state.loading', () => {
const state = { ...INITIAL_STATE };
const result = getLoading(state);
expect(result).toBe(state.loading);
});
it('getLoaded return state.loaded', () => {
const state = { ...INITIAL_STATE };
const result = getLoaded(state);
expect(result).toBe(state.loaded);
});
it('getFailed return state.failed', () => {
const state = { ...INITIAL_STATE };
const result = getFailed(state);
expect(result).toBe(state.failed);
});
});