fradrive/frontend/src/services/util-registry/util-registry.spec.js
2019-05-26 20:57:45 +02:00

133 lines
4.0 KiB
JavaScript

import { UtilRegistry } from "./util-registry";
const TEST_UTILS = [{
name: 'util1',
selector: '#some-id',
setup: () => {},
}, {
name: 'util2',
selector: '[uw-util]',
setup: () => {},
}];
describe('UtilRegistry', () => {
let utilRegistry;
beforeEach(() => {
utilRegistry = new UtilRegistry();
});
it('should create', () => {
expect(utilRegistry).toBeTruthy();
});
it('should setup all utlites when page is done loading', () => {
spyOn(utilRegistry, 'setupAll');
document.dispatchEvent(new Event('DOMContentLoaded'));
expect(utilRegistry.setupAll).toHaveBeenCalled();
});
describe('register()', () => {
it('should allow to add utilities', () => {
utilRegistry.register(TEST_UTILS[0]);
const foundUtil = utilRegistry.find(TEST_UTILS[0].name);
expect(foundUtil).toEqual(TEST_UTILS[0]);
});
});
describe('deregister()', () => {
it('should remove util', () => {
// register util
utilRegistry.register(TEST_UTILS[0]);
let foundUtil = utilRegistry.find('util1');
expect(foundUtil).toBeTruthy();
// deregister util
utilRegistry.deregister(TEST_UTILS[0].name);
foundUtil = utilRegistry.find('util1');
expect(foundUtil).toBeFalsy();
});
it('should destroy util instances if requested', () => {
pending('TBD');
});
});
describe('setup()', () => {
it('should catch errors thrown by the utility', () => {
spyOn(TEST_UTILS[0], 'setup').and.throwError('some error');
expect(() => {
utilRegistry.setup(TEST_UTILS[0]);
}).not.toThrow();
});
it('should pass the app instance', () => {
const scope = document.createElement('div');
const utilElement = document.createElement('div');
utilElement.id = 'some-id';
scope.appendChild(utilElement);
const fakeApp = { fn: () => {} };
utilRegistry.setApp(fakeApp);
spyOn(TEST_UTILS[0], 'setup');
utilRegistry.setup(TEST_UTILS[0], scope);
expect(TEST_UTILS[0].setup).toHaveBeenCalledWith(utilElement, fakeApp);
});
describe('given no scope', () => {
it('should use fallback scope', () => {
spyOn(TEST_UTILS[0], 'setup');
utilRegistry.setup(TEST_UTILS[0]);
expect(TEST_UTILS[0].setup).not.toHaveBeenCalled();
});
});
describe('given a scope', () => {
let scope;
let utilElement1;
let utilElement2;
beforeEach(() => {
scope = document.createElement('div');
utilElement1 = document.createElement('div');
utilElement2 = document.createElement('div');
utilElement1.setAttribute('uw-util', '');
utilElement2.setAttribute('uw-util', '');
scope.appendChild(utilElement1);
scope.appendChild(utilElement2);
});
it('should call the utilities\' setup function for each matching element', () => {
spyOn(TEST_UTILS[1], 'setup');
utilRegistry.setup(TEST_UTILS[1], scope);
// 2 matching elements in scope
expect(TEST_UTILS[1].setup.calls.count()).toBe(2);
expect(TEST_UTILS[1].setup.calls.argsFor(0)).toEqual([utilElement1, undefined]);
expect(TEST_UTILS[1].setup.calls.argsFor(1)).toEqual([utilElement2, undefined]);
});
});
});
describe('setupAll()', () => {
it('should setup all the utilities', () => {
spyOn(utilRegistry, 'setup');
utilRegistry.register(TEST_UTILS[0]);
utilRegistry.register(TEST_UTILS[1]);
utilRegistry.setupAll();
expect(utilRegistry.setup.calls.count()).toBe(2);
expect(utilRegistry.setup.calls.argsFor(0)).toEqual([TEST_UTILS[0], undefined]);
expect(utilRegistry.setup.calls.argsFor(1)).toEqual([TEST_UTILS[1], undefined]);
});
it('should pass the given scope', () => {
spyOn(utilRegistry, 'setup');
utilRegistry.register(TEST_UTILS[0]);
const scope = document.createElement('div');
utilRegistry.setupAll(scope);
expect(utilRegistry.setup).toHaveBeenCalledWith(TEST_UTILS[0], scope);
});
});
});