mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-02 05:52:14 +02:00
Further unit tests
This commit is contained in:
parent
f20b9fc59f
commit
97e8c04536
|
@ -855,7 +855,7 @@ class Choices {
|
||||||
* @return {Object} Class instance
|
* @return {Object} Class instance
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
setChoices(choices, value, label, replaceChoices = false) {
|
setChoices(choices = [], value = '', label = '', replaceChoices = false) {
|
||||||
if (
|
if (
|
||||||
!this.initialised ||
|
!this.initialised ||
|
||||||
!this.isSelectElement ||
|
!this.isSelectElement ||
|
||||||
|
@ -871,29 +871,30 @@ class Choices {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add choices if passed
|
// Add choices if passed
|
||||||
if (choices && choices.length) {
|
if (choices.length) {
|
||||||
this.containerOuter.removeLoadingState();
|
this.containerOuter.removeLoadingState();
|
||||||
|
const addGroupsAndChoices = (groupOrChoice) => {
|
||||||
choices.forEach((result) => {
|
if (groupOrChoice.choices) {
|
||||||
if (result.choices) {
|
|
||||||
this._addGroup(
|
this._addGroup(
|
||||||
result,
|
groupOrChoice,
|
||||||
(result.id || null),
|
(groupOrChoice.id || null),
|
||||||
value,
|
value,
|
||||||
label,
|
label,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
this._addChoice(
|
this._addChoice(
|
||||||
result[value],
|
groupOrChoice[value],
|
||||||
result[label],
|
groupOrChoice[label],
|
||||||
result.selected,
|
groupOrChoice.selected,
|
||||||
result.disabled,
|
groupOrChoice.disabled,
|
||||||
undefined,
|
undefined,
|
||||||
result.customProperties,
|
groupOrChoice.customProperties,
|
||||||
result.placeholder,
|
groupOrChoice.placeholder,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
choices.forEach(addGroupsAndChoices);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
@ -934,13 +935,11 @@ class Choices {
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
ajax(fn) {
|
ajax(fn) {
|
||||||
if (!this.initialised || !this.isSelectElement) {
|
if (!this.initialised || !this.isSelectElement || !fn) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show loading text
|
|
||||||
requestAnimationFrame(() => this._handleLoadingState(true));
|
requestAnimationFrame(() => this._handleLoadingState(true));
|
||||||
// Run callback
|
|
||||||
fn(this._ajaxCallback());
|
fn(this._ajaxCallback());
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -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('renderGroups', () => {});
|
||||||
describe('renderChoices', () => {});
|
describe('renderChoices', () => {});
|
||||||
describe('renderItems', () => {});
|
describe('renderItems', () => {});
|
||||||
|
@ -876,7 +1023,6 @@ describe('choices', () => {
|
||||||
describe('setValue', () => {});
|
describe('setValue', () => {});
|
||||||
describe('setValueByChoice', () => {});
|
describe('setValueByChoice', () => {});
|
||||||
describe('setChoices', () => {});
|
describe('setChoices', () => {});
|
||||||
describe('clearInput', () => {});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.skip('private methods', () => {
|
describe.skip('private methods', () => {
|
||||||
|
|
Loading…
Reference in a new issue