mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-19 22:25:09 +02:00
Further public method unit tests
This commit is contained in:
parent
1dc040d7f8
commit
fdbc2a890f
|
@ -541,24 +541,18 @@ class Choices {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const id = item.id;
|
const { id, groupId = -1, value = '', label = '' } = item;
|
||||||
const groupId = item.groupId;
|
|
||||||
const group = groupId >= 0 ? this.store.getGroupById(groupId) : null;
|
const group = groupId >= 0 ? this.store.getGroupById(groupId) : null;
|
||||||
|
|
||||||
this.store.dispatch(highlightItem(id, true));
|
this.store.dispatch(highlightItem(id, true));
|
||||||
|
|
||||||
if (runEvent) {
|
if (runEvent) {
|
||||||
const eventResponse = {
|
this.passedElement.triggerEvent(EVENTS.highlightItem, {
|
||||||
id,
|
id,
|
||||||
value: item.value,
|
value,
|
||||||
label: item.label,
|
label,
|
||||||
};
|
groupValue: group && group.value ? group.value : null,
|
||||||
|
});
|
||||||
if (group && group.value) {
|
|
||||||
eventResponse.groupValue = group.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.passedElement.triggerEvent(EVENTS.highlightItem, eventResponse);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
@ -575,21 +569,16 @@ class Choices {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const id = item.id;
|
const { id, groupId = -1, value = '', label = '' } = item;
|
||||||
const groupId = item.groupId;
|
|
||||||
const group = groupId >= 0 ? this.store.getGroupById(groupId) : null;
|
const group = groupId >= 0 ? this.store.getGroupById(groupId) : null;
|
||||||
const eventResponse = {
|
|
||||||
id,
|
|
||||||
value: item.value,
|
|
||||||
label: item.label,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (group && group.value) {
|
|
||||||
eventResponse.groupValue = group.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.store.dispatch(highlightItem(id, false));
|
this.store.dispatch(highlightItem(id, false));
|
||||||
this.passedElement.triggerEvent(EVENTS.highlightItem, eventResponse);
|
this.passedElement.triggerEvent(EVENTS.highlightItem, {
|
||||||
|
id,
|
||||||
|
value,
|
||||||
|
label,
|
||||||
|
groupValue: group && group.value ? group.value : null,
|
||||||
|
});
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { expect } from 'chai';
|
||||||
import { spy, stub } from 'sinon';
|
import { spy, stub } from 'sinon';
|
||||||
|
|
||||||
import Choices from './choices';
|
import Choices from './choices';
|
||||||
import { EVENTS } from './constants';
|
import { EVENTS, ACTION_TYPES } from './constants';
|
||||||
|
|
||||||
describe('choices', () => {
|
describe('choices', () => {
|
||||||
let instance;
|
let instance;
|
||||||
|
@ -517,14 +517,332 @@ describe('choices', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('highlightItem', () => {
|
||||||
|
let passedElementTriggerEventStub;
|
||||||
|
let storeDispatchSpy;
|
||||||
|
let storeGetGroupByIdStub;
|
||||||
|
const groupIdValue = 'Test';
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
passedElementTriggerEventStub = stub();
|
||||||
|
storeGetGroupByIdStub = stub().returns({
|
||||||
|
value: groupIdValue,
|
||||||
|
});
|
||||||
|
storeDispatchSpy = spy(instance.store, 'dispatch');
|
||||||
|
|
||||||
|
instance.store.getGroupById = storeGetGroupByIdStub;
|
||||||
|
instance.passedElement.triggerEvent = passedElementTriggerEventStub;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
storeDispatchSpy.restore();
|
||||||
|
instance.store.getGroupById.reset();
|
||||||
|
instance.passedElement.triggerEvent.reset();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('no item passed', () => {
|
||||||
|
let output;
|
||||||
|
beforeEach(() => {
|
||||||
|
output = instance.highlightItem();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns instance', () => {
|
||||||
|
expect(output).to.eql(instance);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns early', () => {
|
||||||
|
expect(passedElementTriggerEventStub.called).to.equal(false);
|
||||||
|
expect(storeDispatchSpy.called).to.equal(false);
|
||||||
|
expect(storeGetGroupByIdStub.called).to.equal(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('item passed', () => {
|
||||||
|
let output;
|
||||||
|
|
||||||
|
const item = {
|
||||||
|
id: 1234,
|
||||||
|
value: 'Test',
|
||||||
|
label: 'Test',
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('passing truthy second paremeter', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
output = instance.highlightItem(item, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns instance', () => {
|
||||||
|
expect(output).to.eql(instance);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('dispatches highlightItem action with correct arguments', () => {
|
||||||
|
expect(storeDispatchSpy.called).to.equal(true);
|
||||||
|
expect(storeDispatchSpy.lastCall.args[0]).to.eql({
|
||||||
|
type: ACTION_TYPES.HIGHLIGHT_ITEM,
|
||||||
|
id: item.id,
|
||||||
|
highlighted: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('item with negative groupId', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
item.groupId = -1;
|
||||||
|
output = instance.highlightItem(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('triggers event with null groupValue', () => {
|
||||||
|
expect(passedElementTriggerEventStub.called).to.equal(true);
|
||||||
|
expect(passedElementTriggerEventStub.lastCall.args[0]).to.equal(EVENTS.highlightItem);
|
||||||
|
expect(passedElementTriggerEventStub.lastCall.args[1]).to.eql({
|
||||||
|
id: item.id,
|
||||||
|
value: item.value,
|
||||||
|
label: item.label,
|
||||||
|
groupValue: null,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('item without groupId', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
item.groupId = 1;
|
||||||
|
output = instance.highlightItem(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('triggers event with groupValue', () => {
|
||||||
|
expect(passedElementTriggerEventStub.called).to.equal(true);
|
||||||
|
expect(passedElementTriggerEventStub.lastCall.args[0]).to.equal(EVENTS.highlightItem);
|
||||||
|
expect(passedElementTriggerEventStub.lastCall.args[1]).to.eql({
|
||||||
|
id: item.id,
|
||||||
|
value: item.value,
|
||||||
|
label: item.label,
|
||||||
|
groupValue: groupIdValue,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('passing falsey second paremeter', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
instance.highlightItem(item, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('doesn\'t trigger event', () => {
|
||||||
|
expect(passedElementTriggerEventStub.called).to.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns instance', () => {
|
||||||
|
expect(output).to.eql(instance);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('unhighlightItem', () => {
|
||||||
|
let passedElementTriggerEventStub;
|
||||||
|
let storeDispatchSpy;
|
||||||
|
let storeGetGroupByIdStub;
|
||||||
|
const groupIdValue = 'Test';
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
passedElementTriggerEventStub = stub();
|
||||||
|
storeGetGroupByIdStub = stub().returns({
|
||||||
|
value: groupIdValue,
|
||||||
|
});
|
||||||
|
storeDispatchSpy = spy(instance.store, 'dispatch');
|
||||||
|
|
||||||
|
instance.store.getGroupById = storeGetGroupByIdStub;
|
||||||
|
instance.passedElement.triggerEvent = passedElementTriggerEventStub;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
storeDispatchSpy.restore();
|
||||||
|
instance.store.getGroupById.reset();
|
||||||
|
instance.passedElement.triggerEvent.reset();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('no item passed', () => {
|
||||||
|
let output;
|
||||||
|
beforeEach(() => {
|
||||||
|
output = instance.unhighlightItem();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns instance', () => {
|
||||||
|
expect(output).to.eql(instance);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns early', () => {
|
||||||
|
expect(passedElementTriggerEventStub.called).to.equal(false);
|
||||||
|
expect(storeDispatchSpy.called).to.equal(false);
|
||||||
|
expect(storeGetGroupByIdStub.called).to.equal(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('item passed', () => {
|
||||||
|
let output;
|
||||||
|
|
||||||
|
const item = {
|
||||||
|
id: 1234,
|
||||||
|
value: 'Test',
|
||||||
|
label: 'Test',
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('passing truthy second paremeter', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
output = instance.unhighlightItem(item, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns instance', () => {
|
||||||
|
expect(output).to.eql(instance);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('dispatches highlightItem action with correct arguments', () => {
|
||||||
|
expect(storeDispatchSpy.called).to.equal(true);
|
||||||
|
expect(storeDispatchSpy.lastCall.args[0]).to.eql({
|
||||||
|
type: ACTION_TYPES.HIGHLIGHT_ITEM,
|
||||||
|
id: item.id,
|
||||||
|
highlighted: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('item with negative groupId', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
item.groupId = -1;
|
||||||
|
output = instance.unhighlightItem(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('triggers event with null groupValue', () => {
|
||||||
|
expect(passedElementTriggerEventStub.called).to.equal(true);
|
||||||
|
expect(passedElementTriggerEventStub.lastCall.args[0]).to.equal(EVENTS.highlightItem);
|
||||||
|
expect(passedElementTriggerEventStub.lastCall.args[1]).to.eql({
|
||||||
|
id: item.id,
|
||||||
|
value: item.value,
|
||||||
|
label: item.label,
|
||||||
|
groupValue: null,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('item without groupId', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
item.groupId = 1;
|
||||||
|
output = instance.highlightItem(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('triggers event with groupValue', () => {
|
||||||
|
expect(passedElementTriggerEventStub.called).to.equal(true);
|
||||||
|
expect(passedElementTriggerEventStub.lastCall.args[0]).to.equal(EVENTS.highlightItem);
|
||||||
|
expect(passedElementTriggerEventStub.lastCall.args[1]).to.eql({
|
||||||
|
id: item.id,
|
||||||
|
value: item.value,
|
||||||
|
label: item.label,
|
||||||
|
groupValue: groupIdValue,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('passing falsey second paremeter', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
instance.highlightItem(item, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('doesn\'t trigger event', () => {
|
||||||
|
expect(passedElementTriggerEventStub.called).to.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns instance', () => {
|
||||||
|
expect(output).to.eql(instance);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('highlightAll', () => {
|
||||||
|
let storeGetItemsStub;
|
||||||
|
let highlightItemSpy;
|
||||||
|
let output;
|
||||||
|
|
||||||
|
const items = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
value: 'Test 1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
value: 'Test 2',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
storeGetItemsStub = stub().returns(items);
|
||||||
|
highlightItemSpy = spy(instance, 'highlightItem');
|
||||||
|
|
||||||
|
instance.store.getItems = storeGetItemsStub;
|
||||||
|
|
||||||
|
output = instance.highlightAll();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
highlightItemSpy.restore();
|
||||||
|
instance.store.getItems.reset();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns instance', () => {
|
||||||
|
expect(output).to.eql(instance);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('highlights each item in store', () => {
|
||||||
|
expect(highlightItemSpy.callCount).to.equal(items.length);
|
||||||
|
expect(highlightItemSpy.firstCall.args[0]).to.equal(items[0]);
|
||||||
|
expect(highlightItemSpy.lastCall.args[0]).to.equal(items[1]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('unhighlightAll', () => {
|
||||||
|
let storeGetItemsStub;
|
||||||
|
let unhighlightItemSpy;
|
||||||
|
let output;
|
||||||
|
|
||||||
|
const items = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
value: 'Test 1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
value: 'Test 2',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
storeGetItemsStub = stub().returns(items);
|
||||||
|
unhighlightItemSpy = spy(instance, 'unhighlightItem');
|
||||||
|
|
||||||
|
instance.store.getItems = storeGetItemsStub;
|
||||||
|
|
||||||
|
output = instance.unhighlightAll();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
unhighlightItemSpy.restore();
|
||||||
|
instance.store.getItems.reset();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns instance', () => {
|
||||||
|
expect(output).to.eql(instance);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('unhighlights each item in store', () => {
|
||||||
|
expect(unhighlightItemSpy.callCount).to.equal(items.length);
|
||||||
|
expect(unhighlightItemSpy.firstCall.args[0]).to.equal(items[0]);
|
||||||
|
expect(unhighlightItemSpy.lastCall.args[0]).to.equal(items[1]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('renderGroups', () => {});
|
describe('renderGroups', () => {});
|
||||||
describe('renderChoices', () => {});
|
describe('renderChoices', () => {});
|
||||||
describe('renderItems', () => {});
|
describe('renderItems', () => {});
|
||||||
describe('render', () => {});
|
describe('render', () => {});
|
||||||
describe('highlightItem', () => {});
|
|
||||||
describe('unhighlightItem', () => {});
|
|
||||||
describe('highlightAll', () => {});
|
|
||||||
describe('unhighlightAll', () => {});
|
|
||||||
describe('removeItemsByValue', () => {});
|
describe('removeItemsByValue', () => {});
|
||||||
describe('removeActiveItems', () => {});
|
describe('removeActiveItems', () => {});
|
||||||
describe('removeHighlightedItems', () => {});
|
describe('removeHighlightedItems', () => {});
|
||||||
|
|
Loading…
Reference in a new issue