mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-08 00:26:34 +02:00
Adding unit test and lint
This commit is contained in:
parent
beb07ebc2c
commit
76780ccc34
|
@ -46,6 +46,7 @@ import {
|
||||||
isType,
|
isType,
|
||||||
sortByScore,
|
sortByScore,
|
||||||
strToEl,
|
strToEl,
|
||||||
|
parseCustomProperties,
|
||||||
} from './lib/utils';
|
} from './lib/utils';
|
||||||
import { defaultState } from './reducers';
|
import { defaultState } from './reducers';
|
||||||
import Store from './store/store';
|
import Store from './store/store';
|
||||||
|
@ -290,7 +291,9 @@ class Choices implements Choices {
|
||||||
disabled: option.disabled || option.parentNode.disabled,
|
disabled: option.disabled || option.parentNode.disabled,
|
||||||
placeholder:
|
placeholder:
|
||||||
option.value === '' || option.hasAttribute('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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_parseCustomProperties(customProperties): any {
|
|
||||||
if(typeof customProperties !== 'undefined') {
|
|
||||||
try {
|
|
||||||
return JSON.parse(customProperties)
|
|
||||||
} catch(e) {
|
|
||||||
return customProperties
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Choices;
|
export default Choices;
|
||||||
|
|
|
@ -15,6 +15,7 @@ import {
|
||||||
sanitise,
|
sanitise,
|
||||||
sortByAlpha,
|
sortByAlpha,
|
||||||
sortByScore,
|
sortByScore,
|
||||||
|
parseCustomProperties,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
|
||||||
describe('utils', () => {
|
describe('utils', () => {
|
||||||
|
@ -256,4 +257,25 @@ describe('utils', () => {
|
||||||
expect(output).to.deep.equal(['baz']);
|
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);
|
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