2017-10-09 18:46:47 +02:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import items, { defaultState } from './items';
|
2019-12-23 19:22:54 +01:00
|
|
|
import { RemoveItemAction } from '../actions/items';
|
2017-10-09 18:46:47 +02:00
|
|
|
|
2017-10-29 19:56:24 +01:00
|
|
|
describe('reducers/items', () => {
|
2017-10-09 18:46:47 +02:00
|
|
|
it('should return same state when no action matches', () => {
|
2019-12-23 19:22:54 +01:00
|
|
|
expect(items(defaultState, {} as any)).to.equal(defaultState);
|
2017-10-09 18:46:47 +02:00
|
|
|
});
|
2017-10-10 12:40:59 +02:00
|
|
|
|
|
|
|
describe('when items do not exist', () => {
|
|
|
|
describe('ADD_ITEM', () => {
|
|
|
|
const value = 'Item one';
|
|
|
|
const label = 'Item one';
|
|
|
|
const id = 1234;
|
|
|
|
const choiceId = 5678;
|
|
|
|
const groupId = 1;
|
|
|
|
const customProperties = {
|
|
|
|
property: 'value',
|
|
|
|
};
|
2019-12-23 19:22:54 +01:00
|
|
|
const placeholder = true;
|
2017-10-10 12:40:59 +02:00
|
|
|
const keyCode = 10;
|
|
|
|
|
2017-12-10 19:00:57 +01:00
|
|
|
describe('passing expected values', () => {
|
|
|
|
let actualResponse;
|
2017-10-10 12:40:59 +02:00
|
|
|
|
2017-12-10 19:00:57 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
actualResponse = items(undefined, {
|
|
|
|
type: 'ADD_ITEM',
|
|
|
|
value,
|
|
|
|
label,
|
|
|
|
id,
|
|
|
|
choiceId,
|
|
|
|
groupId,
|
|
|
|
customProperties,
|
|
|
|
placeholder,
|
|
|
|
keyCode,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adds item', () => {
|
|
|
|
const expectedResponse = [
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
choiceId,
|
|
|
|
groupId,
|
|
|
|
value,
|
|
|
|
label,
|
|
|
|
active: true,
|
|
|
|
highlighted: false,
|
|
|
|
customProperties,
|
|
|
|
placeholder,
|
|
|
|
keyCode: null,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('unhighlights all highlighted items', () => {
|
2018-05-28 14:55:44 +02:00
|
|
|
actualResponse.forEach(item => {
|
2017-12-10 19:00:57 +01:00
|
|
|
expect(item.highlighted).to.equal(false);
|
|
|
|
});
|
|
|
|
});
|
2017-10-10 12:40:59 +02:00
|
|
|
});
|
|
|
|
|
2017-12-10 19:00:57 +01:00
|
|
|
describe('fallback values', () => {
|
|
|
|
describe('passing no placeholder value', () => {
|
|
|
|
it('adds item with placeholder set to false', () => {
|
|
|
|
const expectedResponse = [
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
choiceId,
|
|
|
|
groupId,
|
|
|
|
value,
|
|
|
|
label,
|
|
|
|
active: true,
|
|
|
|
highlighted: false,
|
|
|
|
customProperties,
|
|
|
|
placeholder: false,
|
|
|
|
keyCode: null,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const actualResponse = items(undefined, {
|
|
|
|
type: 'ADD_ITEM',
|
|
|
|
value,
|
|
|
|
label,
|
|
|
|
id,
|
|
|
|
choiceId,
|
|
|
|
groupId,
|
|
|
|
customProperties,
|
|
|
|
placeholder: undefined,
|
|
|
|
keyCode,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-10-10 12:40:59 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when items exist', () => {
|
|
|
|
let state;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
state = [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
choiceId: 1,
|
|
|
|
groupId: -1,
|
|
|
|
value: 'Item one',
|
|
|
|
label: 'Item one',
|
|
|
|
active: false,
|
|
|
|
highlighted: false,
|
|
|
|
customProperties: null,
|
|
|
|
placeholder: false,
|
|
|
|
keyCode: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
choiceId: 2,
|
|
|
|
groupId: -1,
|
|
|
|
value: 'Item one',
|
|
|
|
label: 'Item one',
|
|
|
|
active: true,
|
|
|
|
highlighted: false,
|
|
|
|
customProperties: null,
|
|
|
|
placeholder: false,
|
|
|
|
keyCode: null,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('REMOVE_ITEM', () => {
|
|
|
|
it('sets an item to be inactive based on passed ID', () => {
|
|
|
|
const clonedState = state.slice(0);
|
|
|
|
const id = 2;
|
|
|
|
const expectedResponse = [
|
|
|
|
{
|
|
|
|
...state[0],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...state[1],
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const actualResponse = items(clonedState, {
|
|
|
|
type: 'REMOVE_ITEM',
|
|
|
|
id,
|
2019-12-23 19:22:54 +01:00
|
|
|
} as RemoveItemAction);
|
2017-10-10 12:40:59 +02:00
|
|
|
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('HIGHLIGHT_ITEM', () => {
|
|
|
|
it('sets an item to be inactive based on passed ID', () => {
|
|
|
|
const clonedState = state.slice(0);
|
|
|
|
const id = 2;
|
|
|
|
const expectedResponse = [
|
|
|
|
{
|
|
|
|
...state[0],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...state[1],
|
|
|
|
highlighted: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const actualResponse = items(clonedState, {
|
|
|
|
type: 'HIGHLIGHT_ITEM',
|
|
|
|
id,
|
|
|
|
highlighted: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-10-09 18:46:47 +02:00
|
|
|
});
|