-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathCalendarContainer.test.tsx
206 lines (173 loc) ยท 6.01 KB
/
CalendarContainer.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { mount, shallow, ShallowWrapper, ReactWrapper } from 'enzyme';
import * as dayjs from 'dayjs';
import * as React from 'react';
import * as sinon from 'sinon';
import 'dayjs/locale/en';
import 'dayjs/locale/ko';
import CalendarContainer, { Props, State } from '../src/components/CalendarContainer';
import { IDatePicker } from '../src/common/@types';
describe('<CalendarContainer/>', () => {
const current = dayjs(new Date(2018, 11, 5));
let base = current;
const setBase = (val: dayjs.Dayjs) => {
base = val;
};
const defaultProps = {
base,
setBase,
current,
};
let shallowComponent: ShallowWrapper<React.Component<Props>>;
let mountComponent: ReactWrapper<Props, State>;
describe('prop: show', () => {
beforeEach(() => {
shallowComponent = shallow(<CalendarContainer {...defaultProps} show />);
});
it('should render correctly', () => {
expect(shallowComponent).toBeTruthy();
expect(shallowComponent).toMatchSnapshot();
});
it('props show correctly', () => {
expect(shallowComponent.hasClass('calendar--show')).toBeTruthy();
});
});
describe('prop: locale', () => {
let en: ReactWrapper<Props>;
let ko: ReactWrapper<Props>;
beforeEach(() => {
en = mount(<CalendarContainer {...defaultProps} locale="en" />);
ko = mount(<CalendarContainer {...defaultProps} locale="ko" />);
});
it('should locale prop correctly', () => {
expect(ko).toMatchSnapshot();
expect(
en
.find('th')
.first()
.text()
).toEqual('Sun');
expect(
ko
.find('th')
.first()
.text()
).toEqual('์ผ');
});
});
describe('handle base test(onPrev, onNext)', () => {
beforeEach(() => {
mountComponent = mount(
<CalendarContainer {...defaultProps} prevIcon nextIcon showMonthCnt={1} />
);
});
it('should onPrev correctly', () => {
mountComponent.setState({
viewMode: IDatePicker.ViewMode.DAY,
});
mountComponent.find('.calendar__head--prev > button').simulate('click');
expect(dayjs(base).format('MM')).toEqual('11');
mountComponent.setState({
viewMode: IDatePicker.ViewMode.MONTH,
});
mountComponent.find('.calendar__head--prev > button').simulate('click');
expect(dayjs(base).format('YYYY')).toEqual('2017');
mountComponent.setState({
viewMode: IDatePicker.ViewMode.YEAR,
});
mountComponent.find('.calendar__head--prev > button').simulate('click');
expect(dayjs(base).format('YYYY')).toEqual('2008');
});
});
describe('handle today test', () => {
it('should handle today correctly', () => {
mountComponent = mount(<CalendarContainer {...defaultProps} showToday prevIcon nextIcon />);
mountComponent.setState({
viewMode: IDatePicker.ViewMode.DAY,
});
mountComponent.find('.calendar__head--prev > button').simulate('click');
mountComponent.find('.calendar__head--prev > button').simulate('click');
mountComponent.find('.calendar__panel--today h2').simulate('click');
expect(dayjs(base).format('YYYYMMDD')).toEqual(dayjs().format('YYYYMMDD'));
});
});
describe('handle title click test', () => {
it('should viewMode change correctly', () => {
mountComponent = mount(<CalendarContainer {...defaultProps} />);
// once click expect month
mountComponent.find('.calendar__head--title').simulate('click');
expect(mountComponent.state().viewMode).toEqual(IDatePicker.ViewMode.MONTH);
// twice click expect year
mountComponent.find('.calendar__head--title').simulate('click');
expect(mountComponent.state().viewMode).toEqual(IDatePicker.ViewMode.YEAR);
});
it('should showMontCnt > 1 viewMode noChange', () => {
mountComponent = mount(<CalendarContainer {...defaultProps} showMonthCnt={2} />);
// once click expect day(showMontCnt > 1)
mountComponent.find('.calendar__head--title').simulate('click');
expect(mountComponent.state().viewMode).toEqual(IDatePicker.ViewMode.DAY);
});
});
describe('handle change test', () => {
it('should showMontCnt > 1 onChange call', () => {
const onChange = sinon.spy();
mountComponent = mount(
<CalendarContainer
{...defaultProps}
base={dayjs(new Date(2018, 12, 4))}
showMonthCnt={2}
onChange={onChange}
/>
);
// empty date click
mountComponent
.find('td')
.at(1)
.simulate('click');
expect(onChange).toHaveProperty('callCount', 0);
mountComponent
.find('td')
.at(6)
.simulate('click');
expect(onChange).toHaveProperty('callCount', 1);
});
describe('Mode Specific test', () => {
let onChange: sinon.SinonSpy;
beforeEach(() => {
onChange = sinon.spy();
mountComponent = mount(<CalendarContainer {...defaultProps} onChange={onChange} />);
});
it('should year mode test', () => {
mountComponent.setState({
viewMode: IDatePicker.ViewMode.YEAR,
});
mountComponent
.find('td')
.at(6)
.simulate('click');
expect(dayjs(base).format('YYYY')).toEqual('2020');
expect(mountComponent.state().viewMode).toEqual(IDatePicker.ViewMode.MONTH);
});
it('should month mode test', () => {
mountComponent.setState({
viewMode: IDatePicker.ViewMode.MONTH,
});
mountComponent
.find('td')
.at(1)
.simulate('click');
expect(dayjs(base).format('MM')).toEqual('02');
expect(mountComponent.state().viewMode).toEqual(IDatePicker.ViewMode.DAY);
});
it('should day mode test', () => {
mountComponent.setState({
viewMode: IDatePicker.ViewMode.DAY,
});
mountComponent
.find('td')
.at(6)
.simulate('click');
expect(onChange).toHaveProperty('callCount', 1);
});
});
});
});