-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into new/room_msg_bridge
- Loading branch information
Showing
12 changed files
with
270 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@rocket.chat/meteor": patch | ||
"@rocket.chat/mock-providers": patch | ||
"@rocket.chat/ui-contexts": patch | ||
"@rocket.chat/web-ui-registration": patch | ||
--- | ||
|
||
Fixed an issue with blocked login when dismissed 2FA modal by clicking outside of it or pressing the escape key |
87 changes: 87 additions & 0 deletions
87
apps/meteor/client/components/GenericModal/GenericModal.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { useSetModal } from '@rocket.chat/ui-contexts'; | ||
import { act, screen } from '@testing-library/react'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import userEvent from '@testing-library/user-event'; | ||
import type { ReactElement } from 'react'; | ||
import React, { Suspense } from 'react'; | ||
|
||
import ModalProviderWithRegion from '../../providers/ModalProvider/ModalProviderWithRegion'; | ||
import GenericModal from './GenericModal'; | ||
|
||
import '@testing-library/jest-dom'; | ||
|
||
const renderModal = (modalElement: ReactElement) => { | ||
const { | ||
result: { current: setModal }, | ||
} = renderHook(() => useSetModal(), { | ||
wrapper: ({ children }) => ( | ||
<Suspense fallback={null}> | ||
<ModalProviderWithRegion>{children}</ModalProviderWithRegion> | ||
</Suspense> | ||
), | ||
}); | ||
|
||
act(() => { | ||
setModal(modalElement); | ||
}); | ||
|
||
return { setModal }; | ||
}; | ||
|
||
describe('callbacks', () => { | ||
it('should call onClose callback when dismissed', async () => { | ||
const handleClose = jest.fn(); | ||
|
||
renderModal(<GenericModal title='Modal' onClose={handleClose} />); | ||
|
||
expect(await screen.findByRole('heading', { name: 'Modal', exact: true })).toBeInTheDocument(); | ||
|
||
userEvent.keyboard('{Escape}'); | ||
|
||
expect(screen.queryByRole('heading', { name: 'Modal', exact: true })).not.toBeInTheDocument(); | ||
|
||
expect(handleClose).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should NOT call onClose callback when confirmed', async () => { | ||
const handleConfirm = jest.fn(); | ||
const handleClose = jest.fn(); | ||
|
||
const { setModal } = renderModal(<GenericModal title='Modal' onConfirm={handleConfirm} onClose={handleClose} />); | ||
|
||
expect(await screen.findByRole('heading', { name: 'Modal', exact: true })).toBeInTheDocument(); | ||
|
||
userEvent.click(screen.getByRole('button', { name: 'Ok', exact: true })); | ||
|
||
expect(handleConfirm).toHaveBeenCalled(); | ||
|
||
act(() => { | ||
setModal(null); | ||
}); | ||
|
||
expect(screen.queryByRole('heading', { name: 'Modal', exact: true })).not.toBeInTheDocument(); | ||
|
||
expect(handleClose).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should NOT call onClose callback when cancelled', async () => { | ||
const handleCancel = jest.fn(); | ||
const handleClose = jest.fn(); | ||
|
||
const { setModal } = renderModal(<GenericModal title='Modal' onCancel={handleCancel} onClose={handleClose} />); | ||
|
||
expect(await screen.findByRole('heading', { name: 'Modal', exact: true })).toBeInTheDocument(); | ||
|
||
userEvent.click(screen.getByRole('button', { name: 'Cancel', exact: true })); | ||
|
||
expect(handleCancel).toHaveBeenCalled(); | ||
|
||
act(() => { | ||
setModal(null); | ||
}); | ||
|
||
expect(screen.queryByRole('heading', { name: 'Modal', exact: true })).not.toBeInTheDocument(); | ||
|
||
expect(handleClose).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,32 @@ | ||
import type { ReactElement, ReactNode } from 'react'; | ||
import React, { memo, useEffect, useState } from 'react'; | ||
import type { ReactNode } from 'react'; | ||
import { memo } from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
|
||
import { createAnchor } from '../lib/utils/createAnchor'; | ||
import { deleteAnchor } from '../lib/utils/deleteAnchor'; | ||
const createModalRoot = (): HTMLElement => { | ||
const id = 'modal-root'; | ||
const existing = document.getElementById(id); | ||
|
||
if (existing) return existing; | ||
|
||
const newOne = document.createElement('div'); | ||
newOne.id = id; | ||
document.body.append(newOne); | ||
|
||
return newOne; | ||
}; | ||
|
||
let modalRoot: HTMLElement | null = null; | ||
|
||
type ModalPortalProps = { | ||
children?: ReactNode; | ||
}; | ||
|
||
const ModalPortal = ({ children }: ModalPortalProps): ReactElement => { | ||
const [modalRoot] = useState(() => createAnchor('modal-root')); | ||
useEffect(() => (): void => deleteAnchor(modalRoot), [modalRoot]); | ||
return <>{createPortal(children, modalRoot)}</>; | ||
const ModalPortal = ({ children }: ModalPortalProps) => { | ||
if (!modalRoot) { | ||
modalRoot = createModalRoot(); | ||
} | ||
|
||
return createPortal(children, modalRoot); | ||
}; | ||
|
||
export default memo(ModalPortal); |
Oops, something went wrong.