-
Notifications
You must be signed in to change notification settings - Fork 25
/
Select.js
251 lines (226 loc) · 9.34 KB
/
Select.js
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import React from 'react';
import { expect as chaiExpect } from 'chai';
import { configure, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { WRAPPER_CLASS_IDENTITIFIER, MSG_CLASS_IDENTITIFIER } from '../js/Inputs/const.ts';
import mockConsole from 'jest-mock-console';
import Select, { isValidValue, getIndex, Option } from '../js/Inputs/Select.tsx';
configure({ adapter: new Adapter() });
const WRAPPER = `.${WRAPPER_CLASS_IDENTITIFIER}`;
const OPTION_LIST = [{ id: '', name: 'Please select one country' }, { id: 'us', name: 'US' }, { id: 'ca', name: 'CA' }, { id: 'uk', name: 'UK' }, { id: 'fr', name: 'France' }];
describe('Select component', () => {
it('[Toggling "validate"]: Should show msgHtml(err) when toggling "validate"', () => {
const wrapper = mount(<Select optionList={OPTION_LIST} onBlur={() => {}} validate={false} />);
wrapper.setProps({ validate: true });
expect(wrapper.update().find(`.${MSG_CLASS_IDENTITIFIER}`).length).toEqual(1);
});
it('[Providing msgOnError]: Should msg be msgOnError', () => {
const msgOnError = 'msgOnError';
const wrapper = mount(<Select optionList={OPTION_LIST} onBlur={() => {}} validationOption={{ msgOnError }} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
$wrapper.simulate('blur');
expect(wrapper.find(`.${MSG_CLASS_IDENTITIFIER}`).text()).toEqual(msgOnError);
});
it('[successMsg]: Should show successMsg when msgOnSuccess is provided', () => {
const msgOnSuccess = 'msgOnSuccess';
const wrapper = mount(
<Select
optionList={OPTION_LIST}
value={OPTION_LIST[2].id}
onBlur={() => {}}
onChange={() => {}}
validationOption={{
name: 'foobar',
check: true,
required: true,
showMsg: true,
msgOnSuccess,
}}
/>,
);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
$wrapper.simulate('blur');
expect(wrapper.find(`.${MSG_CLASS_IDENTITIFIER}`).text()).toEqual(msgOnSuccess);
});
it('[Change props value]: Should check immediately when new props value is not equal to internal value', () => {
const wrapper = mount(<Select optionList={OPTION_LIST} value={OPTION_LIST[2].id} onBlur={() => {}} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
$wrapper.simulate('blur');
wrapper.setProps({ value: '' });
expect(wrapper.update().find(`.${MSG_CLASS_IDENTITIFIER}`).length).toEqual(1);
});
it('[disabled]: Should msgHtml not be appeared when disabled', () => {
const wrapper = mount(<Select optionList={OPTION_LIST} onBlur={() => {}} disabled={true} />);
wrapper
.find(`#react-inputs-validation__select_option-${OPTION_LIST[1].id}`)
.hostNodes()
.simulate('click');
expect(wrapper.find(`.${MSG_CLASS_IDENTITIFIER}`).length).toEqual(0);
});
it('[isValidValue]: Should retrun true', () => {
chaiExpect(isValidValue(OPTION_LIST, '')).equal(true);
});
it('[isValidValue]: Should retrun false', () => {
chaiExpect(isValidValue(OPTION_LIST, 'foo')).equal(false);
});
it('[getIndex]: Should retrun 2', () => {
chaiExpect(getIndex(OPTION_LIST, 'ca')).equal(2);
});
it('[getIndex]: Should retrun -1', () => {
chaiExpect(getIndex(OPTION_LIST, 'foo')).equal(-1);
});
it("[onClick]: Should call parent's onClick", () => {
let value = '';
const wrapper = mount(
<Select
onClick={() => {
value = 'clicked';
}}
optionList={OPTION_LIST}
/>,
);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
expect(value).toEqual('clicked');
});
it("[onFocus]: Should call parent's onFocus", () => {
let value = '';
const wrapper = mount(
<Select
onFocus={() => {
value = 'focused';
}}
/>,
);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('focus');
expect(value).toEqual('focused');
});
it('[validationOption.check]: Should msgHtml not be appeared when check is false', () => {
const wrapper = mount(<Select onBlur={() => {}} validationOption={{ check: false }} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('focus');
$wrapper.simulate('blur');
expect(wrapper.find(`.${MSG_CLASS_IDENTITIFIER}`).length).toEqual(0);
});
it('[onClick]: Should choose item', () => {
let value = '';
const wrapper = mount(
<Select
value={value}
optionList={OPTION_LIST}
onChange={res => {
value = res.id;
}}
/>,
);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
const $el = wrapper.find(`#react-inputs-validation__select_option-${OPTION_LIST[2].id}`).hostNodes();
$el.simulate('click');
expect(value).toEqual(OPTION_LIST[2].id);
});
it("[onBlur]: Should not show msgHtml if it's not provide", () => {
const wrapper = mount(<Select />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('focus');
$wrapper.simulate('blur');
expect(wrapper.find(`.${MSG_CLASS_IDENTITIFIER}`).length).toEqual(0);
});
it('[onMouseOver]: Should option item add active class', () => {
const value = '';
const wrapper = mount(<Select value={value} optionList={OPTION_LIST} onChange={() => {}} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
const $el = wrapper.find(`#react-inputs-validation__select_option-${OPTION_LIST[2].id}`).hostNodes();
$el.simulate('mouseover');
expect($el.instance().className).toEqual('button select__options-item-show-cursor select__options-item false false false select__hover-active');
});
it('[onMouseMove]: Should option item remove active class', () => {
const value = '';
const wrapper = mount(<Select value={value} optionList={OPTION_LIST} onChange={() => {}} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
const $el = wrapper.find(`#react-inputs-validation__select_option-${OPTION_LIST[2].id}`).hostNodes();
$el.simulate('mouseover');
$el.simulate('mousemove');
expect($el.instance().className).toEqual('button select__options-item-show-cursor select__options-item false false false select__hover-active');
});
it('[onMouseMove]: Should option item remove active class', () => {
const value = '';
const wrapper = mount(<Select value={value} optionList={OPTION_LIST} onChange={() => {}} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
const $el = wrapper.find(`#react-inputs-validation__select_option-${OPTION_LIST[2].id}`).hostNodes();
$el.simulate('mouseover');
$el.simulate('mouseout');
expect($el.instance().className).toEqual('button select__options-item-show-cursor select__options-item false false false ');
});
it('[<Option/>]: Should not render <Option/>', () => {
const id = `react-inputs-validation__select_option-${OPTION_LIST[0].id}`;
const wrapper = mount(<Option />);
expect(wrapper.find(`#${id}`).hostNodes().length).toEqual(0);
});
it('[validationCallback]: Should call validationCallback', () => {
let valid = false;
const wrapper = mount(
<Select
optionList={OPTION_LIST}
onBlur={() => {}}
validationCallback={res => {
valid = res;
}}
/>,
);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
$wrapper.simulate('blur');
expect(valid).toEqual(true);
});
it('[asyncObj]: Should show error', () => {
const wrapper = mount(<Select onBlur={() => {}} optionList={OPTION_LIST} asyncMsgObj={{}} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('focus');
$wrapper.simulate('blur');
wrapper.setProps({ asyncMsgObj: { error: true, message: 'has error' } });
expect(
wrapper
.update()
.find(`.${MSG_CLASS_IDENTITIFIER}`)
.text(),
).toEqual('has error');
});
it('[asyncObj]: Should not show error', () => {
const wrapper = mount(<Select value={OPTION_LIST[1].id} optionList={OPTION_LIST} onBlur={() => {}} asyncMsgObj={{}} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('focus');
$wrapper.simulate('blur');
wrapper.setProps({ asyncMsgObj: { error: true, message: 'has error', showOnError: false } });
expect(wrapper.update().find(`.${MSG_CLASS_IDENTITIFIER}`).length).toEqual(0);
});
it('[asyncObj]: Should show success', () => {
const wrapper = mount(<Select onBlur={() => {}} optionList={OPTION_LIST} asyncMsgObj={{}} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('focus');
$wrapper.simulate('blur');
wrapper.setProps({ asyncMsgObj: { error: false, message: 'success', showOnSuccess: true } });
expect(
wrapper
.update()
.find(`.${MSG_CLASS_IDENTITIFIER}`)
.text(),
).toEqual('success');
});
it('[console.error REACT_INPUTS_VALIDATION_CUSTOM_ERROR_MESSAGE_EXAMPLE]: Should console.error REACT_INPUTS_VALIDATION_CUSTOM_ERROR_MESSAGE_EXAMPLE', () => {
const restoreConsole = mockConsole();
const wrapper = mount(<Select optionList={OPTION_LIST} onBlur={() => {}} validationOption={{ locale: 'foobar' }} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
$wrapper.simulate('blur');
expect(console.error).toHaveBeenCalled();
restoreConsole();
});
});