Choices/src/scripts/store/store.test.ts

258 lines
7 KiB
TypeScript
Raw Normal View History

2017-10-10 13:06:00 +02:00
import { expect } from 'chai';
import sinon from 'sinon';
import Store from './store';
2017-10-29 19:56:24 +01:00
describe('reducers/store', () => {
2017-10-10 13:06:00 +02:00
let instance;
let subscribeStub;
let dispatchStub;
let getStateStub;
beforeEach(() => {
instance = new Store();
2018-05-24 10:22:07 +02:00
subscribeStub = sinon.stub(instance._store, 'subscribe');
dispatchStub = sinon.stub(instance._store, 'dispatch');
getStateStub = sinon.stub(instance._store, 'getState');
2017-10-10 13:06:00 +02:00
});
afterEach(() => {
subscribeStub.restore();
dispatchStub.restore();
getStateStub.restore();
});
2017-12-10 19:00:57 +01:00
describe('constructor', () => {
it('creates redux store', () => {
2018-05-24 10:22:07 +02:00
expect(instance._store).to.contain.keys([
2017-12-10 19:00:57 +01:00
'subscribe',
'dispatch',
'getState',
]);
});
2017-10-10 13:06:00 +02:00
});
describe('subscribe', () => {
it('wraps redux subscribe method', () => {
const onChange = (): void => {};
2017-10-10 13:06:00 +02:00
expect(subscribeStub.callCount).to.equal(0);
instance.subscribe(onChange);
expect(subscribeStub.callCount).to.equal(1);
expect(subscribeStub.firstCall.args[0]).to.equal(onChange);
});
});
describe('dispatch', () => {
it('wraps redux dispatch method', () => {
const action = 'TEST_ACTION';
expect(dispatchStub.callCount).to.equal(0);
instance.dispatch(action);
expect(dispatchStub.callCount).to.equal(1);
expect(dispatchStub.firstCall.args[0]).to.equal(action);
});
});
2018-04-24 13:54:45 +02:00
describe('state getter', () => {
it('returns state', () => {
2018-04-24 14:23:26 +02:00
const state = { items: [] };
2018-04-24 13:54:45 +02:00
getStateStub.returns(state);
expect(instance.state).to.equal(state);
2017-10-10 13:06:00 +02:00
});
});
describe('store selectors', () => {
let state;
beforeEach(() => {
state = {
items: [
2017-10-10 13:35:05 +02:00
{
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,
2017-12-10 19:00:57 +01:00
value: 'Item two',
label: 'Item two',
2017-10-10 13:35:05 +02:00
active: true,
highlighted: false,
customProperties: null,
placeholder: false,
keyCode: null,
},
2017-12-10 19:00:57 +01:00
{
id: 3,
choiceId: 3,
groupId: -1,
value: 'Item three',
label: 'Item three',
active: true,
highlighted: true,
customProperties: null,
placeholder: false,
keyCode: null,
},
2017-10-10 13:06:00 +02:00
],
choices: [
2017-10-10 13:35:05 +02:00
{
id: 1,
elementId: 'choices-test-1',
groupId: -1,
value: 'Choice 1',
label: 'Choice 1',
disabled: false,
selected: false,
active: true,
score: 9999,
customProperties: null,
placeholder: false,
keyCode: null,
},
{
id: 2,
elementId: 'choices-test-2',
groupId: -1,
value: 'Choice 2',
label: 'Choice 2',
disabled: false,
selected: true,
active: false,
score: 9999,
customProperties: null,
placeholder: false,
keyCode: null,
},
2017-10-10 13:06:00 +02:00
],
groups: [
2017-10-10 13:35:05 +02:00
{
id: 1,
value: 'Group one',
active: true,
disabled: false,
},
{
id: 2,
value: 'Group two',
active: true,
disabled: false,
},
2017-10-10 13:06:00 +02:00
],
};
getStateStub.returns(state);
});
2018-04-24 13:54:45 +02:00
describe('items getter', () => {
2017-10-10 13:06:00 +02:00
it('returns items', () => {
const expectedResponse = state.items;
2018-04-24 13:54:45 +02:00
expect(instance.items).to.eql(expectedResponse);
2017-10-10 13:06:00 +02:00
});
});
2018-04-24 14:52:13 +02:00
describe('activeItems getter', () => {
2017-10-10 13:35:05 +02:00
it('returns items that are active', () => {
const expectedResponse = state.items.filter((item) => item.active);
2018-04-24 14:52:13 +02:00
expect(instance.activeItems).to.eql(expectedResponse);
2017-10-10 13:35:05 +02:00
});
});
2018-04-24 14:52:13 +02:00
describe('highlightedActiveItems getter', () => {
2017-12-10 19:00:57 +01:00
it('returns items that are active and highlighted', () => {
const expectedResponse = state.items.filter(
(item) => item.highlighted && item.active,
);
2018-04-24 14:52:13 +02:00
expect(instance.highlightedActiveItems).to.eql(expectedResponse);
2017-12-10 19:00:57 +01:00
});
});
2018-04-24 14:23:26 +02:00
describe('choices getter', () => {
2017-10-10 13:06:00 +02:00
it('returns choices', () => {
const expectedResponse = state.choices;
2018-04-24 14:23:26 +02:00
expect(instance.choices).to.eql(expectedResponse);
2017-10-10 13:06:00 +02:00
});
});
2018-04-24 14:52:13 +02:00
describe('activeChoices getter', () => {
2017-10-10 13:35:05 +02:00
it('returns choices that are active', () => {
const expectedResponse = state.choices.filter(
(choice) => choice.active,
);
2018-04-24 14:52:13 +02:00
expect(instance.activeChoices).to.eql(expectedResponse);
2017-10-10 13:35:05 +02:00
});
});
2018-04-24 14:52:13 +02:00
describe('selectableChoices getter', () => {
2017-10-10 13:35:05 +02:00
it('returns choices that are not disabled', () => {
const expectedResponse = state.choices.filter(
(choice) => !choice.disabled,
);
2018-04-24 14:52:13 +02:00
expect(instance.selectableChoices).to.eql(expectedResponse);
2017-10-10 13:35:05 +02:00
});
});
2018-04-24 14:49:57 +02:00
describe('searchableChoices getter', () => {
2017-10-10 13:35:05 +02:00
it('returns choices that are not placeholders and are selectable', () => {
const expectedResponse = state.choices.filter(
(choice) => !choice.disabled && !choice.placeholder,
);
2018-04-24 14:49:57 +02:00
expect(instance.searchableChoices).to.eql(expectedResponse);
2017-10-10 13:35:05 +02:00
});
});
describe('getChoiceById', () => {
2017-12-10 19:00:57 +01:00
describe('passing id', () => {
it('returns active choice by passed id', () => {
const id = '1';
const expectedResponse = state.choices.find(
(choice) => choice.id === parseInt(id, 10),
);
2017-12-10 19:00:57 +01:00
const actualResponse = instance.getChoiceById(id);
expect(actualResponse).to.eql(expectedResponse);
});
});
2017-10-10 13:35:05 +02:00
});
2018-04-24 14:49:57 +02:00
describe('placeholderChoice getter', () => {
2017-10-10 13:35:05 +02:00
it('returns placeholder choice', () => {
const expectedResponse = state.choices
.reverse()
.find((choice) => choice.placeholder);
2018-04-24 14:49:57 +02:00
expect(instance.getPlaceholderChoice).to.eql(expectedResponse);
2017-10-10 13:35:05 +02:00
});
});
2017-10-10 13:06:00 +02:00
2018-04-24 14:49:57 +02:00
describe('groups getter', () => {
2017-10-10 13:06:00 +02:00
it('returns groups', () => {
const expectedResponse = state.groups;
2018-04-24 14:49:57 +02:00
expect(instance.groups).to.eql(expectedResponse);
2017-10-10 13:06:00 +02:00
});
});
2018-04-24 14:52:13 +02:00
describe('activeGroups getter', () => {
2017-10-10 13:35:05 +02:00
it('returns active groups', () => {
const expectedResponse = state.groups.filter((group) => group.active);
2018-04-24 14:52:13 +02:00
expect(instance.activeGroups).to.eql(expectedResponse);
2017-10-10 13:35:05 +02:00
});
});
describe('getGroupById', () => {
it('returns group by id', () => {
const id = 1;
const expectedResponse = state.groups.find((group) => group.id === id);
2017-10-10 13:35:05 +02:00
const actualResponse = instance.getGroupById(id);
expect(actualResponse).to.eql(expectedResponse);
});
});
2017-10-10 13:06:00 +02:00
});
});