diff --git a/src/components/ui/__tests__/modal.tsx b/src/components/ui/__tests__/modal.tsx index 07e8b1b144..b967e492f7 100644 --- a/src/components/ui/__tests__/modal.tsx +++ b/src/components/ui/__tests__/modal.tsx @@ -22,21 +22,25 @@ const App: React.FunctionComponent = ({ defaultVisible = false }: { default ) } -// const AppWithMultipleModal: React.FunctionComponent = () => { -// const [visible, setVisible] = React.useState(false) -// return ( -// <> -// -//
-//
-//
-// -// ) -// } +const InnerComponentWithAnotherModal = () => { + const [visible, setVisible] = React.useState(false) + return ( +
+
+ ) +} + +const AppModalInsideModal: React.FunctionComponent = () => { + return ( + + + + ) +} describe('Modal', () => { it('should show Modal when visible prop is true', () => { @@ -55,15 +59,26 @@ describe('Modal', () => { expect(wrapper.find('[data-test="modal"]')).toHaveLength(0) }) - // TODO: will make the test pass later - // it('Should render multiple Modals correctly', () => { - // const wrapper = mount(renderWithPortalProvider()) - // const showSecondModalButton = wrapper.find('[data-test="show-second-modal"]') - // expect(wrapper.find('[data-test="modal"]')).toHaveLength(1) - // expect(showSecondModalButton).toHaveLength(1) - // showSecondModalButton.simulate('click') - // expect(wrapper.find('[data-test="modal"]')).toHaveLength(2) - // }) + it('Should render multiple Modals correctly', () => { + const wrapper = mount( + renderWithPortalProvider( + <> + Modal 1 + Modal 2 + + ) + ) + expect(wrapper.find('[data-test="modal"]')).toHaveLength(2) + }) + + it('Should render a component containing a Modal inside a Modal', () => { + const wrapper = mount(renderWithPortalProvider()) + const showSecondModalButton = wrapper.find('[data-test="show-second-modal"]') + expect(wrapper.find('[data-test="modal"]')).toHaveLength(1) + expect(showSecondModalButton).toHaveLength(1) + showSecondModalButton.simulate('click') + expect(wrapper.find('[data-test="modal"]')).toHaveLength(2) + }) afterEach(() => { jest.resetAllMocks() diff --git a/src/hooks/use-portal/portal-provider.tsx b/src/hooks/use-portal/portal-provider.tsx index 61522fdbd2..f97a25352f 100644 --- a/src/hooks/use-portal/portal-provider.tsx +++ b/src/hooks/use-portal/portal-provider.tsx @@ -11,19 +11,21 @@ export const PortalProvider: React.FunctionComponent = ({ c const showPortal = React.useCallback( (key: string, modal: React.ComponentType) => { - setPortals({ - ...portals, + setPortals(prevPortals => ({ + ...prevPortals, [key]: modal - }) + })) }, [portals] ) const hidePortal = React.useCallback( (key: string) => { - const newPortals = { ...portals } - delete newPortals[key] - setPortals(newPortals) + setPortals(prevPortals => { + const newPortals = { ...prevPortals } + delete newPortals[key] + return newPortals + }) }, [portals] ) diff --git a/src/hooks/use-portal/use-portal.ts b/src/hooks/use-portal/use-portal.ts index 5c267178c5..93ff4828ef 100644 --- a/src/hooks/use-portal/use-portal.ts +++ b/src/hooks/use-portal/use-portal.ts @@ -21,7 +21,9 @@ export function usePortal(component: React.ComponentType) { context.hidePortal(key) } - return () => context.hidePortal(key) + return () => { + context.hidePortal(key) + } }, [portal, isShown]) return [showPortal, hidePortal]