chore(async-form): implemented destroy method in async-form

This commit is contained in:
Johannes Eder 2021-06-28 09:33:53 +02:00 committed by Sarah Vaupel
parent a25b59b318
commit e2aa913ff3
3 changed files with 19 additions and 3 deletions

View File

@ -5,6 +5,7 @@ export const EVENT_TYPE = {
INVALID : 'invalid', INVALID : 'invalid',
CHANGE : 'change', CHANGE : 'change',
MOUSE_OVER : 'mouseover', MOUSE_OVER : 'mouseover',
SUBMIT : 'submit',
//more to be added //more to be added
}; };

View File

@ -1,5 +1,6 @@
import { Utility } from '../../core/utility'; import { Utility } from '../../core/utility';
import { Datepicker } from '../form/datepicker'; import { Datepicker } from '../form/datepicker';
import { EventManager, EventWrapper, EVENT_TYPE } from '../../lib/event-manager/event-manager';
import './async-form.sass'; import './async-form.sass';
const ASYNC_FORM_INITIALIZED_CLASS = 'check-all--initialized'; const ASYNC_FORM_INITIALIZED_CLASS = 'check-all--initialized';
@ -20,6 +21,8 @@ export class AsyncForm {
_element; _element;
_app; _app;
_eventManager;
constructor(element, app) { constructor(element, app) {
if (!element) { if (!element) {
throw new Error('Async Form Utility cannot be setup without an element!'); throw new Error('Async Form Utility cannot be setup without an element!');
@ -28,17 +31,23 @@ export class AsyncForm {
this._element = element; this._element = element;
this._app = app; this._app = app;
this._eventManager = new EventManager();
if (this._element.classList.contains(ASYNC_FORM_INITIALIZED_CLASS)) { if (this._element.classList.contains(ASYNC_FORM_INITIALIZED_CLASS)) {
return false; return false;
} }
this._element.addEventListener('submit', this._submitHandler); const submitEvent = new EventWrapper(EVENT_TYPE.SUBMIT, this._submitHandler.bind(this), this._element);
this._eventManager.registerNewListener(submitEvent);
this._element.classList.add(ASYNC_FORM_INITIALIZED_CLASS); this._element.classList.add(ASYNC_FORM_INITIALIZED_CLASS);
} }
destroy() { destroy() {
// TODO this._eventManager.removeAllEventListenersFromUtil();
if(this._element.classList.contains(ASYNC_FORM_INITIALIZED_CLASS))
this._element.classList.remove(ASYNC_FORM_INITIALIZED_CLASS);
} }
_processResponse(response) { _processResponse(response) {

View File

@ -1,4 +1,4 @@
import { AsyncForm } from './async-form'; import { AsyncForm, ASYNC_FORM_INITIALIZED_CLASS } from './async-form';
describe('AsyncForm', () => { describe('AsyncForm', () => {
@ -13,6 +13,12 @@ describe('AsyncForm', () => {
expect(asyncForm).toBeTruthy(); expect(asyncForm).toBeTruthy();
}); });
it('should destroy asyncForm', () => {
asyncForm.destroy();
expect(asyncForm._eventManager._registeredListeners.length).toBe(0);
expect(asyncForm._element.classList).not.toContain(ASYNC_FORM_INITIALIZED_CLASS);
});
it('should throw if called without an element', () => { it('should throw if called without an element', () => {
expect(() => { expect(() => {
new AsyncForm(); new AsyncForm();