test(modal): add modal test (#941)

This commit is contained in:
XieZongChen 2021-08-22 22:02:34 -05:00 committed by GitHub
parent 3d2736fb61
commit 33725ca4cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 8 deletions

View File

@ -1,8 +0,0 @@
import { mount } from '@vue/test-utils'
import { NModal } from '../index'
describe('n-modal', () => {
it('should work with import on demand', () => {
mount(NModal)
})
})

View File

@ -0,0 +1,102 @@
import { defineComponent, h, ref } from 'vue'
import { mount } from '@vue/test-utils'
import { ModalProps, NModal } from '../index'
import { NButton } from '../../button'
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function mountModal ({
modalProps,
show
}: {
modalProps?: ModalProps
show?: boolean
}) {
return mount(
defineComponent({
setup () {
return {
show: ref(!!show)
}
},
render () {
return [
<NButton
onClick={() => {
this.show = true
}}
>
{{ default: () => 'Show' }}
</NButton>,
<NModal
show={this.show}
onUpdateShow={(show) => {
this.show = show
}}
{...modalProps}
>
{{
default: () => 'test'
}}
</NModal>
]
}
}),
{
attachTo: document.body
}
)
}
describe('n-modal', () => {
it('should work with import on demand', () => {
mount(NModal)
})
it('should work with `display-directive` prop', async () => {
const mousedownEvent = new MouseEvent('mousedown', { bubbles: true })
const mouseupEvent = new MouseEvent('mouseup', { bubbles: true })
let wrapper = mountModal({})
expect(document.querySelector('.n-modal-body-wrapper')).toEqual(null)
await wrapper.find('button').trigger('click')
expect(document.querySelector('.n-modal-body-wrapper')).not.toEqual(null)
document.querySelector('.n-modal-mask')?.dispatchEvent(mousedownEvent)
document.querySelector('.n-modal-mask')?.dispatchEvent(mouseupEvent)
setTimeout(() => {
expect(
document.querySelector('.n-modal-body-wrapper')?.children.length
).toBe(0)
}, 300)
wrapper.unmount()
wrapper = mountModal({ modalProps: { displayDirective: 'show' } })
expect(document.querySelector('.n-modal-body-wrapper')).toEqual(null)
await wrapper.find('button').trigger('click')
expect(document.querySelector('.n-modal-body-wrapper')).not.toEqual(null)
document.querySelector('.n-modal-mask')?.dispatchEvent(mousedownEvent)
document.querySelector('.n-modal-mask')?.dispatchEvent(mouseupEvent)
setTimeout(() => {
expect(
document.querySelector('.n-modal-body-wrapper')?.children.length
).not.toBe(0)
expect(
document.querySelector('.n-modal-body-wrapper')?.getAttribute('style')
).toContain('display: none')
}, 300)
wrapper.unmount()
})
it('should work with `preset` prop', async () => {
let wrapper = mountModal({ modalProps: { preset: 'card' } })
expect(document.querySelector('.n-modal-body-wrapper')).toEqual(null)
await wrapper.find('button').trigger('click')
expect(document.querySelector('.n-modal-body-wrapper')).not.toEqual(null)
expect(document.querySelector('.n-card')).not.toEqual(null)
wrapper.unmount()
wrapper = mountModal({ modalProps: { preset: 'dialog' } })
expect(document.querySelector('.n-modal-body-wrapper')).toEqual(null)
await wrapper.find('button').trigger('click')
expect(document.querySelector('.n-modal-body-wrapper')).not.toEqual(null)
expect(document.querySelector('.n-dialog')).not.toEqual(null)
wrapper.unmount()
})
})