2020-09-16 14:49:47 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
|
|
|
import { nextTick } from 'vue'
|
|
|
|
import Calendar from '../src/index.vue'
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const _mount = (template: string, data?, otherObj?) =>
|
|
|
|
mount({
|
|
|
|
components: {
|
|
|
|
'el-calendar': Calendar,
|
|
|
|
},
|
|
|
|
template,
|
|
|
|
data,
|
|
|
|
...otherObj,
|
|
|
|
})
|
2020-09-16 14:49:47 +08:00
|
|
|
|
|
|
|
describe('Calendar.vue', () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
it('create', async () => {
|
|
|
|
const wrapper = _mount(
|
|
|
|
`
|
2020-09-16 14:49:47 +08:00
|
|
|
<el-calendar v-model="value"></el-calendar>
|
2021-09-04 19:29:28 +08:00
|
|
|
`,
|
|
|
|
() => ({ value: new Date('2019-04-01') })
|
|
|
|
)
|
2020-09-16 14:49:47 +08:00
|
|
|
const titleEl = wrapper.find('.el-calendar__title')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
/2019.*April/.test((titleEl.element as HTMLElement).innerHTML)
|
|
|
|
).toBeTruthy()
|
2020-09-16 14:49:47 +08:00
|
|
|
expect(wrapper.element.querySelectorAll('thead th').length).toBe(7)
|
|
|
|
const rows = wrapper.element.querySelectorAll('.el-calendar-table__row')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(rows.length).toBe(6)
|
|
|
|
;(rows[5].firstElementChild as HTMLElement).click()
|
2020-09-16 14:49:47 +08:00
|
|
|
|
|
|
|
await nextTick()
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
/2019.*May/.test((titleEl.element as HTMLElement).innerHTML)
|
|
|
|
).toBeTruthy()
|
2020-09-16 14:49:47 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const date = vm.value
|
|
|
|
expect(date.getFullYear()).toBe(2019)
|
|
|
|
expect(date.getMonth()).toBe(4)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
(wrapper.find('.is-selected span').element as HTMLElement).innerHTML
|
|
|
|
).toBe('5')
|
2020-09-16 14:49:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('range', () => {
|
|
|
|
const wrapper = _mount(`
|
|
|
|
<el-calendar :range="[new Date(2019, 2, 4), new Date(2019, 2, 24)]"></el-calendar>
|
|
|
|
`)
|
|
|
|
const titleEl = wrapper.find('.el-calendar__title')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
/2019.*March/.test((titleEl.element as HTMLElement).innerHTML)
|
|
|
|
).toBeTruthy()
|
2020-09-16 14:49:47 +08:00
|
|
|
const rows = wrapper.element.querySelectorAll('.el-calendar-table__row')
|
|
|
|
expect(rows.length).toBe(4)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
wrapper.element.querySelector('.el-calendar__button-group')
|
|
|
|
).toBeNull()
|
2020-09-16 14:49:47 +08:00
|
|
|
})
|
|
|
|
|
2021-09-03 09:59:45 +08:00
|
|
|
// https://github.com/element-plus/element-plus/issues/3155
|
|
|
|
it('range when the start date will be calculated to last month', () => {
|
|
|
|
const wrapper = _mount(`
|
|
|
|
<el-calendar :range="[new Date(2021, 1, 2), new Date(2021, 1, 28)]"></el-calendar>
|
|
|
|
`)
|
|
|
|
const titleEl = wrapper.find('.el-calendar__title')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
/2021.*January/.test((titleEl.element as HTMLElement).innerHTML)
|
|
|
|
).toBeTruthy()
|
2021-09-03 09:59:45 +08:00
|
|
|
const rows = wrapper.element.querySelectorAll('.el-calendar-table__row')
|
|
|
|
expect(rows.length).toBe(5)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
wrapper.element.querySelector('.el-calendar__button-group')
|
|
|
|
).toBeNull()
|
2021-09-03 09:59:45 +08:00
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('range tow monthes', async () => {
|
2020-09-16 14:49:47 +08:00
|
|
|
const wrapper = _mount(`
|
|
|
|
<el-calendar :range="[new Date(2019, 3, 14), new Date(2019, 4, 18)]"></el-calendar>
|
|
|
|
`)
|
|
|
|
const titleEl = wrapper.find('.el-calendar__title')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
/2019.*April/.test((titleEl.element as HTMLElement).innerHTML)
|
|
|
|
).toBeTruthy()
|
|
|
|
const dateTables = wrapper.element.querySelectorAll(
|
|
|
|
'.el-calendar-table.is-range'
|
|
|
|
)
|
2020-09-16 14:49:47 +08:00
|
|
|
expect(dateTables.length).toBe(2)
|
|
|
|
const rows = wrapper.element.querySelectorAll('.el-calendar-table__row')
|
|
|
|
expect(rows.length).toBe(5)
|
2021-09-04 19:29:28 +08:00
|
|
|
const cell = rows[rows.length - 1].firstElementChild
|
|
|
|
;(cell as HTMLElement).click()
|
2020-09-16 14:49:47 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
/2019.*May/.test((titleEl.element as HTMLElement).innerHTML)
|
|
|
|
).toBeTruthy()
|
2020-09-16 14:49:47 +08:00
|
|
|
expect(cell.classList.contains('is-selected')).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
2021-09-03 09:59:45 +08:00
|
|
|
// https://github.com/element-plus/element-plus/issues/3155
|
2021-09-04 19:29:28 +08:00
|
|
|
it('range tow monthes when the start date will be calculated to last month', async () => {
|
2021-09-03 09:59:45 +08:00
|
|
|
const wrapper = _mount(`
|
|
|
|
<el-calendar :range="[new Date(2021, 1, 2), new Date(2021, 2, 21)]"></el-calendar>
|
|
|
|
`)
|
|
|
|
const titleEl = wrapper.find('.el-calendar__title')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
/2021.*January/.test((titleEl.element as HTMLElement).innerHTML)
|
|
|
|
).toBeTruthy()
|
|
|
|
const dateTables = wrapper.element.querySelectorAll(
|
|
|
|
'.el-calendar-table.is-range'
|
|
|
|
)
|
2021-09-03 09:59:45 +08:00
|
|
|
expect(dateTables.length).toBe(3)
|
|
|
|
const rows = wrapper.element.querySelectorAll('.el-calendar-table__row')
|
|
|
|
expect(rows.length).toBe(8)
|
2021-09-04 19:29:28 +08:00
|
|
|
const cell = rows[rows.length - 1].firstElementChild
|
|
|
|
;(cell as HTMLElement).click()
|
2021-09-03 09:59:45 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
/2021.*March/.test((titleEl.element as HTMLElement).innerHTML)
|
|
|
|
).toBeTruthy()
|
2021-09-03 09:59:45 +08:00
|
|
|
expect(cell.classList.contains('is-selected')).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
2020-09-16 14:49:47 +08:00
|
|
|
it('firstDayOfWeek', async () => {
|
|
|
|
// default en locale, weekStart 0 Sunday
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`
|
2020-09-16 14:49:47 +08:00
|
|
|
<el-calendar v-model="value"></el-calendar>
|
2021-09-04 19:29:28 +08:00
|
|
|
`,
|
|
|
|
() => ({ value: new Date('2019-04-01') })
|
|
|
|
)
|
2020-09-16 14:49:47 +08:00
|
|
|
const head = wrapper.element.querySelector('.el-calendar-table thead')
|
|
|
|
expect((head.firstElementChild as HTMLElement).innerHTML).toBe('Sun')
|
|
|
|
expect((head.lastElementChild as HTMLElement).innerHTML).toBe('Sat')
|
|
|
|
const firstRow = wrapper.element.querySelector('.el-calendar-table__row')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect((firstRow.firstElementChild as HTMLElement).innerHTML).toContain(
|
|
|
|
'31'
|
|
|
|
)
|
2020-09-16 14:49:47 +08:00
|
|
|
expect((firstRow.lastElementChild as HTMLElement).innerHTML).toContain('6')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('firstDayOfWeek in range mode', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`
|
2020-09-16 14:49:47 +08:00
|
|
|
<el-calendar v-model="value" :first-day-of-week="7" :range="[new Date(2019, 1, 3), new Date(2019, 2, 23)]"></el-calendar>
|
2021-09-04 19:29:28 +08:00
|
|
|
`,
|
|
|
|
() => ({ value: new Date('2019-03-04') })
|
|
|
|
)
|
2020-09-16 14:49:47 +08:00
|
|
|
const head = wrapper.element.querySelector('.el-calendar-table thead')
|
|
|
|
expect((head.firstElementChild as HTMLElement).innerHTML).toBe('Sun')
|
|
|
|
expect((head.lastElementChild as HTMLElement).innerHTML).toBe('Sat')
|
|
|
|
const firstRow = wrapper.element.querySelector('.el-calendar-table__row')
|
|
|
|
expect((firstRow.firstElementChild as HTMLElement).innerHTML).toContain('3')
|
|
|
|
expect((firstRow.lastElementChild as HTMLElement).innerHTML).toContain('9')
|
|
|
|
})
|
|
|
|
})
|