mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[add data] adds unit tests for create_multi_select_model
This commit is contained in:
parent
41dffa248e
commit
9851446b47
2 changed files with 85 additions and 4 deletions
|
@ -0,0 +1,74 @@
|
|||
import expect from 'expect.js';
|
||||
import sinon from 'sinon';
|
||||
import createMultiSelectModel from '../create_multi_select_model';
|
||||
|
||||
describe('createMultiSelectModel', function () {
|
||||
|
||||
it('should throw an error if the first argument is not an array', () => {
|
||||
expect(createMultiSelectModel).withArgs('foo', []).to.throwError();
|
||||
expect(createMultiSelectModel).withArgs(1234, []).to.throwError();
|
||||
expect(createMultiSelectModel).withArgs(undefined, []).to.throwError();
|
||||
expect(createMultiSelectModel).withArgs(null, []).to.throwError();
|
||||
expect(createMultiSelectModel).withArgs([], []).to.not.throwError();
|
||||
});
|
||||
|
||||
it('should throw an error if the second argument is not an array', () => {
|
||||
expect(createMultiSelectModel).withArgs([], 'foo').to.throwError();
|
||||
expect(createMultiSelectModel).withArgs([], 1234).to.throwError();
|
||||
expect(createMultiSelectModel).withArgs([], undefined).to.throwError();
|
||||
expect(createMultiSelectModel).withArgs([], null).to.throwError();
|
||||
expect(createMultiSelectModel).withArgs([], []).to.not.throwError();
|
||||
});
|
||||
|
||||
it('should output an array with an item for each passed in', () => {
|
||||
const items = [ 'foo', 'bar', 'baz' ];
|
||||
const expected = [
|
||||
{ title: 'foo', selected: false },
|
||||
{ title: 'bar', selected: false },
|
||||
{ title: 'baz', selected: false }
|
||||
];
|
||||
const actual = createMultiSelectModel(items, []);
|
||||
|
||||
expect(actual).to.eql(expected);
|
||||
});
|
||||
|
||||
it('should set the selected property in the output', () => {
|
||||
const items = [ 'foo', 'bar', 'baz' ];
|
||||
const selectedItems = [ 'bar', 'baz' ];
|
||||
const expected = [
|
||||
{ title: 'foo', selected: false },
|
||||
{ title: 'bar', selected: true },
|
||||
{ title: 'baz', selected: true }
|
||||
];
|
||||
const actual = createMultiSelectModel(items, selectedItems);
|
||||
|
||||
expect(actual).to.eql(expected);
|
||||
});
|
||||
|
||||
it('should trim values when comparing for selected', () => {
|
||||
const items = [ 'foo', 'bar', 'baz' ];
|
||||
const selectedItems = [ ' bar ', ' baz ' ];
|
||||
const expected = [
|
||||
{ title: 'foo', selected: false },
|
||||
{ title: 'bar', selected: true },
|
||||
{ title: 'baz', selected: true }
|
||||
];
|
||||
const actual = createMultiSelectModel(items, selectedItems);
|
||||
|
||||
expect(actual).to.eql(expected);
|
||||
});
|
||||
|
||||
it('should be case insensitive when comparing for selected', () => {
|
||||
const items = [ 'foo', 'bar', 'baz' ];
|
||||
const selectedItems = [ ' Bar ', ' BAZ ' ];
|
||||
const expected = [
|
||||
{ title: 'foo', selected: false },
|
||||
{ title: 'bar', selected: true },
|
||||
{ title: 'baz', selected: true }
|
||||
];
|
||||
const actual = createMultiSelectModel(items, selectedItems);
|
||||
|
||||
expect(actual).to.eql(expected);
|
||||
});
|
||||
|
||||
});
|
|
@ -1,9 +1,12 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
export default function selectableArray(array, selectedValues) {
|
||||
return array.map((item) => {
|
||||
const selected = _.find(selectedValues, (selectedValue) => {
|
||||
return selectedValue.toUpperCase() === item.toUpperCase();
|
||||
export default function selectableArray(items, selectedItems) {
|
||||
if (!_.isArray(items)) throw new Error('First argument must be an array');
|
||||
if (!_.isArray(selectedItems)) throw new Error('Second argument must be an array');
|
||||
|
||||
return items.map((item) => {
|
||||
const selected = _.find(selectedItems, (selectedItem) => {
|
||||
return cleanItem(selectedItem) === cleanItem(item);
|
||||
});
|
||||
|
||||
return {
|
||||
|
@ -12,3 +15,7 @@ export default function selectableArray(array, selectedValues) {
|
|||
};
|
||||
});
|
||||
};
|
||||
|
||||
function cleanItem(item) {
|
||||
return _.trim(item).toUpperCase();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue