Browse Source

Improve SelectorManager APIs

pull/3905/head
Artur Arseniev 4 years ago
parent
commit
dff20e7153
  1. 18
      src/selector_manager/index.js
  2. 36
      test/specs/selector_manager/index.js

18
src/selector_manager/index.js

@ -231,13 +231,17 @@ export default () => {
},
/**
* Add a new selector to collection if it's not already exists. Class type is a default one
* @param {Object} props Selector properties, eg. `{ name: 'my-class', label: 'My class' }`
* Add a new selector to collection if it's not already exists.
* You can pass selectors properties or string identifiers.
* @param {Object|String} props Selector properties or string identifiers, eg. `{ name: 'my-class', label: 'My class' }`, `.my-cls`
* @param {Object} [opts] Selector options
* @return {[Selector]}
* @example
* const selector = selectorManager.add({ name: 'my-class', label: 'My class' });
* console.log(selector.toString()) // `.my-class`
* // Same as
* const selector = selectorManager.add('.my-class');
* console.log(selector.toString()) // `.my-class`
* */
add(props, opts = {}) {
const cOpts = isString(props) ? {} : opts;
@ -273,13 +277,12 @@ export default () => {
/**
* Get the selector by its name/type
* @param {String} name Selector name
* @param {Number} [type=1] Selector type, default is class
* @return {[Selector]|null}
* @param {String} name Selector name or string identifier
* @returns {[Selector]|null}
* @example
* const selector = selectorManager.get('my-class');
* const selector = selectorManager.get('.my-class');
* // Get Id
* const selectorId = selectorManager.get('my-id', 2);
* const selectorId = selectorManager.get('#my-id');
* */
get(name, type) {
// Keep support for arrays but avoid it in docs
@ -321,6 +324,7 @@ export default () => {
* Return escaped selector name
* @param {String} name Selector name to escape
* @returns {String} Escaped name
* @private
*/
escapeName(name) {
const { escapeName } = this.getConfig();

36
test/specs/selector_manager/index.js

@ -151,6 +151,42 @@ describe('SelectorManager', () => {
expect(obj.get(name, type)).toBe(added);
});
test('Add selectors as string identifiers', () => {
const cls = '.my-class';
const id = '#my-id';
const addedCls = obj.add(cls);
const addedId = obj.add(id);
expect(addedCls.toString()).toBe(cls);
expect(addedId.toString()).toBe(id);
});
test('Get selectors as string identifiers', () => {
const cls = '.my-class';
const id = '#my-id';
const addedCls = obj.add(cls);
const addedId = obj.add(id);
expect(obj.get(cls)).toBe(addedCls);
expect(obj.get(id)).toBe(addedId);
});
test('Remove selectors as string identifiers', () => {
const cls = '.my-class';
const id = '#my-id';
const addedCls = obj.add(cls);
const addedId = obj.add(id);
expect(obj.getAll().length).toBe(2);
expect(obj.remove(cls)).toBe(addedCls);
expect(obj.remove(id)).toBe(addedId);
expect(obj.getAll().length).toBe(0);
});
test('Remove selector as instance', () => {
const addedCls = obj.add('.my-class');
expect(obj.getAll().length).toBe(1);
expect(obj.remove(addedCls)).toBe(addedCls);
expect(obj.getAll().length).toBe(0);
});
test('addClass single class string', () => {
const result = obj.addClass('class1');
expect(result.length).toEqual(1);

Loading…
Cancel
Save