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();
|
|
|
|
subscribeStub = sinon.stub(instance.store, 'subscribe');
|
|
|
|
dispatchStub = sinon.stub(instance.store, 'dispatch');
|
|
|
|
getStateStub = sinon.stub(instance.store, 'getState');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
subscribeStub.restore();
|
|
|
|
dispatchStub.restore();
|
|
|
|
getStateStub.restore();
|
|
|
|
});
|
|
|
|
|
2017-12-10 19:00:57 +01:00
|
|
|
|
|
|
|
describe('constructor', () => {
|
|
|
|
it('creates redux store', () => {
|
|
|
|
expect(instance.store).to.contain.keys([
|
|
|
|
'subscribe',
|
|
|
|
'dispatch',
|
|
|
|
'getState',
|
|
|
|
]);
|
|
|
|
});
|
2017-10-10 13:06:00 +02:00
|
|
|
});
|
|
|
|
|
2017-12-10 19:00:57 +01:00
|
|
|
|
2017-10-10 13:06:00 +02:00
|
|
|
describe('subscribe', () => {
|
|
|
|
it('wraps redux subscribe method', () => {
|
|
|
|
const onChange = () => {};
|
|
|
|
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', () => {
|
|
|
|
const state = {
|
|
|
|
items: [],
|
|
|
|
};
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-10 13:35:05 +02:00
|
|
|
describe('getItemsFilteredByActive', () => {
|
|
|
|
it('returns items that are active', () => {
|
|
|
|
const expectedResponse = state.items.filter((item => item.active));
|
|
|
|
const actualResponse = instance.getItemsFilteredByActive();
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-10 19:00:57 +01:00
|
|
|
describe('getItemsFilteredByHighlighted', () => {
|
|
|
|
it('returns items that are active and highlighted', () => {
|
|
|
|
const expectedResponse = state.items.filter((item => item.highlighted && item.active));
|
|
|
|
const actualResponse = instance.getItemsFilteredByHighlighted();
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-10 13:06:00 +02:00
|
|
|
describe('getChoices', () => {
|
|
|
|
it('returns choices', () => {
|
|
|
|
const expectedResponse = state.choices;
|
|
|
|
const actualResponse = instance.getChoices();
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-10 13:35:05 +02:00
|
|
|
describe('getChoicesFilteredByActive', () => {
|
|
|
|
it('returns choices that are active', () => {
|
|
|
|
const expectedResponse = state.choices.filter((choice => choice.active));
|
|
|
|
const actualResponse = instance.getChoicesFilteredByActive();
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getChoicesFilteredBySelectable', () => {
|
|
|
|
it('returns choices that are not disabled', () => {
|
|
|
|
const expectedResponse = state.choices.filter((choice => !choice.disabled));
|
|
|
|
const actualResponse = instance.getChoicesFilteredBySelectable();
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getSearchableChoices', () => {
|
|
|
|
it('returns choices that are not placeholders and are selectable', () => {
|
|
|
|
const expectedResponse = state.choices.filter((choice => !choice.disabled && !choice.placeholder));
|
|
|
|
const actualResponse = instance.getSearchableChoices();
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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)));
|
|
|
|
const actualResponse = instance.getChoiceById(id);
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('passing no id', () => {
|
|
|
|
it('returns false', () => {
|
|
|
|
const actualResponse = instance.getChoiceById();
|
|
|
|
expect(actualResponse).to.equal(false);
|
|
|
|
});
|
2017-10-10 13:35:05 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getPlaceholderChoice', () => {
|
|
|
|
it('returns placeholder choice', () => {
|
|
|
|
const expectedResponse = state.choices.reverse().find(choice => choice.placeholder);
|
|
|
|
const actualResponse = instance.getPlaceholderChoice();
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
2017-10-10 13:06:00 +02:00
|
|
|
|
|
|
|
describe('getGroups', () => {
|
|
|
|
it('returns groups', () => {
|
|
|
|
const expectedResponse = state.groups;
|
|
|
|
const actualResponse = instance.getGroups();
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-10 13:35:05 +02:00
|
|
|
describe('getGroupsFilteredByActive', () => {
|
|
|
|
it('returns active groups', () => {
|
|
|
|
const expectedResponse = state.groups.filter(group => group.active);
|
|
|
|
const actualResponse = instance.getGroupsFilteredByActive();
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getGroupById', () => {
|
|
|
|
it('returns group by id', () => {
|
|
|
|
const id = '1';
|
|
|
|
const expectedResponse = state.groups.find((group => group.id === parseInt(id, 10)));
|
|
|
|
const actualResponse = instance.getGroupById(id);
|
|
|
|
expect(actualResponse).to.eql(expectedResponse);
|
|
|
|
});
|
|
|
|
});
|
2017-10-10 13:06:00 +02:00
|
|
|
});
|
|
|
|
});
|