Skip to content

Commit a8dcabb

Browse files
committed
test: add date-picker
1 parent 264abff commit a8dcabb

12 files changed

+1783
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import { mount } from '@vue/test-utils'
2+
import { asyncExpect } from '@/tests/utils'
3+
import moment from 'moment'
4+
import MockDate from 'mockdate'
5+
import DatePicker from '..'
6+
import {
7+
selectDateFromBody,
8+
openPanel,
9+
clearInput,
10+
nextYear,
11+
nextMonth,
12+
hasSelected,
13+
$$,
14+
} from './utils'
15+
import focusTest from '../../../tests/shared/focusTest'
16+
17+
describe('DatePicker', () => {
18+
focusTest(DatePicker)
19+
20+
beforeEach(() => {
21+
document.body.outerHTML = ''
22+
MockDate.set(moment('2016-11-22'))
23+
})
24+
25+
afterEach(() => {
26+
MockDate.reset()
27+
})
28+
29+
it('prop locale should works', async () => {
30+
const locale = {
31+
lang: {
32+
placeholder: 'Избери дата',
33+
rangePlaceholder: [
34+
'Начална дата',
35+
'Крайна дата',
36+
],
37+
today: 'Днес',
38+
now: 'Сега',
39+
backToToday: 'Към днес',
40+
ok: 'Добре',
41+
clear: 'Изчистване',
42+
month: 'Месец',
43+
year: 'Година',
44+
timeSelect: 'Избор на час',
45+
dateSelect: 'Избор на дата',
46+
monthSelect: 'Избор на месец',
47+
yearSelect: 'Избор на година',
48+
decadeSelect: 'Десетилетие',
49+
previousMonth: 'Предишен месец (PageUp)',
50+
nextMonth: 'Следващ месец (PageDown)',
51+
previousYear: 'Последна година (Control + left)',
52+
nextYear: 'Следваща година (Control + right)',
53+
previousDecade: 'Предишно десетилетие',
54+
nextDecade: 'Следващо десетилетие',
55+
previousCentury: 'Последен век',
56+
nextCentury: 'Следващ век',
57+
yearFormat: 'YYYY',
58+
dateFormat: 'D M YYYY',
59+
dayFormat: 'D',
60+
dateTimeFormat: 'D M YYYY HH:mm:ss',
61+
monthBeforeYear: true,
62+
},
63+
timePickerLocale: {
64+
placeholder: 'Избор на час',
65+
},
66+
}
67+
const birthday = moment('2000-01-01', 'YYYY-MM-DD')
68+
const wrapper = mount({
69+
render () {
70+
return <DatePicker open locale={locale} value={birthday} />
71+
},
72+
})
73+
await asyncExpect(() => {
74+
expect(wrapper.html()).toMatchSnapshot()
75+
})
76+
})
77+
78+
// Fix https://github.com/ant-design/ant-design/issues/8885
79+
it('control value after panel closed', async () => {
80+
const Test = {
81+
data () {
82+
return {
83+
cleared: false,
84+
value: moment(),
85+
}
86+
},
87+
methods: {
88+
onChange (value) {
89+
let cleared = this.cleared
90+
91+
if (cleared) {
92+
value = moment(moment(value).format('YYYY-MM-DD 12:12:12'))
93+
cleared = false
94+
}
95+
96+
if (!value) {
97+
cleared = true
98+
}
99+
this.value = value
100+
this.cleared = cleared
101+
},
102+
},
103+
render () {
104+
return (
105+
<DatePicker
106+
showTime
107+
value={this.value}
108+
format='YYYY-MM-DD HH:mm:ss'
109+
onChange={this.onChange}
110+
/>
111+
)
112+
},
113+
}
114+
115+
const wrapper = mount(Test, { sync: false, attachToDocument: true })
116+
await asyncExpect(() => {
117+
// clear input
118+
clearInput(wrapper)
119+
})
120+
await asyncExpect(() => {
121+
openPanel(wrapper)
122+
})
123+
await asyncExpect(() => {
124+
selectDateFromBody(moment('2016-11-13'))
125+
}, 0)
126+
await asyncExpect(() => {
127+
expect($$('.ant-calendar-input')[0].value).toBe('2016-11-13 12:12:12')
128+
})
129+
await asyncExpect(() => {
130+
selectDateFromBody(moment('2016-11-14'))
131+
})
132+
await asyncExpect(() => {
133+
expect($$('.ant-calendar-input')[0].value).toBe('2016-11-14 12:12:12')
134+
})
135+
await asyncExpect(() => {
136+
137+
})
138+
})
139+
140+
it('triggers onChange only when date was selected', async () => {
141+
const handleChange = jest.fn()
142+
const wrapper = mount({
143+
render () {
144+
return <DatePicker onChange={handleChange} />
145+
},
146+
}, { sync: false, attachToDocument: true })
147+
await asyncExpect(() => {
148+
openPanel(wrapper)
149+
})
150+
await asyncExpect(() => {
151+
nextYear()
152+
}, 0)
153+
await asyncExpect(() => {
154+
expect(handleChange).not.toBeCalled()
155+
})
156+
await asyncExpect(() => {
157+
nextMonth()
158+
})
159+
await asyncExpect(() => {
160+
expect(handleChange).not.toBeCalled()
161+
})
162+
await asyncExpect(() => {
163+
selectDateFromBody(moment('2017-12-22'))
164+
})
165+
await asyncExpect(() => {
166+
expect(handleChange).toBeCalled()
167+
})
168+
await asyncExpect(() => {
169+
170+
})
171+
})
172+
173+
it('clear input', async () => {
174+
const wrapper = mount(DatePicker, { sync: false, attachToDocument: true })
175+
await asyncExpect(() => {
176+
openPanel(wrapper)
177+
})
178+
await asyncExpect(() => {
179+
selectDateFromBody(moment('2016-11-23'))
180+
}, 0)
181+
await asyncExpect(() => {
182+
clearInput(wrapper)
183+
})
184+
await asyncExpect(() => {
185+
openPanel(wrapper)
186+
})
187+
await asyncExpect(() => {
188+
expect(hasSelected(wrapper, moment('2016-11-22'))).toBe(true)
189+
}, 0)
190+
})
191+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import DatePicker from '..'
2+
import focusTest from '../../../tests/shared/focusTest'
3+
4+
const { MonthPicker } = DatePicker
5+
6+
describe('MonthPicker', () => {
7+
focusTest(MonthPicker)
8+
})

0 commit comments

Comments
 (0)