-
Notifications
You must be signed in to change notification settings - Fork 25
/
Radiobox.js
215 lines (193 loc) · 7.41 KB
/
Radiobox.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
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 Radiobox, { Option, isValidValue } from '../js/Inputs/Radiobox.tsx';
configure({ adapter: new Adapter() });
const WRAPPER = `.${WRAPPER_CLASS_IDENTITIFIER}`;
const OPTION_LIST = [{ id: 'engineer', name: 'engineer' }, { id: 'teacher', name: 'teacher' }, { id: 'student', name: 'student' }];
describe('Radiobox component', () => {
it('[Toggling "validate"]: Should show msgHtml(err) when toggling "validate"', () => {
const wrapper = mount(<Radiobox 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(<Radiobox 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(
<Radiobox
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('[validationCallback]: Should call validationCallback', () => {
let valid = false;
const wrapper = mount(
<Radiobox
optionList={OPTION_LIST}
onBlur={() => {}}
validationCallback={res => {
valid = res;
}}
/>,
);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
$wrapper.simulate('blur');
expect(valid).toEqual(true);
});
it('[<Option/>]: Should not render <Option/>', () => {
const id = `react-inputs-validation__radiobox_option-${OPTION_LIST[0].id}`;
const wrapper = mount(<Option />);
expect(wrapper.find(`#${id}`).hostNodes().length).toEqual(0);
});
it('[disabled]: Should msgHtml not be appeared when disabled', () => {
const wrapper = mount(<Radiobox optionList={OPTION_LIST} onBlur={() => {}} disabled={true} />);
wrapper
.find(`#react-inputs-validation__radiobox_option-${OPTION_LIST[1].id}`)
.hostNodes()
.simulate('change');
expect(wrapper.find(`.${MSG_CLASS_IDENTITIFIER}`).length).toEqual(0);
});
it("[onClick]: Should call parent's onClick", () => {
let value = '';
const wrapper = mount(
<Radiobox
onClick={() => {
value = 'clicked';
}}
/>,
);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
expect(value).toEqual('clicked');
});
it("[onFocus]: Should call parent's onFocus", () => {
let value = '';
const wrapper = mount(
<Radiobox
onFocus={() => {
value = 'focused';
}}
/>,
);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('focus');
expect(value).toEqual('focused');
});
it("[onBlur]: Should not show msgHtml if it's not provide", () => {
const wrapper = mount(<Radiobox />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('focus');
$wrapper.simulate('blur');
expect(wrapper.find(`.${MSG_CLASS_IDENTITIFIER}`).length).toEqual(0);
});
it('[validationOption.check]: Should msgHtml not be appeared when check is false', () => {
const wrapper = mount(<Radiobox 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('[validationOption.required]: Should msgHtml not be appeared when required is false', () => {
const wrapper = mount(<Radiobox onBlur={() => {}} validationOption={{ check: true, required: false }} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('focus');
$wrapper.simulate('blur');
expect(wrapper.find(`.${MSG_CLASS_IDENTITIFIER}`).length).toEqual(0);
});
it('[isValidValue]: Should retrun true', () => {
chaiExpect(isValidValue(OPTION_LIST, OPTION_LIST[0].id)).equal(true);
});
it('[isValidValue]: Should retrun false', () => {
chaiExpect(isValidValue(OPTION_LIST, 'foo')).equal(false);
});
it('[isValidValue]: Should retrun false', () => {
chaiExpect(isValidValue([], 'foo')).equal(false);
});
it('[Change value]: Should change the value when click the option', () => {
let value = '';
const wrapper = mount(
<Radiobox
optionList={OPTION_LIST}
onBlur={() => {}}
validate={false}
onChange={res => {
value = res;
}}
/>,
);
wrapper
.find(`#react-inputs-validation__radiobox_option-${OPTION_LIST[1].id}`)
.hostNodes()
.simulate('change');
expect(value).toEqual(OPTION_LIST[1].id);
});
it('[asyncObj]: Should show error', () => {
const wrapper = mount(<Radiobox onBlur={() => {}} 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(<Radiobox 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(<Radiobox onBlur={() => {}} 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(<Radiobox optionList={OPTION_LIST} onBlur={() => {}} validationOption={{ locale: 'foobar' }} />);
const $wrapper = wrapper.find(WRAPPER);
$wrapper.simulate('click');
$wrapper.simulate('blur');
expect(console.error).toHaveBeenCalled();
restoreConsole();
});
});