feat(rate): add test (#407)

This commit is contained in:
doom-9 2021-07-06 23:58:38 +08:00 committed by GitHub
parent 533f67c418
commit 5ea61d47e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 0 deletions

View File

@ -5,4 +5,78 @@ describe('n-rate', () => {
it('should work with import on demand', () => {
mount(NRate)
})
it('should work with `count` prop', async () => {
const wrapper = mount(NRate)
await wrapper.setProps({ count: 10 })
expect(wrapper.findAll('.n-rate__item').length).toBe(10)
wrapper.unmount()
})
it('should work with `value` prop', async () => {
const wrapper = mount(NRate)
await wrapper.setProps({ value: 3, count: 10 })
expect(wrapper.findAll('.n-rate__item--active').length).toBe(3)
await wrapper.setProps({ value: 10 })
expect(wrapper.findAll('.n-rate__item--active').length).toBe(10)
wrapper.unmount()
})
it('should work with `defaultValue` prop', async () => {
const wrapper = mount(NRate, { props: { defaultValue: 8, count: 10 } })
expect(wrapper.findAll('.n-rate__item--active').length).toBe(8)
wrapper.unmount()
})
it('should work with `size` prop', async () => {
const wrapper = mount(NRate)
await wrapper.setProps({ size: 20 })
expect(wrapper.find('.n-rate').attributes('style')).toContain(
'--item-size: 20px'
)
for (const size of ['small', 'medium', 'large'] as const) {
await wrapper.setProps({ size })
expect(wrapper.find('.n-rate').attributes('style')).toMatchSnapshot()
}
wrapper.unmount()
})
it('should work with `color` prop', async () => {
const wrapper = mount(NRate)
await wrapper.setProps({ color: '#4fb233' })
expect(wrapper.find('.n-rate').attributes('style')).toContain(
'--item-color-active: #4fb233'
)
wrapper.unmount()
})
it('should work with `onUpdateValue` prop', async () => {
const onUpdateValue = jest.fn()
const onUpdateValue2 = jest.fn()
const wrapper = mount(NRate)
const testNumber = 2
await wrapper.setProps({ onUpdateValue })
await wrapper.findAll('.n-rate__item')[testNumber].trigger('click')
expect(onUpdateValue).toHaveBeenCalledWith(testNumber + 1)
await wrapper.setProps({ onUpdateValue: [onUpdateValue, onUpdateValue2] })
await wrapper.findAll('.n-rate__item')[testNumber].trigger('click')
expect(onUpdateValue).toHaveBeenCalledWith(testNumber + 1)
expect(onUpdateValue2).toHaveBeenCalledWith(testNumber + 1)
wrapper.unmount()
})
})

View File

@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`n-rate should work with \`size\` prop 1`] = `"--bezier: cubic-bezier(.4, 0, .2, 1); --item-color: rgb(219, 219, 223); --item-color-active: #FFCC33; --item-size: 16px;"`;
exports[`n-rate should work with \`size\` prop 2`] = `"--bezier: cubic-bezier(.4, 0, .2, 1); --item-color: rgb(219, 219, 223); --item-color-active: #FFCC33; --item-size: 20px;"`;
exports[`n-rate should work with \`size\` prop 3`] = `"--bezier: cubic-bezier(.4, 0, .2, 1); --item-color: rgb(219, 219, 223); --item-color-active: #FFCC33; --item-size: 24px;"`;