-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvider.test.tsx
188 lines (161 loc) · 6.05 KB
/
Provider.test.tsx
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
import React from 'react';
import { mount } from 'enzyme';
import {
Validator,
ValidatorProvider,
ValidatorProviderProps,
ValidatorArea
} from '../../src';
import tick from '../common/tick';
describe('test ValidatorProvider', () => {
beforeEach(() => {
Validator.extend('passes_not', {
passed(): boolean {
return false;
},
message(): string {
return 'not passed';
}
});
Validator.extend('passes', {
passed(): boolean {
return true;
},
message(): string {
return 'passed';
}
});
});
it('should render ValidatorProvider', () => {
const provider = mount<ValidatorProvider, ValidatorProviderProps>(
<ValidatorProvider rules="passes_not" />
);
expect(provider.instance().props.rules).toBeDefined();
});
it('should throw error when area with existing name is added', () => {
const provider = () => {
mount<ValidatorProvider, ValidatorProviderProps>(
<ValidatorProvider>
<ValidatorArea name="test">
<div />
</ValidatorArea>
<ValidatorArea name="test">
<div />
</ValidatorArea>
</ValidatorProvider>
);
}
expect(() => provider()).toThrow('Validation area names should be unique');
});
it('should render with function as child', () => {
const provider = mount<ValidatorProvider, ValidatorProviderProps>(
<ValidatorProvider>
{() => <div>test</div>}
</ValidatorProvider>
);
expect(provider.find('div').text()).toBe('test');
});
it('should add an area when provided as child', () => {
const provider = mount<ValidatorProvider, ValidatorProviderProps>(
<ValidatorProvider>
<ValidatorArea name="test">
<div />
</ValidatorArea>
</ValidatorProvider>
);
const areas = provider.state().areas;
expect(areas.test).toBeDefined();
expect(areas.test).toBeInstanceOf(ValidatorArea);
})
it('should not call callback when areas dirty', async () => {
const mockFn = jest.fn();
const provider = mount<ValidatorProvider, ValidatorProviderProps>(
<ValidatorProvider>
{({ validate }) => (
<>
<ValidatorArea rules="passes_not" name="test1">
<input value="test" />
</ValidatorArea>
<ValidatorArea rules="passes_not" name="test2">
<input value="test" />
</ValidatorArea>
<button onClick={() => validate(mockFn)} />
</>
)}
</ValidatorProvider>
);
provider.find('button').simulate('click');
await tick();
expect(mockFn).not.toHaveBeenCalled()
})
it('should call callback when areas not dirty', async () => {
const mockFn = jest.fn();
const provider = mount<ValidatorProvider, ValidatorProviderProps>(
<ValidatorProvider>
{({ validate }) => (
<>
<ValidatorArea rules="passes" name="test1">
<input value="foo" />
</ValidatorArea>
<button onClick={() => validate(mockFn)} />
</>
)}
</ValidatorProvider>
);
provider.find('button').simulate('click');
await tick();
expect(mockFn).toHaveBeenCalled()
})
it('should just validate when not rules prop is given', async () => {
const mockFn = jest.fn();
const provider = mount<ValidatorProvider, ValidatorProviderProps>(
<ValidatorProvider>
{({ validate }) => (
<>
<ValidatorArea name="test1">
<input value="" />
</ValidatorArea>
<button onClick={() => validate(mockFn)} />
</>
)}
</ValidatorProvider>
);
provider.find('button').simulate('click');
await tick();
expect(mockFn).toHaveBeenCalled()
});
it('should set errors in areas from props and change over time', () => {
const provider = mount<ValidatorProvider, ValidatorProviderProps>(
<ValidatorProvider errors={{ test: ['test error']}}>
<ValidatorArea name="test">
<input value="" />
</ValidatorArea>
</ValidatorProvider>
);
expect(provider.find(ValidatorArea).state().errors.length).toBe(1);
provider.setProps({ errors: { test: ['test error 2'] } });
expect(provider.find(ValidatorArea).state().errors.length).toBe(2);
});
it('should set errors in areas from props', () => {
const provider = mount<ValidatorProvider, ValidatorProviderProps>(
<ValidatorProvider>
<ValidatorArea name="test">
<input value="" />
</ValidatorArea>
</ValidatorProvider>
);
provider.setProps({ errors: { test: ['test error 2'] } });
expect(provider.find(ValidatorArea).state().errors.length).toBe(1);
});
it('should ignore errors where no area is for', () => {
const provider = mount<ValidatorProvider, ValidatorProviderProps>(
<ValidatorProvider>
<ValidatorArea name="test">
<input value="" />
</ValidatorArea>
</ValidatorProvider>
);
provider.setProps({ errors: { not_existing: ['test error 2'] } });
expect(provider.find(ValidatorArea).state().errors.length).toBe(0);
});
})