adjust FE tests to using classes

This commit is contained in:
Felix Hamann 2019-06-03 13:26:24 +02:00
parent 3e48809f00
commit ade9667671
3 changed files with 94 additions and 62 deletions

View File

@ -1,8 +1,15 @@
import { App } from './app'; import { App } from './app';
import { Utility } from './core/utility';
@Utility({ selector: 'util1' })
class TestUtil1 { }
@Utility({ selector: 'util2' })
class TestUtil2 { }
const TEST_UTILS = [ const TEST_UTILS = [
{ name: 'util1' }, TestUtil1,
{ name: 'util2' }, TestUtil2,
]; ];
describe('App', () => { describe('App', () => {

View File

@ -59,6 +59,8 @@ export class UtilRegistry {
console.log('setting up util', { util }); console.log('setting up util', { util });
} }
let instances = [];
if (util) { if (util) {
const elements = this._findUtilElements(util, scope); const elements = this._findUtilElements(util, scope);
@ -78,10 +80,13 @@ export class UtilRegistry {
console.info('Got utility instance for utility "' + util.name + '"', { utilInstance }); console.info('Got utility instance for utility "' + util.name + '"', { utilInstance });
} }
this._activeUtilInstances.push(utilInstance); instances.push(utilInstance);
} }
}); });
} }
this._activeUtilInstances.push(...instances);
return instances;
} }
find(name) { find(name) {

View File

@ -1,14 +1,5 @@
import { UtilRegistry } from "./util-registry"; import { UtilRegistry } from './util-registry';
import { Utility } from '../../core/utility';
const TEST_UTILS = [{
name: 'util1',
selector: '#some-id',
setup: () => {},
}, {
name: 'util2',
selector: '[uw-util]',
setup: () => {},
}];
describe('UtilRegistry', () => { describe('UtilRegistry', () => {
let utilRegistry; let utilRegistry;
@ -23,23 +14,26 @@ describe('UtilRegistry', () => {
describe('register()', () => { describe('register()', () => {
it('should allow to add utilities', () => { it('should allow to add utilities', () => {
utilRegistry.register(TEST_UTILS[0]); let foundUtil = utilRegistry.find(TestUtil1.name);
expect(foundUtil).toBeFalsy();
const foundUtil = utilRegistry.find(TEST_UTILS[0].name); utilRegistry.register(TestUtil1);
expect(foundUtil).toEqual(TEST_UTILS[0]);
foundUtil = utilRegistry.find(TestUtil1.name);
expect(foundUtil).toEqual(TestUtil1);
}); });
}); });
describe('deregister()', () => { describe('deregister()', () => {
it('should remove util', () => { it('should remove util', () => {
// register util // register util
utilRegistry.register(TEST_UTILS[0]); utilRegistry.register(TestUtil1);
let foundUtil = utilRegistry.find('util1'); let foundUtil = utilRegistry.find(TestUtil1.name);
expect(foundUtil).toBeTruthy(); expect(foundUtil).toBeTruthy();
// deregister util // deregister util
utilRegistry.deregister(TEST_UTILS[0].name); utilRegistry.deregister(TestUtil1.name);
foundUtil = utilRegistry.find('util1'); foundUtil = utilRegistry.find(TestUtil1.name);
expect(foundUtil).toBeFalsy(); expect(foundUtil).toBeFalsy();
}); });
@ -49,55 +43,62 @@ describe('UtilRegistry', () => {
}); });
describe('setup()', () => { describe('setup()', () => {
it('should catch errors thrown by the utility', () => { it('should catch errors thrown by the utility', () => {
spyOn(TEST_UTILS[0], 'setup').and.throwError('some error');
expect(() => { expect(() => {
utilRegistry.setup(TEST_UTILS[0]); utilRegistry.setup(ThrowingUtil);
}).not.toThrow(); }).not.toThrow();
}); });
it('should pass the app instance', () => { describe('scope has no matching elements', () => {
const scope = document.createElement('div'); it('should not construct an instance', () => {
const utilElement = document.createElement('div'); const scope = document.createElement('div');
utilElement.id = 'some-id'; const instances = utilRegistry.setup(TestUtil1, scope);
scope.appendChild(utilElement); expect(instances.length).toBe(0);
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', () => { it('should use fallback scope', () => {
spyOn(TEST_UTILS[0], 'setup'); const instances = utilRegistry.setup(TestUtil1);
utilRegistry.setup(TEST_UTILS[0]); expect(instances.length).toBe(0);
expect(TEST_UTILS[0].setup).not.toHaveBeenCalled();
}); });
}); });
describe('given a scope', () => { describe('scope has matching elements', () => {
let scope; let testScope;
let utilElement1; let testElement1;
let utilElement2; let testElement2;
beforeEach(() => { beforeEach(() => {
scope = document.createElement('div'); testScope = document.createElement('div');
utilElement1 = document.createElement('div'); testElement1 = document.createElement('div');
utilElement2 = document.createElement('div'); testElement2 = document.createElement('div');
utilElement1.setAttribute('uw-util', ''); testElement1.classList.add('util1');
utilElement2.setAttribute('uw-util', ''); testElement2.classList.add('util1');
scope.appendChild(utilElement1); testScope.appendChild(testElement1);
scope.appendChild(utilElement2); testScope.appendChild(testElement2);
}); });
it('should call the utilities\' setup function for each matching element', () => { it('should construct a utility instance', () => {
spyOn(TEST_UTILS[1], 'setup'); const setupUtilities = utilRegistry.setup(TestUtil1, testScope);
utilRegistry.setup(TEST_UTILS[1], scope); expect(setupUtilities).toBeTruthy();
// 2 matching elements in scope expect(setupUtilities[0]).toBeTruthy();
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]); it('should construct an instance for each matching element', () => {
const setupUtilities = utilRegistry.setup(TestUtil1, testScope);
expect(setupUtilities).toBeTruthy();
expect(setupUtilities[0].element).toBe(testElement1);
expect(setupUtilities[1].element).toBe(testElement2);
});
it('should pass the app instance', () => {
const fakeApp = { };
utilRegistry.setApp(fakeApp);
const setupUtilities = utilRegistry.setup(TestUtil1, testScope);
expect(setupUtilities).toBeTruthy();
expect(setupUtilities[0].app).toBe(fakeApp);
expect(setupUtilities[1].app).toBe(fakeApp);
}); });
}); });
}); });
@ -105,22 +106,41 @@ describe('UtilRegistry', () => {
describe('setupAll()', () => { describe('setupAll()', () => {
it('should setup all the utilities', () => { it('should setup all the utilities', () => {
spyOn(utilRegistry, 'setup'); spyOn(utilRegistry, 'setup');
utilRegistry.register(TEST_UTILS[0]); utilRegistry.register(TestUtil1);
utilRegistry.register(TEST_UTILS[1]); utilRegistry.register(TestUtil2);
utilRegistry.setupAll(); utilRegistry.setupAll();
expect(utilRegistry.setup.calls.count()).toBe(2); expect(utilRegistry.setup.calls.count()).toBe(2);
expect(utilRegistry.setup.calls.argsFor(0)).toEqual([TEST_UTILS[0], undefined]); expect(utilRegistry.setup.calls.argsFor(0)).toEqual([TestUtil1, undefined]);
expect(utilRegistry.setup.calls.argsFor(1)).toEqual([TEST_UTILS[1], undefined]); expect(utilRegistry.setup.calls.argsFor(1)).toEqual([TestUtil2, undefined]);
}); });
it('should pass the given scope', () => { it('should pass the given scope', () => {
spyOn(utilRegistry, 'setup'); spyOn(utilRegistry, 'setup');
utilRegistry.register(TEST_UTILS[0]); utilRegistry.register(TestUtil1);
const scope = document.createElement('div'); const scope = document.createElement('div');
utilRegistry.setupAll(scope); utilRegistry.setupAll(scope);
expect(utilRegistry.setup).toHaveBeenCalledWith(TEST_UTILS[0], scope); expect(utilRegistry.setup).toHaveBeenCalledWith(TestUtil1, scope);
}); });
}); });
}); });
// test utilities
@Utility({ selector: '.util1' })
class TestUtil1 {
constructor(element, app) {
this.element = element;
this.app = app;
}
}
@Utility({ selector: '#util2' })
class TestUtil2 { }
@Utility({ selector: '#throws' })
class ThrowingUtil {
constructor() {
throw new Error();
}
}