Skip to content

Commit

Permalink
test(countdown): enhance test case
Browse files Browse the repository at this point in the history
  • Loading branch information
BeADre committed Jul 4, 2022
1 parent 90800e1 commit f2ede3b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 99 deletions.

This file was deleted.

164 changes: 91 additions & 73 deletions packages/varlet-ui/src/countdown/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,88 @@
import example from '../example'
import Countdown from '..'
import VarCountdown from '../Countdown'
import { mount } from '@vue/test-utils'
import { createApp } from 'vue'
import { delay, mockConsole } from '../../utils/jest'

test('test countdown example', () => {
const { mockRestore } = mockConsole('log')
const wrapper = mount(example)
expect(wrapper.html()).toMatchSnapshot()

mockRestore()
})
import { delay } from '../../utils/jest'

test('test countdown plugin', () => {
const app = createApp({}).use(Countdown)
expect(app.component(Countdown.name)).toBeTruthy()
})

test('test countdown format prop', () => {
const wrapper = mount(VarCountdown, {
props: {
time: 108000000,
autoStart: false,
format: 'HH : mm : ss : SS',
},
})
describe('test countdown props', () => {
test('test format prop', () => {
const wrapper = mount(VarCountdown, {
props: {
time: 108000000,
format: 'HH-mm-ss-SS',
},
})

expect(wrapper.html()).toMatchSnapshot()
})
const reg = /(\d{2}-){3}\d{2}/
expect(reg.test(wrapper.text())).toBe(true)

test('test countdown autostart prop', async () => {
const wrapper = mount(VarCountdown, {
props: {
time: 10800,
autoStart: false,
},
wrapper.unmount()
})

const text = wrapper.text()
test('test autostart prop', async () => {
const wrapper = mount(VarCountdown, {
props: {
time: 10800,
autoStart: false,
},
})

const text = wrapper.text()

await delay(100)
await delay(100)
expect(wrapper.text()).toBe(text)

expect(wrapper.text()).toBe(text)
wrapper.unmount()
})
})

test('test countdown onEnd and onChange', async () => {
describe('test countdown events', () => {
const onEnd = jest.fn()
const onChange = jest.fn()

mount(VarCountdown, {
props: {
time: 1000,
onEnd,
onChange,
},
test('test onChange event', async () => {
const wrapper = mount(VarCountdown, {
props: {
time: 1,
onChange,
},
})

await delay(10)
expect(onChange).toHaveBeenCalled()
expect(onChange.mock.calls[0][0]).toEqual({
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 1,
})

wrapper.unmount()
})

await delay(1100)
test('test onEnd event', async () => {
const wrapper = mount(VarCountdown, {
props: {
time: 1,
onEnd,
},
})

expect(onEnd).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalled()
await delay(10)
expect(onEnd).toBeCalledTimes(1)

wrapper.unmount()
})
})

test('test start, pause and reset methods', async () => {
const template = `<var-countdown :time="time" ref="countdown" :auto-start="false" />`
const wrapper = mount({
describe('test countdown methods', () => {
const Wrapper = {
components: {
[VarCountdown.name]: VarCountdown,
},
Expand All @@ -74,52 +91,53 @@ test('test start, pause and reset methods', async () => {
time: 108000,
}
},
template,
})
template: `
<var-countdown :time="time" ref="countdown" :auto-start="false"/>`,
}

const text = wrapper.text()
test('test countdown start method', async () => {
const wrapper = mount(Wrapper)
const text = wrapper.text()

await delay(100)
await delay(50)
expect(wrapper.text()).toBe(text)

expect(wrapper.text()).toBe(text)
wrapper.vm.$refs.countdown.start()

wrapper.vm.$refs.countdown.start()
await delay(100)
expect(wrapper.text()).not.toBe(text)

await delay(100)
wrapper.unmount()
})

expect(wrapper.text()).not.toBe(text)
test('test countdown start method', async () => {
const wrapper = mount(Wrapper)
wrapper.vm.$refs.countdown.start()

wrapper.vm.$refs.countdown.pause()
const text1 = wrapper.text()
await delay(50)

await delay(100)
wrapper.vm.$refs.countdown.pause()
const text = wrapper.text()

expect(wrapper.text()).toBe(text1)
await delay(50)
expect(wrapper.text()).toBe(text)

wrapper.vm.$refs.countdown.reset()
wrapper.unmount()
})

await delay(500)
test('test countdown reset method', async () => {
const wrapper = mount(Wrapper)
const text = wrapper.text()

expect(wrapper.text()).toBe(text)
})
wrapper.vm.$refs.countdown.start()

test('test change event argument', async () => {
const onChange = jest.fn()
await delay(50)

mount(VarCountdown, {
props: {
time: 1,
onChange,
},
})
wrapper.vm.$refs.countdown.reset()

await delay(50)
await delay(50)
expect(wrapper.text()).toBe(text)

expect(onChange.mock.calls[0][0]).toEqual({
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 1,
wrapper.unmount()
})
})

0 comments on commit f2ede3b

Please sign in to comment.