This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
/
useValidation.test.ts
198 lines (161 loc) · 6.07 KB
/
useValidation.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { ValidationRuleSet, VALIDATORS } from './types';
import useValidation from './useValidation';
import createProcessor from './processor';
import Validity from './validity';
import {
createDefaultValidity,
processFormValidity,
processFieldValidity,
validateRuleSet,
} from './helpers';
import { renderHook, act } from '@testing-library/react-hooks';
jest.mock('./processor');
jest.mock('./helpers');
const validRuleSet: ValidationRuleSet = [{
field: 'field_name',
rules: [{ validator: VALIDATORS.required }],
}];
const defaultValidity = new Validity([{
name: 'field',
index: 0,
pristine: true,
error: false,
}]);
const newValidity = new Validity([{
name: 'field',
index: 0,
pristine: false,
error: false,
}]);
const processor = jest.fn().mockName('processor');
beforeEach(() => {
jest.mocked(createProcessor).mockName('createProcessor').mockReturnValue(processor);
jest.mocked(createDefaultValidity).mockName('createDefaultValidity').mockReturnValue(defaultValidity);
jest.mocked(processFormValidity).mockName('processFormValidity').mockResolvedValue(newValidity);
jest.mocked(processFieldValidity).mockName('processFieldValidity').mockResolvedValue(newValidity);
jest.mocked(validateRuleSet).mockName('validateRuleSet').mockReturnValue();
});
function runHook() {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { result } = renderHook(() => useValidation(validRuleSet));
return result;
}
describe('Hook throws an error', () => {
test('no ruleset', () => {
jest.mocked(validateRuleSet).mockName('validateRuleSet').mockImplementation(() => {
throw new Error('RuleSet checker throws an error');
});
expect(() => useValidation([])).toThrow(new Error('RuleSet checker throws an error'));
});
});
test('Keeps default validity initially as a state', () => {
const hook = runHook();
expect(hook.current.validity).toBe(defaultValidity);
expect(createProcessor).toBeCalledTimes(1);
expect(createProcessor).toBeCalledWith(validRuleSet);
expect(createDefaultValidity).toBeCalledTimes(1);
expect(createDefaultValidity).toBeCalledWith(validRuleSet);
});
describe('validateForm', () => {
test('Throws an exception if no payload', async () => {
const hook = runHook();
await act(async () => {
await expect(hook.current.validateForm()).rejects.toThrow(
new Error('You have to pass a form payload object to validate the form.')
);
});
expect(createProcessor).toBeCalledTimes(1);
expect(createProcessor).toBeCalledWith(validRuleSet);
expect(createDefaultValidity).toBeCalledTimes(1);
expect(createDefaultValidity).toBeCalledWith(validRuleSet);
});
test('Returns new validity and changes state', async () => {
const hook = runHook();
await act(async () => {
const result = await hook.current.validateForm({});
expect(result).toBe(newValidity);
});
expect(createProcessor).toBeCalledTimes(1);
expect(createProcessor).toBeCalledWith(validRuleSet);
expect(createDefaultValidity).toBeCalledTimes(1);
expect(createDefaultValidity).toBeCalledWith(validRuleSet);
expect(processFormValidity).toBeCalledTimes(1);
expect(processFormValidity).toBeCalledWith(processor, defaultValidity, {});
expect(hook.current.validity).toBe(newValidity);
});
});
describe('validateField', () => {
test('Throws an exception if no payload', async () => {
const hook = runHook();
await act(async () => {
await expect(hook.current.validateField()).rejects.toThrow(
new Error('You have to pass a form payload object to validate the field.')
);
});
expect(createProcessor).toBeCalledTimes(1);
expect(createProcessor).toBeCalledWith(validRuleSet);
expect(createDefaultValidity).toBeCalledTimes(1);
expect(createDefaultValidity).toBeCalledWith(validRuleSet);
});
test('Throws an exception if no field name', async () => {
const hook = runHook();
await act(async () => {
await expect(hook.current.validateField({})).rejects.toThrow(
new Error('You have to pass a field name to validate the field.')
);
});
expect(createProcessor).toBeCalledTimes(1);
expect(createProcessor).toBeCalledWith(validRuleSet);
expect(createDefaultValidity).toBeCalledTimes(1);
expect(createDefaultValidity).toBeCalledWith(validRuleSet);
});
test('Throws an exception if field name is not a string', async () => {
const hook = runHook();
await act(async () => {
await expect(hook.current.validateField({}, [])).rejects.toThrow(
new Error('You have to pass a field name to validate the field.')
);
});
expect(createProcessor).toBeCalledTimes(1);
expect(createProcessor).toBeCalledWith(validRuleSet);
expect(createDefaultValidity).toBeCalledTimes(1);
expect(createDefaultValidity).toBeCalledWith(validRuleSet);
});
test('Throws an exception if field name is an empty string', async () => {
const hook = runHook();
await act(async () => {
await expect(hook.current.validateField({}, ' ')).rejects.toThrow(
new Error('You have to pass a field name to validate the field.')
);
});
expect(createProcessor).toBeCalledTimes(1);
expect(createProcessor).toBeCalledWith(validRuleSet);
expect(createDefaultValidity).toBeCalledTimes(1);
expect(createDefaultValidity).toBeCalledWith(validRuleSet);
});
test('Returns new validity and changes state', async () => {
const hook = runHook();
await act(async () => {
const result = await hook.current.validateField({}, 'field');
expect(result).toBe(newValidity);
});
expect(createProcessor).toBeCalledTimes(1);
expect(createProcessor).toBeCalledWith(validRuleSet);
expect(createDefaultValidity).toBeCalledTimes(1);
expect(createDefaultValidity).toBeCalledWith(validRuleSet);
expect(processFieldValidity).toBeCalledTimes(1);
expect(processFieldValidity).toBeCalledWith(processor, defaultValidity, {}, 'field', 0);
expect(hook.current.validity).toBe(newValidity);
});
});
test('resetForm', async () => {
const hook = runHook();
await act(async () => {
const validity = await hook.current.validateForm({ fieldname: [''] });
expect(validity).toBe(newValidity);
});
act(() => {
const validity = hook.current.resetForm();
expect(validity).toBe(defaultValidity);
});
});