test(NDrawer): add test for placement (#902)

This commit is contained in:
XieZongChen 2021-08-17 21:01:36 -05:00 committed by GitHub
parent ea25207831
commit 653047406f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,12 @@
import { mount } from '@vue/test-utils' import { mount } from '@vue/test-utils'
import { defineComponent, h, nextTick, ref } from 'vue' import { defineComponent, h, nextTick, ref } from 'vue'
import { NButton } from '../../button' import { NButton } from '../../button'
import { NDrawer, NDrawerContent } from '../index' import {
DrawerContentProps,
DrawerProps,
NDrawer,
NDrawerContent
} from '../index'
// It seems due to special handling of transition in naive-ui, the drawer's DOM // It seems due to special handling of transition in naive-ui, the drawer's DOM
// won't disappear even if its `show` prop is false. No time to find out the // won't disappear even if its `show` prop is false. No time to find out the
@ -14,42 +19,59 @@ function expectDrawerExists (): void {
).toEqual('none') ).toEqual('none')
} }
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function mountDrawer ({
drawerProps,
drawerContentProps
}: {
drawerProps?: DrawerProps
drawerContentProps?: DrawerContentProps
}) {
return mount(
defineComponent({
setup () {
return {
show: ref(false)
}
},
render () {
return [
<NButton
onClick={() => {
this.show = true
}}
>
{{ default: () => 'Show' }}
</NButton>,
<NDrawer
show={this.show}
onUpdateShow={(show) => {
this.show = show
}}
{...drawerProps}
>
{{
default: () => (
<NDrawerContent {...drawerContentProps}></NDrawerContent>
)
}}
</NDrawer>
]
}
}),
{
attachTo: document.body
}
)
}
describe('n-drawer', () => { describe('n-drawer', () => {
it('should work with import on demand', () => { it('should work with import on demand', () => {
mount(NDrawer) mount(NDrawer)
}) })
it('closable', async () => { it('closable', async () => {
const wrapper = mount( const wrapper = mountDrawer({ drawerContentProps: { closable: true } })
defineComponent({
setup () {
return {
show: ref(false)
}
},
render () {
return [
<NButton
onClick={() => {
this.show = true
}}
>
{{ default: () => 'Show' }}
</NButton>,
<NDrawer
show={this.show}
onUpdateShow={(show) => {
this.show = show
}}
>
{{ default: () => <NDrawerContent closable></NDrawerContent> }}
</NDrawer>
]
}
}),
{
attachTo: document.body
}
)
expect(document.querySelector('.n-drawer')).toEqual(null) expect(document.querySelector('.n-drawer')).toEqual(null)
await wrapper.find('button').trigger('click') await wrapper.find('button').trigger('click')
expect(document.querySelector('.n-drawer')).not.toEqual(null) expect(document.querySelector('.n-drawer')).not.toEqual(null)
@ -60,4 +82,38 @@ describe('n-drawer', () => {
expectDrawerExists() expectDrawerExists()
wrapper.unmount() wrapper.unmount()
}) })
it('should work with `placement` prop', async () => {
let wrapper = mountDrawer({ drawerProps: { placement: 'top' } })
await wrapper.find('button').trigger('click')
expect(document.querySelector('.n-drawer')?.className).toContain(
'n-drawer--top-placement'
)
expectDrawerExists()
wrapper.unmount()
wrapper = mountDrawer({ drawerProps: { placement: 'right' } })
await wrapper.find('button').trigger('click')
expect(document.querySelector('.n-drawer')?.className).toContain(
'n-drawer--right-placement'
)
expectDrawerExists()
wrapper.unmount()
wrapper = mountDrawer({ drawerProps: { placement: 'bottom' } })
await wrapper.find('button').trigger('click')
expect(document.querySelector('.n-drawer')?.className).toContain(
'n-drawer--bottom-placement'
)
expectDrawerExists()
wrapper.unmount()
wrapper = mountDrawer({ drawerProps: { placement: 'left' } })
await wrapper.find('button').trigger('click')
expect(document.querySelector('.n-drawer')?.className).toContain(
'n-drawer--left-placement'
)
expectDrawerExists()
wrapper.unmount()
})
}) })