Further unit tests

This commit is contained in:
Josh Johnson 2017-11-07 14:04:37 +00:00
parent f20b9fc59f
commit 97e8c04536
2 changed files with 163 additions and 18 deletions

View file

@ -855,7 +855,7 @@ class Choices {
* @return {Object} Class instance
* @public
*/
setChoices(choices, value, label, replaceChoices = false) {
setChoices(choices = [], value = '', label = '', replaceChoices = false) {
if (
!this.initialised ||
!this.isSelectElement ||
@ -871,29 +871,30 @@ class Choices {
}
// Add choices if passed
if (choices && choices.length) {
if (choices.length) {
this.containerOuter.removeLoadingState();
choices.forEach((result) => {
if (result.choices) {
const addGroupsAndChoices = (groupOrChoice) => {
if (groupOrChoice.choices) {
this._addGroup(
result,
(result.id || null),
groupOrChoice,
(groupOrChoice.id || null),
value,
label,
);
} else {
this._addChoice(
result[value],
result[label],
result.selected,
result.disabled,
groupOrChoice[value],
groupOrChoice[label],
groupOrChoice.selected,
groupOrChoice.disabled,
undefined,
result.customProperties,
result.placeholder,
groupOrChoice.customProperties,
groupOrChoice.placeholder,
);
}
});
};
choices.forEach(addGroupsAndChoices);
}
return this;
@ -934,13 +935,11 @@ class Choices {
* @public
*/
ajax(fn) {
if (!this.initialised || !this.isSelectElement) {
if (!this.initialised || !this.isSelectElement || !fn) {
return this;
}
// Show loading text
requestAnimationFrame(() => this._handleLoadingState(true));
// Run callback
fn(this._ajaxCallback());
return this;

View file

@ -865,6 +865,153 @@ describe('choices', () => {
});
});
describe('clearInput', () => {
let output;
let inputClearSpy;
let storeDispatchStub;
beforeEach(() => {
inputClearSpy = spy(instance.input, 'clear');
storeDispatchStub = stub();
instance.store.dispatch = storeDispatchStub;
});
afterEach(() => {
inputClearSpy.restore();
instance.store.dispatch.reset();
});
it('returns instance', () => {
output = instance.clearInput();
expect(output).to.eql(instance);
});
describe('text element', () => {
beforeEach(() => {
instance.isSelectOneElement = false;
instance.isTextElement = false;
output = instance.clearInput();
});
it('clears input with correct arguments', () => {
expect(inputClearSpy.called).to.equal(true);
expect(inputClearSpy.lastCall.args[0]).to.equal(true);
});
});
describe('select element with search enabled', () => {
beforeEach(() => {
instance.isSelectOneElement = true;
instance.isTextElement = false;
instance.config.searchEnabled = true;
output = instance.clearInput();
});
it('clears input with correct arguments', () => {
expect(inputClearSpy.called).to.equal(true);
expect(inputClearSpy.lastCall.args[0]).to.equal(false);
});
it('resets search flag', () => {
expect(instance.isSearching).to.equal(false);
});
it('dispatches activateChoices action', () => {
expect(storeDispatchStub.called).to.equal(true);
expect(storeDispatchStub.lastCall.args[0]).to.eql({
type: ACTION_TYPES.ACTIVATE_CHOICES,
active: true,
});
});
});
});
describe('ajax', () => {
const callbackResponse = 'worked';
let output;
let handleLoadingStateStub;
let ajaxCallbackStub;
const unhappyPath = () => {
it('returns instance', () => {
expect(output).to.eql(instance);
});
it('returns early', () => {
expect(handleLoadingStateStub.called).to.equal(false);
expect(ajaxCallbackStub.called).to.equal(false);
});
};
beforeEach(() => {
handleLoadingStateStub = stub();
ajaxCallbackStub = stub().returns(callbackResponse);
instance._ajaxCallback = ajaxCallbackStub;
instance._handleLoadingState = handleLoadingStateStub;
});
afterEach(() => {
instance._ajaxCallback.reset();
instance._handleLoadingState.reset();
});
describe('not initialised', () => {
beforeEach(() => {
instance.initialised = false;
output = instance.ajax(() => {});
});
unhappyPath();
});
describe('text element', () => {
beforeEach(() => {
instance.isSelectElement = false;
output = instance.ajax(() => {});
});
unhappyPath();
});
describe('passing invalid function', () => {
beforeEach(() => {
output = instance.ajax(null);
});
unhappyPath();
});
describe('select element', () => {
let callback;
beforeEach(() => {
instance.initialised = true;
instance.isSelectElement = true;
ajaxCallbackStub = stub();
callback = stub();
output = instance.ajax(callback);
});
it('sets loading state', () => {
requestAnimationFrame(() => {
expect(handleLoadingStateStub.called).to.equal(true);
});
});
it('calls passed function with ajax callback', () => {
expect(callback.called).to.equal(true);
expect(callback.lastCall.args[0]).to.eql(callbackResponse);
});
it('returns instance', () => {
expect(output).to.eql(instance);
});
});
});
describe('renderGroups', () => {});
describe('renderChoices', () => {});
describe('renderItems', () => {});
@ -876,7 +1023,6 @@ describe('choices', () => {
describe('setValue', () => {});
describe('setValueByChoice', () => {});
describe('setChoices', () => {});
describe('clearInput', () => {});
});
describe.skip('private methods', () => {