Browse Source

Add remove method to devices

pull/3725/head
Artur Arseniev 5 years ago
parent
commit
ebaa6205c6
  1. 12
      src/common/module.js
  2. 35
      src/device_manager/index.js
  3. 18
      test/specs/device_manager/index.js

12
src/common/module.js

@ -1,3 +1,4 @@
import { isString } from 'underscore';
import { createId } from 'utils/mixins';
export default {
@ -17,6 +18,17 @@ export default {
}, {});
},
__remove(model, opts = {}) {
const { em } = this;
const md = isString(model) ? this.get(model) : model;
const rm = () => {
md && this.all.remove(md, opts);
return md;
};
!opts.silent && em && em.trigger(this.events.removeBefore, md, rm, opts);
return !opts.abort && rm();
},
__catchAllEvent(event, model, coll, opts) {
const { em, events } = this;
const options = opts || coll;

35
src/device_manager/index.js

@ -67,8 +67,8 @@ export default () => {
update: evUpdate,
add: evAdd,
// addBefore: evAddBefore,
remove: evRemove
// removeBefore: evRemoveBefore
remove: evRemove,
removeBefore: evRemoveBefore
},
init(config = {}) {
@ -78,9 +78,11 @@ export default () => {
devices = new Devices();
(c.devices || []).forEach(dv => this.add(dv.id || dv.name, dv.width, dv));
devices.on('add', (m, c, o) => em.trigger(evAdd, m, o));
devices.on('all', this.__catchAllEvent, this);
this.all = devices;
devices
.on('add', (m, c, o) => em.trigger(evAdd, m, o))
.on('remove', (m, c, o) => em.trigger(evRemove, m, o))
.on('all', this.__catchAllEvent, this);
return this;
},
@ -145,24 +147,17 @@ export default () => {
},
/**
* Remove devie
* @param {String|[Device]} page Page or page id
* @returns {[Page]}
* Remove device
* @param {String|[Device]} device Device or device id
* @returns {[Device]} Removed device
* @example
* const removedPage = pageManager.remove('page-id');
* // or by passing the page
* const somePage = pageManager.get('page-id');
* pageManager.remove(somePage);
* const removed = deviceManager.remove('device-id');
* // or by passing the Device
* const device = deviceManager.get('device-id');
* deviceManager.remove(device);
*/
remove(page, opts = {}) {
// const { em } = this;
// const pg = isString(page) ? this.get(page) : page;
// const rm = () => {
// pg && this.pages.remove(pg, opts);
// return pg;
// };
// !opts.silent && em.trigger(evPageRemoveBefore, pg, rm, opts);
// return !opts.abort && rm();
remove(device, opts = {}) {
return this.__remove(device, opts);
},
/**

18
test/specs/device_manager/index.js

@ -103,6 +103,24 @@ describe('DeviceManager', () => {
expect(model).toBe(model2);
});
test('Remove device', () => {
const id = 'device';
const all = obj.getAll();
const model = obj.add({ id });
expect(all.length).toEqual(1);
const eventFn = jest.fn();
const eventFnAll = jest.fn();
em.on(obj.events.remove, eventFn);
em.on(obj.events.all, eventFnAll);
const removed = obj.remove(id);
expect(all.length).toEqual(0);
expect(model).toBe(removed);
// Check for events
expect(eventFn).toBeCalledTimes(1);
expect(eventFnAll).toBeCalled();
});
test('Render devices', () => {
expect(obj.render()).toBeTruthy();
});

Loading…
Cancel
Save