-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(test): add tests for action menu
- Loading branch information
1 parent
a0b4f97
commit 10f19f0
Showing
3 changed files
with
216 additions
and
1 deletion.
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
95 changes: 95 additions & 0 deletions
95
src/components/__tests__/__snapshots__/actionMenu.test.tsx.snap
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,95 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders proper dom including elipse icon 1`] = ` | ||
<DocumentFragment> | ||
<button | ||
aria-expanded="false" | ||
aria-label="Action Menu Toggle" | ||
class="pf-v5-c-menu-toggle pf-m-plain" | ||
type="button" | ||
> | ||
<svg | ||
aria-hidden="true" | ||
class="pf-v5-svg" | ||
fill="currentColor" | ||
height="1em" | ||
role="img" | ||
viewBox="0 0 192 512" | ||
width="1em" | ||
> | ||
<path | ||
d="M96 184c39.8 0 72 32.2 72 72s-32.2 72-72 72-72-32.2-72-72 32.2-72 72-72zM24 80c0 39.8 32.2 72 72 72s72-32.2 72-72S135.8 8 96 8 24 40.2 24 80zm0 352c0 39.8 32.2 72 72 72s72-32.2 72-72-32.2-72-72-72-72 32.2-72 72z" | ||
/> | ||
</svg> | ||
</button> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`snapshot matches after opening the action menu 1`] = ` | ||
<DocumentFragment> | ||
<button | ||
aria-expanded="true" | ||
aria-label="Action Menu Toggle" | ||
class="pf-v5-c-menu-toggle pf-m-expanded pf-m-plain" | ||
type="button" | ||
> | ||
<svg | ||
aria-hidden="true" | ||
class="pf-v5-svg" | ||
fill="currentColor" | ||
height="1em" | ||
role="img" | ||
viewBox="0 0 192 512" | ||
width="1em" | ||
> | ||
<path | ||
d="M96 184c39.8 0 72 32.2 72 72s-32.2 72-72 72-72-32.2-72-72 32.2-72 72-72zM24 80c0 39.8 32.2 72 72 72s72-32.2 72-72S135.8 8 96 8 24 40.2 24 80zm0 352c0 39.8 32.2 72 72 72s72-32.2 72-72-32.2-72-72-72-72 32.2-72 72z" | ||
/> | ||
</svg> | ||
</button> | ||
<div | ||
class="pf-v5-c-menu" | ||
data-ouia-component-id="OUIA-Generated-Dropdown-4" | ||
data-ouia-component-type="PF5/Dropdown" | ||
data-ouia-safe="true" | ||
data-popper-escaped="true" | ||
data-popper-placement="bottom-start" | ||
data-popper-reference-hidden="true" | ||
style="position: absolute; left: 0px; top: 0px; z-index: 9999; opacity: 1; transition: opacity 0ms cubic-bezier(.54, 1.5, .38, 1.11); min-width: 0px; transform: translate(0px, 0px);" | ||
> | ||
<div | ||
class="pf-v5-c-menu__content" | ||
> | ||
<ul | ||
class="pf-v5-c-menu__list" | ||
role="menu" | ||
> | ||
<li | ||
class="pf-v5-c-menu__list-item" | ||
data-ouia-component-id="OUIA-Generated-DropdownItem-2" | ||
data-ouia-component-type="PF5/DropdownItem" | ||
data-ouia-safe="true" | ||
role="none" | ||
> | ||
<button | ||
class="pf-v5-c-menu__item" | ||
role="menuitem" | ||
tabindex="0" | ||
type="button" | ||
> | ||
<span | ||
class="pf-v5-c-menu__item-main" | ||
> | ||
<span | ||
class="pf-v5-c-menu__item-text" | ||
> | ||
Delete | ||
</span> | ||
</span> | ||
</button> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
</DocumentFragment> | ||
`; |
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,120 @@ | ||
import React from 'react'; | ||
import ActionMenu from '../ActionMenu'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import '@testing-library/jest-dom'; | ||
|
||
test('renders proper dom including elipse icon', () => { | ||
const item = { foo: 'bar' }; | ||
const actionClicked = (_item: {foo: string}) => {}; | ||
const { asFragment } = render( | ||
<ActionMenu<{ foo: string }> | ||
item={item} | ||
actions={[{ label: 'Delete', onClick: actionClicked }]} | ||
/> | ||
); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
test('actions will not show before button is clicked', () => { | ||
const user = userEvent.setup(); | ||
|
||
const item = { foo: 'bar' }; | ||
const actionClicked = (_item: {foo: string}) => {}; | ||
const { queryByText } = render( | ||
<ActionMenu<{ foo: string }> | ||
item={item} | ||
actions={[{ label: 'Delete', onClick: actionClicked }]} | ||
/> | ||
); | ||
expect(queryByText("Delete")).toBeNull(); | ||
}); | ||
|
||
test('actions will show after button is clicked', async () => { | ||
const user = userEvent.setup(); | ||
|
||
const item = { foo: 'bar' }; | ||
const actionClicked = (_item: {foo: string}) => {}; | ||
render( | ||
<ActionMenu<{ foo: string }> | ||
item={item} | ||
actions={[{ label: 'Delete', onClick: actionClicked }]} | ||
/> | ||
); | ||
const toggleButton = await screen.findByLabelText('Action Menu Toggle'); | ||
await user.click(toggleButton); | ||
expect(screen.getByText("Delete")).toBeVisible(); | ||
}); | ||
|
||
test('snapshot matches after opening the action menu', async () => { | ||
const user = userEvent.setup(); | ||
|
||
const item = { foo: 'bar' }; | ||
const actionClicked = (_item: {foo: string}) => {}; | ||
const { asFragment } = render( | ||
<ActionMenu<{ foo: string }> | ||
item={item} | ||
actions={[{ label: 'Delete', onClick: actionClicked }]} | ||
/> | ||
); | ||
const toggleButton = await screen.findByLabelText('Action Menu Toggle'); | ||
await user.click(toggleButton); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
test('action callback should get called when clicked', async () => { | ||
const user = userEvent.setup(); | ||
|
||
const item = { foo: 'bar' }; | ||
const actionClicked = jest.fn(); | ||
render( | ||
<ActionMenu<{ foo: string }> | ||
item={item} | ||
actions={[{ label: 'Delete', onClick: actionClicked }]} | ||
/> | ||
); | ||
expect(actionClicked).toHaveBeenCalledTimes(0); | ||
const toggleButton = await screen.findByLabelText('Action Menu Toggle'); | ||
await user.click(toggleButton); | ||
const deleteButton = await screen.findByText("Delete"); | ||
await user.click(deleteButton); | ||
expect(actionClicked).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('action menu closes after selection', async () => { | ||
const user = userEvent.setup(); | ||
|
||
const item = { foo: 'bar' }; | ||
const actionClicked = jest.fn(); | ||
render( | ||
<ActionMenu<{ foo: string }> | ||
item={item} | ||
actions={[{ label: 'Delete', onClick: actionClicked }]} | ||
/> | ||
); | ||
const toggleButton = await screen.findByLabelText('Action Menu Toggle'); | ||
await user.click(toggleButton); | ||
const deleteButton = await screen.findByText("Delete"); | ||
expect(deleteButton).toBeVisible(); | ||
await user.click(deleteButton); | ||
expect(deleteButton).not.toBeVisible(); | ||
}); | ||
|
||
test('action menu closes if toggle is clicked again', async () => { | ||
const user = userEvent.setup(); | ||
|
||
const item = { foo: 'bar' }; | ||
const actionClicked = jest.fn(); | ||
render( | ||
<ActionMenu<{ foo: string }> | ||
item={item} | ||
actions={[{ label: 'Delete', onClick: actionClicked }]} | ||
/> | ||
); | ||
const toggleButton = await screen.findByLabelText('Action Menu Toggle'); | ||
await user.click(toggleButton); | ||
const deleteButton = await screen.findByText("Delete"); | ||
expect(deleteButton).toBeVisible(); | ||
await user.click(toggleButton); | ||
expect(deleteButton).not.toBeVisible(); | ||
}); |