Skip to content

Commit

Permalink
Improve coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
terrylinooo committed Dec 2, 2023
1 parent 885bc20 commit 42aed15
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/core/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const listen = (main: Main): void => {
const { form, event, state, setting } = main;
const { fields, asterisk, callback } = setting;

/* v8 ignore next 4 */
if (event === null) {
console.error('EventAdapter is not initialized.');
return;
Expand All @@ -20,6 +21,7 @@ export const listen = (main: Main): void => {
if ((window as Window & { disableautofill_unit_test?: boolean }).disableautofill_unit_test) {
console.log('test ok, submitted.');
return;
/* v8 ignore next 2 */
}
form.submit();
};
Expand Down Expand Up @@ -61,13 +63,9 @@ export const listen = (main: Main): void => {
});

restorePassword.then(() => {
if (typeof callback === 'function') {
if (callback(form)) {
submit(form);
}
return;
if (typeof callback === 'function' && callback(form)) {
submit(form);
}
submit(form);
});
};

Expand Down
1 change: 1 addition & 0 deletions src/core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default class Main {
}

#resetForm(): void {
/* v8 ignore next 3 */
if (this.form.parentNode === null || this.clonedForm === null) {
return;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/units/event-adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,28 @@ describe('EventAdapter class', () => {
mockElement.dispatchEvent(new Event('testEvent'));
expect(mockHandler).not.toHaveBeenCalled();
});

test('emit should dispatch an event if it exists', () => {
eventAdapter.on('testEvent', mockHandler);
eventAdapter.emit('testEvent');
expect(mockHandler).toHaveBeenCalledTimes(1);
});

test('emit should not dispatch an event if it does not exist', () => {
eventAdapter.emit('nonExistentEvent');
expect(mockHandler).not.toHaveBeenCalled();
});

test('mock should directly invoke the event handler', () => {
eventAdapter.on('testEvent', mockHandler);
const fakeEvent = new Event('testEvent');
eventAdapter.mock('testEvent', fakeEvent);
expect(mockHandler).toHaveBeenCalledWith(fakeEvent);
});

test('mock should not invoke the event handler if event does not exist', () => {
const fakeEvent = new Event('nonExistentEvent');
eventAdapter.mock('nonExistentEvent', fakeEvent);
expect(mockHandler).not.toHaveBeenCalled();
});
});
43 changes: 43 additions & 0 deletions tests/units/handle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,47 @@ describe('handle function', () => {

expect(passwordField.value).toBe(originalPassword);
});

test('should handle Backspace correctly in the middle of the text', () => {
const backspaceEvent = new KeyboardEvent('keydown', { code: 'Backspace' });
passwordField.value = 'newpassword';
passwordField.setSelectionRange(3, 3); // Set cursor position

handle({
fieldDom: passwordField,
event: backspaceEvent,
asterisk,
action: 'randomize',
state,
});

// Simulate backspace effect
passwordField.value = passwordField.value.substring(0, 2) + passwordField.value.substring(3);
handle({
fieldDom: passwordField,
event: backspaceEvent,
asterisk,
action: 'randomize',
state,
});

expect(passwordField.value).toBe(asterisk.repeat(passwordField.value.length));
});

test('should assign a random ID to an element without ID', () => {
const inputEvent = new Event('input');
const noIdField = document.createElement('input');
noIdField.type = 'password';
document.body.appendChild(noIdField);

handle({
fieldDom: noIdField,
event: inputEvent,
asterisk,
action: 'randomize',
state,
});

expect(noIdField.id).not.toBe('');
});
});
6 changes: 5 additions & 1 deletion tests/units/listen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ describe('Listen function', () => {
formElement = document.getElementById('login-form');
passwordField = document.getElementById('password');

main = new Main('#login-form', { fields: ['#password'], asterisk: '●' });
main = new Main('#login-form', {
fields: ['#password'],
asterisk: '●',
callback: () => true,
});
state = main.getState();

keyupEvent = new KeyboardEvent('keyup', {
Expand Down
21 changes: 21 additions & 0 deletions tests/units/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,25 @@ describe('Main class', () => {

console.error = originalConsoleError;
});

test('should properly destroy the instance', () => {
const main = new Main('#login-form', {});
const eventSpy = vi.spyOn(main.event, 'emit');
const eventDestroySpy = vi.spyOn(main.event, 'destroy');

main.destroy();

expect(eventSpy).toHaveBeenCalledWith('restorePasswordName');
expect(eventDestroySpy).toHaveBeenCalled();
});

test('should reset form when calling #resetForm', () => {
const main = new Main('#login-form', {});
const originalForm = main.form;
const parentNode = main.form.parentNode;

main.destroy();

expect(parentNode.querySelector('#login-form')).not.toBe(originalForm);
});
});

0 comments on commit 42aed15

Please sign in to comment.