Skip to content

Commit

Permalink
fix(modal-close): some modal close unexpectedly clicking on the overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoaibdev7 committed Apr 19, 2024
1 parent 2a43b44 commit 3d97bad
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const BriefDescription: FC<Props> = ({ trend, onClose, selectTrending })
}, [handleClose])

return (
<BaseModal id="briefDescription" kind="regular" noWrap onClose={handleClose}>
<BaseModal id="briefDescription" kind="regular" noWrap onClose={handleClose} preventOutsideClose>
{trend.audio_EN ? (
<>
<Flex direction="row" justify="flex-start" m={20}>
Expand Down
1 change: 1 addition & 0 deletions src/components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const BaseModal = ({
<>
<Bg
align="center"
data-testid="modal-overlay"
hideBg={hideBg}
justify="center"
onClick={(e) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { render, screen } from '@testing-library/react'
import { render, screen, waitFor } from '@testing-library/react'
import '@testing-library/jest-dom'
import { GraphBlueprint } from '../index'

Expand All @@ -15,8 +15,10 @@ describe('GraphBlueprint', () => {
it('should display only one Custom node', async () => {
render(<GraphBlueprint />)

const customNodes = await screen.findAllByText('Custom')
waitFor(async () => {
const customNodes = await screen.findAllByText('Custom')

expect(customNodes).toHaveLength(1)
expect(customNodes).toHaveLength(1)
})
})
})
2 changes: 1 addition & 1 deletion src/components/SettingsModal/SettingsView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const SettingsView: React.FC<Props> = ({ onClose }) => {
]

return (
<Wrapper direction="column">
<Wrapper data-testid="settings-modal" direction="column">
<SettingsHeader>
<StyledTabs aria-label="settings tabs" onChange={handleChange} value={value}>
{tabs.map((tab, index) => (
Expand Down
34 changes: 34 additions & 0 deletions src/components/SettingsModal/__tests__/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable padding-line-between-statements */
import '@testing-library/jest-dom'
import { fireEvent, render } from '@testing-library/react'
import * as modalStore from '../../../stores/useModalStore'
import { SettingsModal } from '../index'

jest.mock('../../../stores/useModalStore', () => ({
useModalStore: jest.fn(),
useModal: jest.fn().mockImplementation((id) => ({
close: jest.fn(),
open: jest.fn(),
visible: id === 'settings',
})),
useSomeModalIsOpen: jest.fn(),
}))

describe('SettingsModal', () => {
it('Settings Modal does not close on overlay click', () => {
const { getByTestId } = render(<SettingsModal />)

const overlay = getByTestId('modal-overlay')

fireEvent.click(overlay)

// Check Settings modal is still open
const modalContent = getByTestId('settings-modal')
expect(modalContent).toBeInTheDocument()

// Check close function was not called
const { close } = modalStore.useModal('settings')

expect(close).not.toHaveBeenCalled()
})
})
2 changes: 1 addition & 1 deletion src/components/SettingsModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const SettingsModal = () => {
const { visible } = useModal('addItem')

return visible ? null : (
<BaseModal background="BG1" id="settings" noWrap onClose={close}>
<BaseModal background="BG1" id="settings" noWrap onClose={close} preventOutsideClose>
<SettingsView onClose={close} />
</BaseModal>
)
Expand Down
2 changes: 1 addition & 1 deletion src/components/SourcesTableModal/SourcesView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const SourcesView = () => {
})

return (
<Wrapper direction="column">
<Wrapper data-testid="sources-table" direction="column">
<StyledTabs aria-label="sources tabs" onChange={handleChange} value={value}>
{tabs.map((tab, index) => (
<StyledTab key={tab.label} color={colors.white} disableRipple label={tab.label} {...a11yProps(index)} />
Expand Down
34 changes: 34 additions & 0 deletions src/components/SourcesTableModal/__tests__/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable padding-line-between-statements */
import '@testing-library/jest-dom'
import { fireEvent, render } from '@testing-library/react'
import * as modalStore from '~/stores/useModalStore'
import { SourcesTableModal } from '../index'

jest.mock('~/stores/useModalStore', () => ({
useModalStore: jest.fn(),
useModal: jest.fn().mockImplementation((id) => ({
close: jest.fn(),
open: jest.fn(),
visible: id === 'sourcesTable',
})),
useSomeModalIsOpen: jest.fn(),
}))

describe('SourcesTable Modal', () => {
it('SourcesTable Modal does not close on overlay click', () => {
const { getByTestId } = render(<SourcesTableModal />)

const overlay = getByTestId('modal-overlay')

fireEvent.click(overlay)

// Check SourcesTable modal is still open
const modalContent = getByTestId('sources-table')
expect(modalContent).toBeInTheDocument()

// Check close function was not called
const { close } = modalStore.useModal('sourcesTable')

expect(close).not.toHaveBeenCalled()
})
})
2 changes: 1 addition & 1 deletion src/components/SourcesTableModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const SourcesTableModal = () => {
const { visible } = useModal('addContent')

return visible ? null : (
<BaseModal background="BG1" id="sourcesTable" kind="large" noWrap onClose={close}>
<BaseModal background="BG1" id="sourcesTable" kind="large" noWrap onClose={close} preventOutsideClose>
<SourcesView />
</BaseModal>
)
Expand Down

0 comments on commit 3d97bad

Please sign in to comment.