Skip to content

Commit

Permalink
feat(Toast): Expose dismissToast function
Browse files Browse the repository at this point in the history
  • Loading branch information
m7kvqbe1 committed Nov 26, 2024
1 parent 427642b commit fb6d0ab
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useEffect } from 'react'
import { Meta, StoryFn } from '@storybook/react'

import { TOAST_APPEARANCE, Toast, ToastProps, showToast } from '.'
import { TOAST_APPEARANCE, Toast, ToastProps, showToast, dismissToast } from '.'
import { Button } from '../Button'
import { Group } from '../Group'

export default {
component: Toast,
Expand Down Expand Up @@ -31,13 +32,22 @@ const ToastButton = (props: ToastProps) => {
return (
<div style={{ minHeight: '10rem' }}>
<Toast {...props} />
<Button
onClick={(_: React.FormEvent<HTMLButtonElement>) => {
showToast({ label: 'another', message: DESCRIPTION })
}}
>
Show Toast
</Button>
<Group gap="6">
<Button
onClick={(_: React.FormEvent<HTMLButtonElement>) => {
showToast({ label: 'Another Toast', message: DESCRIPTION })
}}
>
Show Toast
</Button>
<Button
onClick={(_: React.FormEvent<HTMLButtonElement>) => {
dismissToast()
}}
>
Dismiss All Toasts
</Button>
</Group>
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react'
import { color } from '@royalnavy/design-tokens'
import { render, screen, waitFor, within } from '@testing-library/react'
import { render, screen, within, waitFor } from '@testing-library/react'

import { TOAST_APPEARANCE, Toast, showToast } from '.'
import { TOAST_APPEARANCE, Toast, dismissToast, showToast } from '.'

const LABEL = 'Example label'
const MESSAGE = 'This is an example toast message'
Expand All @@ -19,12 +19,20 @@ function setup() {

/* Added this function to resolve an issue when running tests for
* the whole of this file and more than one toast is shown */
async function getLastToast(lastToastLabel: string) {
await waitFor(() => {
expect(screen.getByText(lastToastLabel)).toBeInTheDocument()
})

return screen.getAllByRole('status')[0]
async function getLastToast(label?: string) {
try {
const toasts = await screen.findAllByRole('status', {
name: label ? new RegExp(label, 'i') : undefined,
})

return toasts[0]
} catch (error) {
if (error instanceof Error && error.message.includes('Unable to find')) {
return null
}

throw error
}
}

it('renders the toast', async () => {
Expand All @@ -41,11 +49,11 @@ it('renders the toast', async () => {
const contentId = screen.getByText(MESSAGE).getAttribute('id')
expect(lastToast).toHaveAttribute('aria-describedby', contentId)

expect(within(lastToast).getByTestId('icon')).toHaveAttribute(
expect(within(lastToast!).getByTestId('icon')).toHaveAttribute(
'aria-hidden',
'true'
)
expect(within(lastToast).getByTestId('icon')).toHaveStyle({
expect(within(lastToast!).getByTestId('icon')).toHaveStyle({
color: color('action', '500'),
})

Expand All @@ -66,9 +74,9 @@ it('sets new props when `showToast` is called with new props', async () => {

const lastToast = await getLastToast(expectedNewLabel)

expect(within(lastToast).getByText(expectedNewLabel)).toBeInTheDocument()
expect(within(lastToast).getByText(expectedNewMessage)).toBeInTheDocument()
expect(within(lastToast).getByTestId('icon')).toHaveStyle({
expect(within(lastToast!).getByText(expectedNewLabel)).toBeInTheDocument()
expect(within(lastToast!).getByText(expectedNewMessage)).toBeInTheDocument()
expect(within(lastToast!).getByTestId('icon')).toHaveStyle({
color: color('danger', '500'),
})
})
Expand All @@ -91,8 +99,8 @@ it('sets the message when the message is JSX', async () => {

const lastToast = await getLastToast(expectedNewLabel)

expect(within(lastToast).getByText(expectedNewLabel)).toBeInTheDocument()
expect(within(lastToast).getAllByRole('paragraph')).toHaveLength(2)
expect(within(lastToast!).getByText(expectedNewLabel)).toBeInTheDocument()
expect(within(lastToast!).getAllByRole('paragraph')).toHaveLength(2)
})

it('sets unique IDs when there are multiple toasts', async () => {
Expand Down Expand Up @@ -121,3 +129,18 @@ it('sets an accessible name when there is no message', async () => {

expect(toast).toHaveAccessibleName(new RegExp(label))
})

it('dismisses all toasts when `dismissToast` is called', async () => {
setup()

showToast(MESSAGE)
showToast(MESSAGE)

expect(screen.queryAllByRole('status').length).toBeGreaterThan(0)

dismissToast()

await waitFor(() => {
expect(screen.queryAllByRole('status')).toHaveLength(0)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,7 @@ export const showToast = (
})
}
}

export const dismissToast = (id?: string) => {
toast.dismiss(id)
}

0 comments on commit fb6d0ab

Please sign in to comment.