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

Commit 550bdf5

Browse files
committed
Merge remote-tracking branch 'origin/next' into final-stable-next-on-master
2 parents 6b6992a + ff62de0 commit 550bdf5

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

jest.config.js

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

src/composables/useInputEvents.ts

Lines changed: 2 additions & 0 deletions
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)