add rudimentary unit tests for some fe utilities

This commit is contained in:
Felix Hamann 2019-06-03 14:01:51 +02:00
parent ade9667671
commit 0d50a43edf
8 changed files with 146 additions and 8 deletions

View File

@ -18,4 +18,10 @@ describe('Alerts', () => {
it('should create', () => {
expect(alerts).toBeTruthy();
});
it('should throw if called without an element', () => {
expect(() => {
new Alerts();
}).toThrow();
});
});

View File

@ -42,11 +42,13 @@ export class Asidenav {
_initFavoritesButton() {
const favoritesBtn = document.querySelector('.' + FAVORITES_BTN_CLASS);
favoritesBtn.addEventListener('click', (event) => {
favoritesBtn.classList.toggle(FAVORITES_BTN_ACTIVE_CLASS);
this._element.classList.toggle(ASIDENAV_EXPANDED_CLASS);
event.preventDefault();
}, true);
if (favoritesBtn) {
favoritesBtn.addEventListener('click', (event) => {
favoritesBtn.classList.toggle(FAVORITES_BTN_ACTIVE_CLASS);
this._element.classList.toggle(ASIDENAV_EXPANDED_CLASS);
event.preventDefault();
}, true);
}
}
_initAsidenavSubmenus() {

View File

@ -0,0 +1,21 @@
import { Asidenav } from './asidenav';
describe('Asidenav', () => {
let asidenav;
beforeEach(() => {
const element = document.createElement('div');
asidenav = new Asidenav(element);
});
it('should create', () => {
expect(asidenav).toBeTruthy();
});
it('should throw if called without an element', () => {
expect(() => {
new Asidenav();
}).toThrow();
});
});

View File

@ -0,0 +1,21 @@
import { AsyncForm } from './async-form';
describe('AsyncForm', () => {
let asyncForm;
beforeEach(() => {
const element = document.createElement('div');
asyncForm = new AsyncForm(element);
});
it('should create', () => {
expect(asyncForm).toBeTruthy();
});
it('should throw if called without an element', () => {
expect(() => {
new AsyncForm();
}).toThrow();
});
});

View File

@ -44,6 +44,10 @@ export class AsyncTable {
throw new Error('Async Table utility cannot be setup without an element!');
}
if (!app) {
throw new Error('Async Table utility cannot be setup without an app!');
}
this._element = element;
this._app = app;
@ -56,7 +60,12 @@ export class AsyncTable {
this._asyncTableHeader = this._element.dataset.asyncTableDbHeader;
}
const rawTableId = this._element.querySelector('table').id;
const table = this._element.querySelector('table');
if (!table) {
throw new Error('Async Table utility needs a <table> in its element!');
}
const rawTableId = table.id;
this._cssIdPrefix = findCssIdPrefix(rawTableId);
this._asyncTableId = rawTableId.replace(this._cssIdPrefix, '');

View File

@ -0,0 +1,52 @@
import { AsyncTable } from './async-table';
const AppTestMock = {
httpClient: {
get: () => {},
},
htmlHelpers: {
parseResponse: () => {},
},
utilRegistry: {
setupAll: () => {},
},
};
describe('AsyncTable', () => {
let asyncTable;
beforeEach(() => {
const element = document.createElement('div');
const scrollTable = document.createElement('div');
const table = document.createElement('table');
scrollTable.classList.add('scrolltable');
scrollTable.appendChild(table);
element.appendChild(scrollTable);
asyncTable = new AsyncTable(element, AppTestMock);
});
it('should create', () => {
expect(asyncTable).toBeTruthy();
});
it('should throw if element does not contain a .scrolltable', () => {
const element = document.createElement('div');
expect(() => {
new AsyncTable(element, AppTestMock);
}).toThrow();
});
it('should throw if element does not contain a table', () => {
const element = document.createElement('div');
expect(() => {
new AsyncTable(element, AppTestMock);
}).toThrow();
});
it('should throw if called without an element', () => {
expect(() => {
new AsyncTable();
}).toThrow();
});
});

View File

@ -92,8 +92,8 @@ export class CheckAll {
this._checkAllCheckbox.setAttribute('id', this._getCheckboxId());
th.insertBefore(this._checkAllCheckbox, th.firstChild);
// manually set up new checkbox
this._app.utilRegistry.setup(this._app.utilRegistry.find('checkbox'), th);
// set up new checkbox
this._app.utilRegistry.setupAll(th);
this._checkAllCheckbox.addEventListener('input', this._onCheckAllCheckboxInput);
this._setupCheckboxListeners();

View File

@ -0,0 +1,27 @@
import { CheckAll } from './check-all';
const MOCK_APP = {
utilRegistry: {
setupAll: () => {},
},
};
describe('CheckAll', () => {
let checkAll;
beforeEach(() => {
const element = document.createElement('div');
checkAll = new CheckAll(element, MOCK_APP);
});
it('should create', () => {
expect(checkAll).toBeTruthy();
});
it('should throw if called without an element', () => {
expect(() => {
new CheckAll();
}).toThrow();
});
});