Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 92a3a56

Browse files
committed
test(composables): useinputEvents unit test
1 parent ce97da3 commit 92a3a56

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

jest.config.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ module.exports = {
3131
],
3232

3333
collectCoverage: true,
34-
collectCoverageFrom: ['<rootDir>/src/**/*.(vue|ts)'],
34+
collectCoverageFrom: [
35+
'<rootDir>/src/components/**/*.(vue|ts)',
36+
'<rootDir>/src/composables/**/*.(vue|ts)',
37+
'<rootDir>/src/core/**/*.(vue|ts)',
38+
],
39+
3540
preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
3641
};
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { FieldControl, TextField, Validator } from '@/core/factories';
2+
import { required } from '@/core/utils/validators';
3+
import { useInputEvents } from './useInputEvents';
4+
5+
describe('UseInputEvents', () => {
6+
let props;
7+
let ctx;
8+
let input;
9+
beforeEach(() => {
10+
props = {
11+
control: FieldControl({
12+
name: 'test-input',
13+
...TextField({
14+
label: 'Test Input',
15+
}),
16+
}),
17+
};
18+
ctx = {
19+
emit: (path, obj) => ({
20+
path,
21+
...obj,
22+
}),
23+
};
24+
input = document.createElement('input');
25+
input.type = 'text';
26+
});
27+
28+
it('should emit a change event with the value when element emit `input` event', async () => {
29+
const spy = jest.spyOn(ctx, 'emit');
30+
const { onInput } = useInputEvents(props, ctx.emit);
31+
input.value = 'Awiwi';
32+
input.addEventListener('input', onInput, false);
33+
input.dispatchEvent(new Event('input', { bubbles: true }));
34+
35+
expect(spy).toHaveBeenCalledWith('change', {
36+
name: 'test-input',
37+
value: 'Awiwi',
38+
});
39+
40+
input.removeEventListener('input', onInput);
41+
});
42+
43+
it('should not emit a change event if there is no value', async () => {
44+
const spy = jest.spyOn(ctx, 'emit');
45+
const { onInput } = useInputEvents(props, ctx.emit);
46+
47+
input.addEventListener('input', onInput, false);
48+
input.dispatchEvent(new Event('input', { bubbles: true }));
49+
50+
expect(spy).not.toBeCalled();
51+
52+
input.removeEventListener('input', onInput);
53+
});
54+
55+
it('should stopImmediatePropagation of event', async () => {
56+
const { onInput } = useInputEvents(props, ctx.emit);
57+
58+
const event = new Event('input', { bubbles: true });
59+
const spy = jest.spyOn(event, 'stopImmediatePropagation');
60+
61+
input.addEventListener('input', onInput, false);
62+
input.dispatchEvent(event);
63+
64+
expect(spy).toBeCalled();
65+
66+
input.removeEventListener('input', onInput);
67+
});
68+
69+
it('should stop propagation and prevent default if event change is trigerred', () => {
70+
const { onChange } = useInputEvents(props, ctx.emit);
71+
72+
const event = new Event('change', { bubbles: true });
73+
const spy = jest.spyOn(event, 'stopImmediatePropagation');
74+
const spy2 = jest.spyOn(event, 'preventDefault');
75+
76+
input.addEventListener('change', onChange, false);
77+
input.dispatchEvent(event);
78+
79+
expect(spy).toBeCalled();
80+
expect(spy2).toBeCalled();
81+
input.removeEventListener('change', onChange);
82+
});
83+
84+
it('should emit a change event with the value when element is checked', async () => {
85+
const spy = jest.spyOn(ctx, 'emit');
86+
const { onCheck } = useInputEvents(props, ctx.emit);
87+
const checkbox = document.createElement('input');
88+
checkbox.type = 'checkbox';
89+
checkbox.checked = true;
90+
checkbox.addEventListener('check', onCheck, false);
91+
checkbox.dispatchEvent(new Event('check', { bubbles: true }));
92+
93+
expect(spy).toHaveBeenCalledWith('change', {
94+
name: 'test-input',
95+
value: true,
96+
});
97+
98+
checkbox.removeEventListener('check', onCheck);
99+
});
100+
101+
it('should emit a focus event with the value when input is focused', async () => {
102+
const spy = jest.spyOn(ctx, 'emit');
103+
const { onFocus } = useInputEvents(props, ctx.emit);
104+
105+
input.addEventListener('focus', onFocus, false);
106+
input.dispatchEvent(new Event('focus', { bubbles: true }));
107+
108+
expect(spy).toHaveBeenCalledWith('focus', {
109+
name: 'test-input',
110+
});
111+
112+
input.removeEventListener('focus', onFocus);
113+
});
114+
115+
it('should emit a blur event with the value when input is blurred', async () => {
116+
const spy = jest.spyOn(ctx, 'emit');
117+
const { onBlur } = useInputEvents(props, ctx.emit);
118+
119+
input.addEventListener('blur', onBlur, false);
120+
input.dispatchEvent(new Event('blur', { bubbles: true }));
121+
122+
expect(spy).toHaveBeenCalledWith('blur', {
123+
name: 'test-input',
124+
});
125+
126+
input.removeEventListener('blur', onBlur);
127+
});
128+
});

src/composables/useInputEvents.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useInputValidation } from '@/composables/useValidation';
66
import { ValidationTriggerTypes } from '@/core/models';
77

88
interface InputEventsComposition {
9+
validate: (force: boolean) => void;
910
onInput: ($event: Event) => void;
1011
onChange: ($event: Event) => void;
1112
onCheck: ($event: Event) => void;
@@ -105,6 +106,7 @@ export function useInputEvents(props, emit): InputEventsComposition {
105106
);
106107

107108
return {
109+
validate,
108110
onFocus,
109111
onInput,
110112
onChange,

0 commit comments

Comments
 (0)