chore(frontend-tests): fix environment specifications

This commit is contained in:
Gregor Kleen 2019-12-16 14:41:56 +01:00
parent e371412db4
commit cb75f4e4f1
2 changed files with 37 additions and 27 deletions

View File

@ -1,3 +1,5 @@
/* eslint-env node */
import { App } from './app';
import { Utility } from './core/utility';
@ -13,52 +15,50 @@ const TEST_UTILS = [
];
describe('App', () => {
let app;
beforeEach(() => {
app = new App();
global.App = new App();
});
it('should create', () => {
expect(app).toBeTruthy();
expect(global.App).toBeTruthy();
});
it('should setup all utlites when page is done loading', () => {
spyOn(app.utilRegistry, 'setupAll');
spyOn(global.App.utilRegistry, 'setupAll');
document.dispatchEvent(new Event('DOMContentLoaded'));
expect(app.utilRegistry.setupAll).toHaveBeenCalled();
expect(global.App.utilRegistry.setupAll).toHaveBeenCalled();
});
describe('provides services', () => {
it('HttpClient as httpClient', () => {
expect(app.httpClient).toBeTruthy();
expect(global.App.httpClient).toBeTruthy();
});
it('HtmlHelpers as htmlHelpers', () => {
expect(app.htmlHelpers).toBeTruthy();
expect(global.App.htmlHelpers).toBeTruthy();
});
it('I18n as i18n', () => {
expect(app.i18n).toBeTruthy();
expect(global.App.i18n).toBeTruthy();
});
it('UtilRegistry as utilRegistry', () => {
expect(app.utilRegistry).toBeTruthy();
expect(global.App.utilRegistry).toBeTruthy();
});
});
describe('registerUtilities()', () => {
it('should register the given utilities', () => {
spyOn(app.utilRegistry, 'register');
app.registerUtilities(TEST_UTILS);
expect(app.utilRegistry.register.calls.count()).toBe(TEST_UTILS.length);
expect(app.utilRegistry.register.calls.argsFor(0)).toEqual([TEST_UTILS[0]]);
expect(app.utilRegistry.register.calls.argsFor(1)).toEqual([TEST_UTILS[1]]);
spyOn(global.App.utilRegistry, 'register');
global.App.registerUtilities(TEST_UTILS);
expect(global.App.utilRegistry.register.calls.count()).toBe(TEST_UTILS.length);
expect(global.App.utilRegistry.register.calls.argsFor(0)).toEqual([TEST_UTILS[0]]);
expect(global.App.utilRegistry.register.calls.argsFor(1)).toEqual([TEST_UTILS[1]]);
});
it('should throw an error if not passed an array of utilities', () => {
expect(() => {
app.registerUtilities({});
global.App.registerUtilities({});
}).toThrow();
});
});

View File

@ -1,3 +1,5 @@
/* global global:writable */
import * as semver from 'semver';
export const LOCATION = {
@ -12,6 +14,7 @@ export class StorageManager {
namespace;
version;
_options;
_global;
constructor(namespace, version, options) {
this.namespace = namespace;
@ -27,6 +30,13 @@ export class StorageManager {
if (options !== undefined) {
this._options = options;
}
if (global !== undefined)
this._global = global;
else if (window !== undefined)
this._global = window;
else
throw new Error('Cannot setup StorageManager without window or global');
}
save(key, value, options=this._options) {
@ -178,36 +188,36 @@ export class StorageManager {
_getFromWindow() {
if (!window || !window.App)
if (!this._global || !this._global.App)
return {};
if (!window.App.Storage)
window.App.Storage = {};
if (!this._global.App.Storage)
this._global.App.Storage = {};
return window.App.Storage[this.namespace] || {};
return this._global.App.Storage[this.namespace] || {};
}
_saveToWindow(value) {
if (!window || !window.App) {
if (!this._global || !this._global.App) {
throw new Error('StorageManager._saveToWindow called when window.App is not available');
}
if (!value)
return this._clearWindow();
if (!window.App.Storage)
window.App.Storage = {};
if (!this._global.App.Storage)
this._global.App.Storage = {};
window.App.Storage[this.namespace] = value;
this._global.App.Storage[this.namespace] = value;
}
_clearWindow() {
if (!window || !window.App) {
if (!this._global || !this._global.App) {
throw new Error('StorageManager._saveToWindow called when window.App is not available');
}
if (window.App.Storage) {
delete window.App.Storage[this.namespace];
if (this._global.App.Storage) {
delete this._global.App.Storage[this.namespace];
}
}
}