From 19282b6d56e4c4654c77adf50d512739b1432b48 Mon Sep 17 00:00:00 2001 From: XieZongChen <46394163+amadeus711@users.noreply.github.com> Date: Thu, 19 Aug 2021 01:27:15 -0500 Subject: [PATCH] test(NDrawer): add show/on-update:show/mask-closable props test (#913) --- src/drawer/tests/Drawer.spec.tsx | 59 +++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/src/drawer/tests/Drawer.spec.tsx b/src/drawer/tests/Drawer.spec.tsx index 9aa7a8f0d..a8273351c 100644 --- a/src/drawer/tests/Drawer.spec.tsx +++ b/src/drawer/tests/Drawer.spec.tsx @@ -22,16 +22,20 @@ function expectDrawerExists (): void { // eslint-disable-next-line @typescript-eslint/explicit-function-return-type function mountDrawer ({ drawerProps, - drawerContentProps + drawerContentProps, + hasOnUpdateShow, + show }: { drawerProps?: DrawerProps drawerContentProps?: DrawerContentProps + hasOnUpdateShow?: boolean + show?: boolean }) { return mount( defineComponent({ setup () { return { - show: ref(false) + show: ref(!!show) } }, render () { @@ -45,9 +49,13 @@ function mountDrawer ({ , { - this.show = show - }} + onUpdateShow={ + hasOnUpdateShow + ? drawerProps?.onUpdateShow + : (show) => { + this.show = show + } + } {...drawerProps} > {{ @@ -116,4 +124,45 @@ describe('n-drawer', () => { expectDrawerExists() wrapper.unmount() }) + + it('should work with `show` prop', async () => { + await mountDrawer({ + show: false + }) + expect(document.querySelector('.n-drawer')).toEqual(null) + await mountDrawer({ + show: true + }) + expect(document.querySelector('.n-drawer')).not.toEqual(null) + }) + + it('should work with `on-update:show` prop', async () => { + const onUpdate = jest.fn() + const wrapper = mountDrawer({ + hasOnUpdateShow: true, + drawerProps: { onUpdateShow: onUpdate }, + drawerContentProps: { closable: true } + }) + await wrapper.find('button').trigger('click') + setTimeout(() => { + expect(onUpdate).toHaveBeenCalled() + }, 300) + }) + + it('should work with `mask-closable` prop', async () => { + const onUpdate = jest.fn() + const mousedownEvent = new MouseEvent('mousedown', { bubbles: true }) + const mouseupEvent = new MouseEvent('mouseup', { bubbles: true }) + await mountDrawer({ + show: true, + hasOnUpdateShow: true, + drawerProps: { onUpdateShow: onUpdate }, + drawerContentProps: { closable: true } + }) + document.querySelector('.n-drawer-mask')?.dispatchEvent(mousedownEvent) + document.querySelector('.n-drawer-mask')?.dispatchEvent(mouseupEvent) + setTimeout(() => { + expect(onUpdate).toHaveBeenCalled() + }, 300) + }) })