Skip to content

Commit

Permalink
Merge pull request #4393 from thematters/fix/dialog-test
Browse files Browse the repository at this point in the history
fix: fix dialog tests
  • Loading branch information
robertu7 authored May 16, 2024
2 parents 3c20312 + 6fb1ee9 commit 5fd86a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest'

import { TEST_ID } from '~/common/enums'
import { render, screen, waitFor } from '~/common/utils/test'
import { MOCK_COLLECTION } from '~/stories/mocks'

Expand Down Expand Up @@ -29,9 +30,9 @@ describe('<CollectionDigest/DropdownActions>', () => {
})
expect($editButton).toBeInTheDocument()
$editButton.click()
const $editDialog = screen.getByRole('dialog', { name: 'Edit collection' })
const $editDialog = screen.getByTestId(TEST_ID.DIALOG_EDIT_COLLECTION)
expect($editDialog).toBeInTheDocument()
screen.getByRole('button', { name: 'Close' }).click()
screen.getByRole('button', { name: 'Close', hidden: true }).click()
await waitFor(() => {
expect(
screen.queryByRole('button', { name: 'Close' })
Expand Down
14 changes: 8 additions & 6 deletions src/components/Dialog/Dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ describe('<Dialog>', () => {

// open dialog
fireEvent.click(screen.getByRole('button', { name: 'Open' }))
expect(screen.getAllByText('Header')).toHaveLength(2)
expect(screen.getByText('Content')).toBeInTheDocument()
expect(screen.getAllByText('Header')[0]).toBeInTheDocument()
expect(screen.getAllByText('Content')[0]).toBeInTheDocument()

// click confirm
fireEvent.click(screen.getByRole('button', { name: 'Confirm' }))
fireEvent.click(
screen.getByRole('button', { name: 'Confirm', hidden: true })
)
expect(onClickConfirm).toHaveBeenCalledTimes(1)

// close dialog
fireEvent.click(screen.getByRole('button', { name: 'Close' }))
fireEvent.click(screen.getByRole('button', { name: 'Close', hidden: true }))
await waitFor(() => {
expect(screen.queryByText('Content')).not.toBeInTheDocument()
})
Expand All @@ -75,7 +77,7 @@ describe('<Dialog>', () => {
)

expect(screen.getAllByText('Header')).toHaveLength(2)
expect(screen.getByText('Content')).toBeInTheDocument()
expect(screen.getAllByText('Content')[0]).toBeInTheDocument()

// click outside
fireEvent.mouseDown(document.body)
Expand All @@ -95,7 +97,7 @@ describe('<Dialog>', () => {
// )

// expect(screen.getAllByText('Header')).toHaveLength(2)
// expect(screen.getByText('Content')).toBeInTheDocument()
// expect(screen.getAllByText('Content')[0]).toBeInTheDocument()

// // hit ESC
// fireEvent.keyDown(container, { key: 'Escape' })
Expand Down
8 changes: 3 additions & 5 deletions src/components/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const Container: React.FC<
smBgColor,
smUpBgColor,
hidePaddingBottom,
testId,
onDismiss,
dismissOnClickOutside = false,
dismissOnHandle = true,
Expand Down Expand Up @@ -98,7 +97,6 @@ const Container: React.FC<

return (
<div
{...(testId ? { 'data-test-id': testId } : {})}
ref={node}
className={containerClasses}
style={style}
Expand Down Expand Up @@ -234,7 +232,7 @@ export const Dialog: React.ComponentType<
RoundedButton: typeof RoundedButton
Lazy: typeof Lazy
} = (props) => {
const { isOpen } = props
const { isOpen, testId } = props
const [mounted, setMounted] = useState(isOpen)

useEffect(() => {
Expand All @@ -248,14 +246,14 @@ export const Dialog: React.ComponentType<
}

return (
<>
<div {...(testId ? { 'data-test-id': testId } : {})}>
<Media at="sm">
<SimpleDialog {...props} mounted={mounted} setMounted={setMounted} />
</Media>
<Media greaterThan="sm">
<AnimatedDilaog {...props} mounted={mounted} setMounted={setMounted} />
</Media>
</>
</div>
)
}

Expand Down

0 comments on commit 5fd86a5

Please sign in to comment.