Skip to content

Commit

Permalink
Add tests to <ActionListButton />
Browse files Browse the repository at this point in the history
  • Loading branch information
danmondra committed Sep 11, 2023
1 parent 3f33551 commit f7dc260
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ACTIONS_MSGS } from './BookNotification'

const { IS_ALREADY_IN_LIST, ADD_TO_SPECIFIC_LIST, DELETE_FROM_LIST } = ACTIONS_MSGS

const TITLE_VARIANTS = {
export const TITLE_VARIANTS = {
delete: (nameList) => `${DELETE_FROM_LIST} ${nameList}`,
isAlready: (nameList) => `${IS_ALREADY_IN_LIST} "${nameList}"`,
default: (nameList) => `${ADD_TO_SPECIFIC_LIST} "${nameList}"`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { cleanup, fireEvent, render, waitFor } from '@testing-library/react'
import { afterEach, expect, it, vi } from 'vitest'

import { ActionListButton, TITLE_VARIANTS } from '../components/ActionListButton.jsx'

afterEach(cleanup)

it('should render the <ActionListButton /> with the default title', () => {
const { getByRole } = render(<ActionListButton action={() => {}} Icon={() => null} />)

const name = TITLE_VARIANTS?.default('')
getByRole('button', { name })
})

it('should render the <ActionListButton /> with the delete variant and the title to delete from the list', () => {
const listNameTest = 'LIST_TO_DELETE_TEST'

const { getByRole } = render(
<ActionListButton
action={() => {}}
Icon={() => null}
variant='delete'
nameList={listNameTest}
/>
)

const name = TITLE_VARIANTS?.delete(listNameTest)
getByRole('button', { name })
})

it('should execute the callback provided when is clicked', async () => {
const spy = vi.fn()

const { getByRole } = render(
<ActionListButton
action={spy}
Icon={() => null}
/>
)

fireEvent.click(getByRole('button'))

await waitFor(() => {
expect(spy).toHaveBeenCalledOnce()
})
})

0 comments on commit f7dc260

Please sign in to comment.