mirror of
https://github.com/Choices-js/Choices.git
synced 2024-04-26 11:20:33 +02:00
Adding unit test and lint
This commit is contained in:
parent
beb07ebc2c
commit
76780ccc34
|
@ -46,6 +46,7 @@ import {
|
|||
isType,
|
||||
sortByScore,
|
||||
strToEl,
|
||||
parseCustomProperties,
|
||||
} from './lib/utils';
|
||||
import { defaultState } from './reducers';
|
||||
import Store from './store/store';
|
||||
|
@ -290,7 +291,9 @@ class Choices implements Choices {
|
|||
disabled: option.disabled || option.parentNode.disabled,
|
||||
placeholder:
|
||||
option.value === '' || option.hasAttribute('placeholder'),
|
||||
customProperties: this._parseCustomProperties(option.dataset['customProperties']),
|
||||
customProperties: parseCustomProperties(
|
||||
option.dataset.customProperties,
|
||||
),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -2429,17 +2432,6 @@ class Choices implements Choices {
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
_parseCustomProperties(customProperties): any {
|
||||
if(typeof customProperties !== 'undefined') {
|
||||
try {
|
||||
return JSON.parse(customProperties)
|
||||
} catch(e) {
|
||||
return customProperties
|
||||
}
|
||||
}
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
export default Choices;
|
||||
|
|
|
@ -15,6 +15,7 @@ import {
|
|||
sanitise,
|
||||
sortByAlpha,
|
||||
sortByScore,
|
||||
parseCustomProperties,
|
||||
} from './utils';
|
||||
|
||||
describe('utils', () => {
|
||||
|
@ -256,4 +257,25 @@ describe('utils', () => {
|
|||
expect(output).to.deep.equal(['baz']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('_parseCustomProperties', () => {
|
||||
describe('when custom properties are valid json', () => {
|
||||
it('returns the properties as object', () => {
|
||||
const customProperties = '{"description": "foo", "bar": "foo"}';
|
||||
const result = { description: 'foo', bar: 'foo' };
|
||||
|
||||
const value = parseCustomProperties(customProperties);
|
||||
expect(value).to.eql(result);
|
||||
});
|
||||
});
|
||||
describe('when custom properties are undefined', () => {
|
||||
it('returns an empty object', () => {
|
||||
const customProperties = undefined;
|
||||
const result = {};
|
||||
|
||||
const value = parseCustomProperties(customProperties);
|
||||
expect(value).to.eql(result);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -180,3 +180,15 @@ export const diff = (
|
|||
|
||||
return aKeys.filter((i) => bKeys.indexOf(i) < 0);
|
||||
};
|
||||
|
||||
export const parseCustomProperties = (customProperties): any => {
|
||||
if (typeof customProperties !== 'undefined') {
|
||||
try {
|
||||
return JSON.parse(customProperties);
|
||||
} catch (e) {
|
||||
return customProperties;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue