electron/spec-main/api-callbacks-registry-spec.ts

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
import { expect } from 'chai';
import { CallbacksRegistry } from '../lib/renderer/remote/callbacks-registry';
import { ifdescribe } from './spec-helpers';
2017-11-16 05:04:33 +00:00
2020-03-20 20:28:31 +00:00
const features = process.electronBinding('features');
ifdescribe(features.isRemoteModuleEnabled())('CallbacksRegistry module', () => {
2020-03-20 20:28:31 +00:00
let registry: CallbacksRegistry;
2017-11-16 05:04:33 +00:00
beforeEach(() => {
2020-03-20 20:28:31 +00:00
registry = new CallbacksRegistry();
});
2017-11-16 05:04:33 +00:00
it('adds a callback to the registry', () => {
2020-03-20 20:28:31 +00:00
const cb = () => [1, 2, 3, 4, 5];
const key = registry.add(cb);
2017-11-16 18:39:07 +00:00
2020-03-20 20:28:31 +00:00
expect(key).to.exist('key');
});
2017-11-16 05:04:33 +00:00
it('returns a specified callback if it is in the registry', () => {
2020-03-20 20:28:31 +00:00
const cb = () => [1, 2, 3, 4, 5];
const key = registry.add(cb);
expect(key).to.exist('key');
const callback = registry.get(key!);
2017-11-16 05:04:33 +00:00
2020-03-20 20:28:31 +00:00
expect(callback.toString()).equal(cb.toString());
});
2017-11-16 05:04:33 +00:00
it('returns an empty function if the cb doesnt exist', () => {
2020-03-20 20:28:31 +00:00
const callback = registry.get(1);
2017-11-16 18:39:07 +00:00
2020-03-20 20:28:31 +00:00
expect(callback).to.be.a('function');
});
2017-11-16 05:04:33 +00:00
it('removes a callback to the registry', () => {
2020-03-20 20:28:31 +00:00
const cb = () => [1, 2, 3, 4, 5];
const key = registry.add(cb);
expect(key).to.exist('key');
2017-11-16 18:39:07 +00:00
2020-03-20 20:28:31 +00:00
registry.remove(key!);
const afterCB = registry.get(key!);
2017-11-16 18:39:07 +00:00
2020-03-20 20:28:31 +00:00
expect(afterCB).to.be.a('function');
expect(afterCB.toString()).to.not.equal(cb.toString());
});
});