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

View File

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