Skip to content

Commit

Permalink
chore: update the test for dropdown button in modal
Browse files Browse the repository at this point in the history
  • Loading branch information
Chisomchima committed May 8, 2024
1 parent b9c59a2 commit d3e7770
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ describe('<DropdownButton>', () => {
expect(queryByText(componentText)).not.toBeInTheDocument()
})
})
test('closes dropdown when escape key is pressed inside modal', async () => {
const componentText = 'Dropdown Content'
test('modal remains open when dropdown button is closed on escape click', async () => {
const dropdownButtonText = 'Dropdown Content'
const headingText = 'Heading Text'
const modalContent = (
<DropdownButton component={componentText} />
<div>
<h1>{headingText}</h1>
<DropdownButton component={dropdownButtonText} />
</div>
)

const { getByTestId, queryByText } = render(
Expand All @@ -85,16 +89,20 @@ describe('<DropdownButton>', () => {
fireEvent.click(toggleButton)

await waitFor(() => {
expect(queryByText(componentText)).toBeInTheDocument()
expect(queryByText(dropdownButtonText)).toBeInTheDocument()
})

fireEvent.keyDown(document, { key: 'Escape' })

await waitFor(() => {
expect(queryByText(componentText)).not.toBeInTheDocument()
expect(
queryByText(dropdownButtonText)
).not.toBeInTheDocument()
expect(queryByText(headingText)).toBeInTheDocument()
})
})
})

describe('closed state', () => {
const onClick = jest.fn()
const wrapper = mount(
Expand Down
17 changes: 13 additions & 4 deletions components/button/src/dropdown-button/dropdown-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,20 @@ class DropdownButton extends Component {
document.removeEventListener('keydown', this.handleKeyDown)
}

// handleKeyDown = (event) => {
// event.preventDefault()
// if (event.key === 'Escape' && this.state.open) {
// event.stopPropagation()
// this.setState({ open: false })
// }
// }

handleKeyDown = (event) => {
event.preventDefault()
if (event.key === 'Escape' && this.state.open) {
event.stopPropagation()
this.setState({ open: false })
if (event.key === 'Escape') {
if (this.state.open) {
event.stopPropagation()
this.setState({ open: false })
}
}
}

Expand Down

0 comments on commit d3e7770

Please sign in to comment.