chore(auto-submit-input): implemented destroy in auto-submit-input
This commit is contained in:
parent
3e01c8d910
commit
a75f6621ce
@ -1,5 +1,6 @@
|
||||
import * as debounce from 'lodash.debounce';
|
||||
import { Utility } from '../../core/utility';
|
||||
import { EventManager, EventWrapper, EVENT_TYPE } from '../../lib/event-manager/event-manager';
|
||||
|
||||
export const AUTO_SUBMIT_INPUT_UTIL_SELECTOR = '[uw-auto-submit-input]';
|
||||
|
||||
@ -12,6 +13,8 @@ export class AutoSubmitInput {
|
||||
|
||||
_element;
|
||||
|
||||
_eventManager;
|
||||
|
||||
_form;
|
||||
_debouncedHandler;
|
||||
|
||||
@ -22,6 +25,8 @@ export class AutoSubmitInput {
|
||||
|
||||
this._element = element;
|
||||
|
||||
this._eventManager = new EventManager();
|
||||
|
||||
if (this._element.classList.contains(AUTO_SUBMIT_INPUT_INITIALIZED_CLASS)) {
|
||||
return false;
|
||||
}
|
||||
@ -33,12 +38,16 @@ export class AutoSubmitInput {
|
||||
|
||||
this._debouncedHandler = debounce(this.autoSubmit, 500);
|
||||
|
||||
this._element.addEventListener('input', this._debouncedHandler);
|
||||
const inputEvent = new EventWrapper(EVENT_TYPE.INPUT, this._debouncedHandler.bind(this), this._element);
|
||||
this._eventManager.registerNewListener(inputEvent);
|
||||
|
||||
this._element.classList.add(AUTO_SUBMIT_INPUT_INITIALIZED_CLASS);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this._element.removeEventListener('input', this._debouncedHandler);
|
||||
this._eventManager.removeAllEventListenersFromUtil();
|
||||
if(this._element.classList.contains(AUTO_SUBMIT_INPUT_INITIALIZED_CLASS))
|
||||
this._element.classList.remove(AUTO_SUBMIT_INPUT_INITIALIZED_CLASS);
|
||||
}
|
||||
|
||||
autoSubmit = () => {
|
||||
|
||||
30
frontend/src/utils/form/auto-submit-input.spec.js
Normal file
30
frontend/src/utils/form/auto-submit-input.spec.js
Normal file
@ -0,0 +1,30 @@
|
||||
import { AutoSubmitInput, AUTO_SUBMIT_INPUT_INITIALIZED_CLASS } from './auto-submit-input.js';
|
||||
|
||||
describe('Auto-submit-input', () => {
|
||||
|
||||
let autoSubmitInput;
|
||||
|
||||
beforeEach(() => {
|
||||
const form = document.createElement('form');
|
||||
const element = document.createElement('input');
|
||||
element.setAttribute('type', 'text');
|
||||
form.append(element);
|
||||
autoSubmitInput = new AutoSubmitInput(element);
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(autoSubmitInput).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should destory auto-submit-button', () => {
|
||||
autoSubmitInput.destroy();
|
||||
expect(autoSubmitInput._eventManager._registeredListeners.length).toBe(0);
|
||||
expect(autoSubmitInput._element.classList).not.toEqual(jasmine.arrayContaining([AUTO_SUBMIT_INPUT_INITIALIZED_CLASS]));
|
||||
});
|
||||
|
||||
it('should throw if called without an element', () => {
|
||||
expect(() => {
|
||||
new AutoSubmitInput();
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user