use-mask-input 3.10.0 → 3.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -1
- package/dist/antd.cjs +1 -65
- package/dist/antd.cjs.map +1 -1
- package/dist/antd.d.cts +12 -10
- package/dist/{antd.d.ts → antd.d.mts} +12 -10
- package/dist/antd.mjs +2 -0
- package/dist/antd.mjs.map +1 -0
- package/dist/index-BmKzoe0X.d.cts +836 -0
- package/dist/index-BmKzoe0X.d.mts +836 -0
- package/dist/index.cjs +1 -173
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +18 -15
- package/dist/{index.d.ts → index.d.mts} +18 -15
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/withMask-VWeBqi_u.cjs +2 -0
- package/dist/withMask-VWeBqi_u.cjs.map +1 -0
- package/dist/withMask-jrErtLYS.mjs +2 -0
- package/dist/withMask-jrErtLYS.mjs.map +1 -0
- package/package.json +42 -22
- package/src/api/withMask.ts +1 -1
- package/src/core/inputmask.ts +10 -0
- package/src/core/maskEngine.ts +2 -2
- package/src/utils/maskHelpers.ts +5 -3
- package/dist/antd.js +0 -63
- package/dist/antd.js.map +0 -1
- package/dist/chunk-DTC7JTZP.cjs +0 -3925
- package/dist/chunk-DTC7JTZP.cjs.map +0 -1
- package/dist/chunk-TVCNC3TP.js +0 -3915
- package/dist/chunk-TVCNC3TP.js.map +0 -1
- package/dist/index-D8KkaDbQ.d.cts +0 -596
- package/dist/index-D8KkaDbQ.d.ts +0 -596
- package/dist/index.js +0 -165
- package/dist/index.js.map +0 -1
- package/src/antd/useHookFormMaskAntd.spec.ts +0 -181
- package/src/antd/useMaskInputAntd-server.spec.tsx +0 -37
- package/src/antd/useMaskInputAntd.spec.tsx +0 -131
- package/src/api/useHookFormMask.spec.ts +0 -259
- package/src/api/useMaskInput-server.spec.tsx +0 -30
- package/src/api/useMaskInput.spec.tsx +0 -238
- package/src/api/withHookFormMask.spec.ts +0 -179
- package/src/api/withMask.spec.ts +0 -137
- package/src/api/withTanStackFormMask.spec.ts +0 -76
- package/src/core/elementResolver.spec.ts +0 -175
- package/src/core/maskConfig.spec.ts +0 -208
- package/src/core/maskEngine.spec.ts +0 -114
- package/src/utils/flow.spec.ts +0 -57
- package/src/utils/isServer.spec.ts +0 -15
- package/src/utils/moduleInterop.spec.ts +0 -37
package/src/api/withMask.spec.ts
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import inputmask from 'inputmask';
|
|
2
|
-
import {
|
|
3
|
-
beforeEach, describe, expect, it, vi,
|
|
4
|
-
} from 'vitest';
|
|
5
|
-
|
|
6
|
-
import withMask from './withMask';
|
|
7
|
-
|
|
8
|
-
vi.mock('inputmask', () => ({
|
|
9
|
-
default: vi.fn((options) => ({
|
|
10
|
-
mask: vi.fn(),
|
|
11
|
-
options,
|
|
12
|
-
})),
|
|
13
|
-
}));
|
|
14
|
-
|
|
15
|
-
vi.mock('../utils/isServer', () => ({
|
|
16
|
-
default: false,
|
|
17
|
-
}));
|
|
18
|
-
|
|
19
|
-
describe('withMask', () => {
|
|
20
|
-
beforeEach(() => {
|
|
21
|
-
vi.clearAllMocks();
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('returns a function', () => {
|
|
25
|
-
const result = withMask('999-999');
|
|
26
|
-
expect(typeof result).toBe('function');
|
|
27
|
-
expect(typeof result.unmaskedValue).toBe('function');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('applies mask to input element', () => {
|
|
31
|
-
const input = document.createElement('input');
|
|
32
|
-
const maskFn = vi.fn();
|
|
33
|
-
vi.mocked(inputmask).mockReturnValue({ mask: maskFn } as any);
|
|
34
|
-
|
|
35
|
-
const refCallback = withMask('999-999');
|
|
36
|
-
refCallback(input);
|
|
37
|
-
|
|
38
|
-
expect(maskFn).toHaveBeenCalledWith(input);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('exposes the unmasked value from the masked input', () => {
|
|
42
|
-
const input = document.createElement('input');
|
|
43
|
-
const maskFn = vi.fn();
|
|
44
|
-
vi.mocked(inputmask).mockReturnValue({ mask: maskFn } as any);
|
|
45
|
-
|
|
46
|
-
const refCallback = withMask('999-999');
|
|
47
|
-
refCallback(input);
|
|
48
|
-
|
|
49
|
-
input.inputmask = {
|
|
50
|
-
unmaskedvalue: vi.fn(() => '2026-04-01'),
|
|
51
|
-
} as any;
|
|
52
|
-
|
|
53
|
-
expect(refCallback.unmaskedValue()).toBe('2026-04-01');
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('does nothing if input is null', () => {
|
|
57
|
-
const maskFn = vi.fn();
|
|
58
|
-
vi.mocked(inputmask).mockReturnValue({ mask: maskFn } as any);
|
|
59
|
-
|
|
60
|
-
const refCallback = withMask('999-999');
|
|
61
|
-
refCallback(null);
|
|
62
|
-
|
|
63
|
-
expect(maskFn).not.toHaveBeenCalled();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('does nothing if mask is null', () => {
|
|
67
|
-
const input = document.createElement('input');
|
|
68
|
-
const maskFn = vi.fn();
|
|
69
|
-
vi.mocked(inputmask).mockReturnValue({ mask: maskFn } as any);
|
|
70
|
-
|
|
71
|
-
const refCallback = withMask(null);
|
|
72
|
-
refCallback(input);
|
|
73
|
-
|
|
74
|
-
expect(maskFn).not.toHaveBeenCalled();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('applies mask with custom options', () => {
|
|
78
|
-
const input = document.createElement('input');
|
|
79
|
-
const maskFn = vi.fn();
|
|
80
|
-
vi.mocked(inputmask).mockReturnValue({ mask: maskFn } as any);
|
|
81
|
-
|
|
82
|
-
const refCallback = withMask('999-999', { placeholder: '_' });
|
|
83
|
-
refCallback(input);
|
|
84
|
-
|
|
85
|
-
expect(maskFn).toHaveBeenCalledWith(input);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('works with alias masks', () => {
|
|
89
|
-
const input = document.createElement('input');
|
|
90
|
-
const maskFn = vi.fn();
|
|
91
|
-
vi.mocked(inputmask).mockReturnValue({ mask: maskFn } as any);
|
|
92
|
-
|
|
93
|
-
const refCallback = withMask('cpf');
|
|
94
|
-
refCallback(input);
|
|
95
|
-
|
|
96
|
-
expect(maskFn).toHaveBeenCalledWith(input);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('works with array masks', () => {
|
|
100
|
-
const input = document.createElement('input');
|
|
101
|
-
const maskFn = vi.fn();
|
|
102
|
-
vi.mocked(inputmask).mockReturnValue({ mask: maskFn } as any);
|
|
103
|
-
|
|
104
|
-
const refCallback = withMask(['999-999', '9999-9999']);
|
|
105
|
-
refCallback(input);
|
|
106
|
-
|
|
107
|
-
expect(maskFn).toHaveBeenCalledWith(input);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('returns the same callback reference for the same mask (stable identity)', () => {
|
|
111
|
-
const first = withMask('999-999');
|
|
112
|
-
const second = withMask('999-999');
|
|
113
|
-
|
|
114
|
-
expect(first).toBe(second);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('returns the same callback reference for the same array mask', () => {
|
|
118
|
-
const first = withMask(['999-999', '9999-9999']);
|
|
119
|
-
const second = withMask(['999-999', '9999-9999']);
|
|
120
|
-
|
|
121
|
-
expect(first).toBe(second);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('returns different callbacks for different masks', () => {
|
|
125
|
-
const phone = withMask('999-999');
|
|
126
|
-
const cpf = withMask('cpf');
|
|
127
|
-
|
|
128
|
-
expect(phone).not.toBe(cpf);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('always returns a new callback when options are provided', () => {
|
|
132
|
-
const first = withMask('999-999', { placeholder: '_' });
|
|
133
|
-
const second = withMask('999-999', { placeholder: '_' });
|
|
134
|
-
|
|
135
|
-
expect(first).not.toBe(second);
|
|
136
|
-
});
|
|
137
|
-
});
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import inputmask from 'inputmask';
|
|
2
|
-
import {
|
|
3
|
-
beforeEach,
|
|
4
|
-
describe, expect, it, vi,
|
|
5
|
-
} from 'vitest';
|
|
6
|
-
|
|
7
|
-
import withTanStackFormMask from './withTanStackFormMask';
|
|
8
|
-
|
|
9
|
-
import type { TanStackFormInputProps } from '../types';
|
|
10
|
-
|
|
11
|
-
vi.mock('inputmask', () => ({
|
|
12
|
-
default: vi.fn((options) => ({
|
|
13
|
-
mask: vi.fn(),
|
|
14
|
-
options,
|
|
15
|
-
})),
|
|
16
|
-
}));
|
|
17
|
-
|
|
18
|
-
describe('withTanStackFormMask', () => {
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
vi.clearAllMocks();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('returns masked input props with stable structure', () => {
|
|
24
|
-
const inputProps: TanStackFormInputProps = {
|
|
25
|
-
name: 'cpf',
|
|
26
|
-
ref: vi.fn(),
|
|
27
|
-
onBlur: vi.fn(),
|
|
28
|
-
onChange: vi.fn(),
|
|
29
|
-
value: '',
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const result = withTanStackFormMask(inputProps, 'cpf');
|
|
33
|
-
|
|
34
|
-
expect(typeof result.ref).toBe('function');
|
|
35
|
-
expect(result.onBlur).toBe(inputProps.onBlur);
|
|
36
|
-
expect(result.onChange).toBe(inputProps.onChange);
|
|
37
|
-
expect(result.name).toBe('cpf');
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('applies mask when ref callback receives an input', () => {
|
|
41
|
-
const maskFn = vi.fn();
|
|
42
|
-
vi.mocked(inputmask).mockReturnValue({ mask: maskFn } as any);
|
|
43
|
-
|
|
44
|
-
const input = document.createElement('input');
|
|
45
|
-
const originalRef = vi.fn();
|
|
46
|
-
const inputProps: TanStackFormInputProps = {
|
|
47
|
-
name: 'phone',
|
|
48
|
-
ref: originalRef,
|
|
49
|
-
onBlur: vi.fn(),
|
|
50
|
-
onChange: vi.fn(),
|
|
51
|
-
value: '',
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const result = withTanStackFormMask(inputProps, '(99) 99999-9999');
|
|
55
|
-
result.ref(input);
|
|
56
|
-
|
|
57
|
-
expect(maskFn).toHaveBeenCalled();
|
|
58
|
-
expect(originalRef).toHaveBeenCalledWith(input);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('keeps ref cache stable for same ref and mask', () => {
|
|
62
|
-
const originalRef = vi.fn();
|
|
63
|
-
const inputProps: TanStackFormInputProps = {
|
|
64
|
-
name: 'cpf',
|
|
65
|
-
ref: originalRef,
|
|
66
|
-
onBlur: vi.fn(),
|
|
67
|
-
onChange: vi.fn(),
|
|
68
|
-
value: '',
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const first = withTanStackFormMask(inputProps, 'cpf');
|
|
72
|
-
const second = withTanStackFormMask(inputProps, 'cpf');
|
|
73
|
-
|
|
74
|
-
expect(first.ref).toBe(second.ref);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
describe, expect, it, vi,
|
|
3
|
-
} from 'vitest';
|
|
4
|
-
|
|
5
|
-
import { findInputElement, isHTMLElement, resolveInputRef } from './elementResolver';
|
|
6
|
-
|
|
7
|
-
import type { Input } from '..';
|
|
8
|
-
|
|
9
|
-
describe('elementResolver', () => {
|
|
10
|
-
describe('isHTMLElement', () => {
|
|
11
|
-
it('returns true for valid HTMLElement', () => {
|
|
12
|
-
const element = document.createElement('div');
|
|
13
|
-
expect(isHTMLElement(element)).toBe(true);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('returns false for null', () => {
|
|
17
|
-
expect(isHTMLElement(null)).toBe(false);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('returns false for undefined', () => {
|
|
21
|
-
expect(isHTMLElement(undefined)).toBe(false);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('returns false for string', () => {
|
|
25
|
-
expect(isHTMLElement('string')).toBe(false);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('returns false for number', () => {
|
|
29
|
-
expect(isHTMLElement(123)).toBe(false);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('returns false for object without nodeType', () => {
|
|
33
|
-
expect(isHTMLElement({})).toBe(false);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('returns false for object without querySelector', () => {
|
|
37
|
-
expect(isHTMLElement({ nodeType: 1 })).toBe(false);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('returns false for object with non-function querySelector', () => {
|
|
41
|
-
expect(isHTMLElement({ nodeType: 1, querySelector: 'not a function' })).toBe(false);
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
describe('findInputElement', () => {
|
|
46
|
-
it('returns null for null', () => {
|
|
47
|
-
expect(findInputElement(null)).toBe(null);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('returns null for undefined', () => {
|
|
51
|
-
expect(findInputElement(undefined)).toBe(null);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('returns input element directly if it is an INPUT', () => {
|
|
55
|
-
const input = document.createElement('input');
|
|
56
|
-
expect(findInputElement(input)).toBe(input);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('returns textarea element directly if it is a TEXTAREA', () => {
|
|
60
|
-
const textarea = document.createElement('textarea');
|
|
61
|
-
expect(findInputElement(textarea)).toBe(textarea);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('finds input inside wrapper element', () => {
|
|
65
|
-
const wrapper = document.createElement('div');
|
|
66
|
-
const input = document.createElement('input');
|
|
67
|
-
wrapper.appendChild(input);
|
|
68
|
-
expect(findInputElement(wrapper)).toBe(input);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('finds textarea inside wrapper element', () => {
|
|
72
|
-
const wrapper = document.createElement('div');
|
|
73
|
-
const textarea = document.createElement('textarea');
|
|
74
|
-
wrapper.appendChild(textarea);
|
|
75
|
-
expect(findInputElement(wrapper)).toBe(textarea);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('returns null if no input found inside wrapper', () => {
|
|
79
|
-
const wrapper = document.createElement('div');
|
|
80
|
-
const span = document.createElement('span');
|
|
81
|
-
wrapper.appendChild(span);
|
|
82
|
-
expect(findInputElement(wrapper)).toBe(null);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('returns null for invalid element', () => {
|
|
86
|
-
expect(findInputElement('not an element')).toBe(null);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it('handles querySelector error gracefully', () => {
|
|
90
|
-
const element = {
|
|
91
|
-
nodeType: 1,
|
|
92
|
-
nodeName: 'DIV',
|
|
93
|
-
querySelector: vi.fn(() => {
|
|
94
|
-
throw new Error('querySelector error');
|
|
95
|
-
}),
|
|
96
|
-
};
|
|
97
|
-
expect(findInputElement(element)).toBe(null);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('handles element without querySelector method', () => {
|
|
101
|
-
const element = {
|
|
102
|
-
nodeType: 1,
|
|
103
|
-
nodeName: 'DIV',
|
|
104
|
-
};
|
|
105
|
-
expect(findInputElement(element)).toBe(null);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('handles element with non-function querySelector', () => {
|
|
109
|
-
const element = {
|
|
110
|
-
nodeType: 1,
|
|
111
|
-
nodeName: 'DIV',
|
|
112
|
-
querySelector: 'not a function',
|
|
113
|
-
};
|
|
114
|
-
expect(findInputElement(element)).toBe(null);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('handles element where querySelector is not in element', () => {
|
|
118
|
-
const element = {
|
|
119
|
-
nodeType: 1,
|
|
120
|
-
nodeName: 'DIV',
|
|
121
|
-
};
|
|
122
|
-
// element doesn't have querySelector property
|
|
123
|
-
expect(findInputElement(element)).toBe(null);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('handles querySelector returning null', () => {
|
|
127
|
-
const element = {
|
|
128
|
-
nodeType: 1,
|
|
129
|
-
nodeName: 'DIV',
|
|
130
|
-
querySelector: vi.fn(() => null),
|
|
131
|
-
};
|
|
132
|
-
expect(findInputElement(element)).toBe(null);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
it('handles querySelector returning non-HTMLElement', () => {
|
|
136
|
-
const element = {
|
|
137
|
-
nodeType: 1,
|
|
138
|
-
nodeName: 'DIV',
|
|
139
|
-
querySelector: vi.fn(() => 'not an element'),
|
|
140
|
-
};
|
|
141
|
-
expect(findInputElement(element)).toBe(null);
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
describe('resolveInputRef', () => {
|
|
146
|
-
it('returns null for null', () => {
|
|
147
|
-
expect(resolveInputRef(null)).toBe(null);
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it('returns element for direct HTMLElement', () => {
|
|
151
|
-
const input = document.createElement('input');
|
|
152
|
-
expect(resolveInputRef(input)).toBe(input);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it('returns element from ref object with current', () => {
|
|
156
|
-
const input = document.createElement('input');
|
|
157
|
-
const ref = { current: input };
|
|
158
|
-
expect(resolveInputRef(ref as unknown as Input)).toBe(input);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('returns null for ref object with null current', () => {
|
|
162
|
-
const ref = { current: null } as unknown as Input;
|
|
163
|
-
expect(resolveInputRef(ref)).toBe(null);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it('returns null for ref object with invalid current', () => {
|
|
167
|
-
const ref = { current: 'not an element' } as unknown as Input;
|
|
168
|
-
expect(resolveInputRef(ref)).toBe(null);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('returns null for invalid input type', () => {
|
|
172
|
-
expect(resolveInputRef('not an element' as unknown as Input)).toBe(null);
|
|
173
|
-
});
|
|
174
|
-
});
|
|
175
|
-
});
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import { getMaskOptions } from './maskConfig';
|
|
4
|
-
|
|
5
|
-
describe('maskConfig', () => {
|
|
6
|
-
describe('getMaskOptions', () => {
|
|
7
|
-
it('returns default options when no mask is provided', () => {
|
|
8
|
-
const options = getMaskOptions();
|
|
9
|
-
expect(options).toEqual({
|
|
10
|
-
jitMasking: false,
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('merges custom options with defaults', () => {
|
|
15
|
-
const options = getMaskOptions(undefined, { placeholder: '_' });
|
|
16
|
-
expect(options).toEqual({
|
|
17
|
-
jitMasking: false,
|
|
18
|
-
placeholder: '_',
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('returns options for datetime mask', () => {
|
|
23
|
-
const options = getMaskOptions('datetime');
|
|
24
|
-
expect(options).toEqual({
|
|
25
|
-
alias: 'datetime',
|
|
26
|
-
jitMasking: false,
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('returns options for email mask', () => {
|
|
31
|
-
const options = getMaskOptions('email');
|
|
32
|
-
expect(options).toEqual({
|
|
33
|
-
alias: 'email',
|
|
34
|
-
placeholder: '',
|
|
35
|
-
jitMasking: false,
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('returns options for numeric mask', () => {
|
|
40
|
-
const options = getMaskOptions('numeric');
|
|
41
|
-
expect(options).toEqual({
|
|
42
|
-
alias: 'numeric',
|
|
43
|
-
placeholder: '',
|
|
44
|
-
jitMasking: false,
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('returns options for currency mask', () => {
|
|
49
|
-
const options = getMaskOptions('currency');
|
|
50
|
-
expect(options).toEqual({
|
|
51
|
-
alias: 'currency',
|
|
52
|
-
prefix: '$ ',
|
|
53
|
-
placeholder: '',
|
|
54
|
-
jitMasking: false,
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('returns options for decimal mask', () => {
|
|
59
|
-
const options = getMaskOptions('decimal');
|
|
60
|
-
expect(options).toEqual({
|
|
61
|
-
alias: 'decimal',
|
|
62
|
-
placeholder: '',
|
|
63
|
-
jitMasking: false,
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('returns options for integer mask', () => {
|
|
68
|
-
const options = getMaskOptions('integer');
|
|
69
|
-
expect(options).toEqual({
|
|
70
|
-
alias: 'integer',
|
|
71
|
-
placeholder: '',
|
|
72
|
-
jitMasking: false,
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('returns options for percentage mask', () => {
|
|
77
|
-
const options = getMaskOptions('percentage');
|
|
78
|
-
expect(options).toEqual({
|
|
79
|
-
alias: 'percentage',
|
|
80
|
-
placeholder: ' %',
|
|
81
|
-
suffix: ' %',
|
|
82
|
-
jitMasking: false,
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('returns options for url mask', () => {
|
|
87
|
-
const options = getMaskOptions('url');
|
|
88
|
-
expect(options).toEqual({
|
|
89
|
-
alias: 'url',
|
|
90
|
-
placeholder: 'https://',
|
|
91
|
-
jitMasking: false,
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('returns options for ip mask', () => {
|
|
96
|
-
const options = getMaskOptions('ip');
|
|
97
|
-
expect(options).toEqual({
|
|
98
|
-
alias: 'ip',
|
|
99
|
-
jitMasking: false,
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('returns options for mac mask', () => {
|
|
104
|
-
const options = getMaskOptions('mac');
|
|
105
|
-
expect(options).toEqual({
|
|
106
|
-
alias: 'mac',
|
|
107
|
-
jitMasking: false,
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('returns options for ssn mask', () => {
|
|
112
|
-
const options = getMaskOptions('ssn');
|
|
113
|
-
expect(options).toEqual({
|
|
114
|
-
alias: 'ssn',
|
|
115
|
-
jitMasking: false,
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('returns options for brl-currency mask', () => {
|
|
120
|
-
const options = getMaskOptions('brl-currency');
|
|
121
|
-
expect(options).toEqual({
|
|
122
|
-
alias: 'currency',
|
|
123
|
-
prefix: 'R$ ',
|
|
124
|
-
placeholder: '0,00',
|
|
125
|
-
displayFormat: 'currency',
|
|
126
|
-
radixPoint: ',',
|
|
127
|
-
groupSeparator: '.',
|
|
128
|
-
autoUnmask: true,
|
|
129
|
-
jitMasking: false,
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('returns options for cpf mask', () => {
|
|
134
|
-
const options = getMaskOptions('cpf');
|
|
135
|
-
expect(options).toEqual({
|
|
136
|
-
mask: '999.999.999-99',
|
|
137
|
-
placeholder: '___.___.___-__',
|
|
138
|
-
jitMasking: false,
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('returns options for cnpj mask', () => {
|
|
143
|
-
const options = getMaskOptions('cnpj');
|
|
144
|
-
expect(options).toEqual({
|
|
145
|
-
mask: ['A|9{2}.A|9{3}.A|9{3}/A|9{4}-9{2}'],
|
|
146
|
-
placeholder: '__.___.___/____-__',
|
|
147
|
-
jitMasking: false,
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
it('returns options for br-bank-account mask', () => {
|
|
152
|
-
const options = getMaskOptions('br-bank-account');
|
|
153
|
-
expect(options).toEqual({
|
|
154
|
-
mask: [
|
|
155
|
-
'9{4,10}[-]9',
|
|
156
|
-
'999999[-][9]',
|
|
157
|
-
'[999]9{7,8}[-]9',
|
|
158
|
-
'[9999]9{8}[-]9',
|
|
159
|
-
],
|
|
160
|
-
placeholder: '',
|
|
161
|
-
greedy: false,
|
|
162
|
-
jitMasking: false,
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it('returns options for br-bank-agency mask', () => {
|
|
167
|
-
const options = getMaskOptions('br-bank-agency');
|
|
168
|
-
expect(options).toEqual({
|
|
169
|
-
mask: '9{1,5}[-][9]',
|
|
170
|
-
placeholder: '',
|
|
171
|
-
jitMasking: false,
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it('returns custom mask string', () => {
|
|
176
|
-
const options = getMaskOptions('999-999');
|
|
177
|
-
expect(options).toEqual({
|
|
178
|
-
mask: '999-999',
|
|
179
|
-
jitMasking: false,
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it('returns custom mask array', () => {
|
|
184
|
-
const mask = ['999-999', '9999-9999'];
|
|
185
|
-
const options = getMaskOptions(mask);
|
|
186
|
-
expect(options).toEqual({
|
|
187
|
-
mask,
|
|
188
|
-
jitMasking: false,
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
it('merges custom options with alias options', () => {
|
|
193
|
-
const options = getMaskOptions('cpf', { placeholder: '___' });
|
|
194
|
-
expect(options).toEqual({
|
|
195
|
-
mask: '999.999.999-99',
|
|
196
|
-
placeholder: '___',
|
|
197
|
-
jitMasking: false,
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it('handles null mask', () => {
|
|
202
|
-
const options = getMaskOptions(null);
|
|
203
|
-
expect(options).toEqual({
|
|
204
|
-
jitMasking: false,
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
});
|
|
208
|
-
});
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import inputmask from 'inputmask';
|
|
2
|
-
import {
|
|
3
|
-
beforeEach, describe, expect, it, vi,
|
|
4
|
-
} from 'vitest';
|
|
5
|
-
|
|
6
|
-
import { applyMaskToElement, createMaskInstance } from './maskEngine';
|
|
7
|
-
|
|
8
|
-
type MaskInstance = ReturnType<typeof createMaskInstance>;
|
|
9
|
-
|
|
10
|
-
function stubMaskInstance(maskFn: ReturnType<typeof vi.fn>): MaskInstance {
|
|
11
|
-
return { mask: maskFn } as unknown as MaskInstance;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
vi.mock('inputmask', () => ({
|
|
15
|
-
default: vi.fn((options) => ({
|
|
16
|
-
mask: vi.fn(),
|
|
17
|
-
options,
|
|
18
|
-
})),
|
|
19
|
-
}));
|
|
20
|
-
|
|
21
|
-
describe('maskEngine', () => {
|
|
22
|
-
beforeEach(() => {
|
|
23
|
-
vi.clearAllMocks();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
describe('createMaskInstance', () => {
|
|
27
|
-
it('creates mask instance with string mask', () => {
|
|
28
|
-
const instance = createMaskInstance('999-999');
|
|
29
|
-
expect(inputmask).toHaveBeenCalled();
|
|
30
|
-
expect(instance).toBeDefined();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('creates mask instance with alias', () => {
|
|
34
|
-
const instance = createMaskInstance('cpf');
|
|
35
|
-
expect(inputmask).toHaveBeenCalled();
|
|
36
|
-
expect(instance).toBeDefined();
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('creates mask instance with options', () => {
|
|
40
|
-
const instance = createMaskInstance('999-999', { placeholder: '_' });
|
|
41
|
-
expect(inputmask).toHaveBeenCalled();
|
|
42
|
-
expect(instance).toBeDefined();
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('creates mask instance with array mask', () => {
|
|
46
|
-
const instance = createMaskInstance(['999-999', '9999-9999']);
|
|
47
|
-
expect(inputmask).toHaveBeenCalled();
|
|
48
|
-
expect(instance).toBeDefined();
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
describe('applyMaskToElement', () => {
|
|
53
|
-
it('applies mask to input element', () => {
|
|
54
|
-
const input = document.createElement('input');
|
|
55
|
-
const maskFn = vi.fn();
|
|
56
|
-
vi.mocked(inputmask).mockImplementation(() => stubMaskInstance(maskFn));
|
|
57
|
-
|
|
58
|
-
applyMaskToElement(input, '999-999');
|
|
59
|
-
|
|
60
|
-
expect(maskFn).toHaveBeenCalledWith(input);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('applies mask to textarea element', () => {
|
|
64
|
-
const textarea = document.createElement('textarea');
|
|
65
|
-
const maskFn = vi.fn();
|
|
66
|
-
vi.mocked(inputmask).mockImplementation(() => stubMaskInstance(maskFn));
|
|
67
|
-
|
|
68
|
-
applyMaskToElement(textarea, '999-999');
|
|
69
|
-
|
|
70
|
-
expect(maskFn).toHaveBeenCalledWith(textarea);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('finds and applies mask to input inside wrapper', () => {
|
|
74
|
-
const wrapper = document.createElement('div');
|
|
75
|
-
const input = document.createElement('input');
|
|
76
|
-
wrapper.appendChild(input);
|
|
77
|
-
const maskFn = vi.fn();
|
|
78
|
-
vi.mocked(inputmask).mockImplementation(() => stubMaskInstance(maskFn));
|
|
79
|
-
|
|
80
|
-
applyMaskToElement(wrapper, '999-999');
|
|
81
|
-
|
|
82
|
-
expect(maskFn).toHaveBeenCalledWith(input);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('applies mask to wrapper if no input found inside', () => {
|
|
86
|
-
const wrapper = document.createElement('div');
|
|
87
|
-
const maskFn = vi.fn();
|
|
88
|
-
vi.mocked(inputmask).mockImplementation(() => stubMaskInstance(maskFn));
|
|
89
|
-
|
|
90
|
-
applyMaskToElement(wrapper, '999-999');
|
|
91
|
-
|
|
92
|
-
expect(maskFn).toHaveBeenCalledWith(wrapper);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('does nothing if element is null', () => {
|
|
96
|
-
const maskFn = vi.fn();
|
|
97
|
-
vi.mocked(inputmask).mockImplementation(() => stubMaskInstance(maskFn));
|
|
98
|
-
|
|
99
|
-
applyMaskToElement(null as unknown as HTMLElement, '999-999');
|
|
100
|
-
|
|
101
|
-
expect(maskFn).not.toHaveBeenCalled();
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it('applies mask with custom options', () => {
|
|
105
|
-
const input = document.createElement('input');
|
|
106
|
-
const maskFn = vi.fn();
|
|
107
|
-
vi.mocked(inputmask).mockImplementation(() => stubMaskInstance(maskFn));
|
|
108
|
-
|
|
109
|
-
applyMaskToElement(input, '999-999', { placeholder: '_' });
|
|
110
|
-
|
|
111
|
-
expect(maskFn).toHaveBeenCalledWith(input);
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
});
|