|
1 | 1 | import {render, fireEvent} from '@testing-library/react'
|
2 |
| -import {describe, it, expect} from 'vitest' |
| 2 | +import {describe, it, expect, vi} from 'vitest' |
3 | 3 | import type React from 'react'
|
4 | 4 | import {useCallback, useRef, useState} from 'react'
|
5 | 5 |
|
@@ -96,6 +96,37 @@ const CustomProps = ({
|
96 | 96 | )
|
97 | 97 | }
|
98 | 98 |
|
| 99 | +const LoadingStates = ({ |
| 100 | + confirmButtonLoading, |
| 101 | + cancelButtonLoading, |
| 102 | +}: Pick<React.ComponentProps<typeof ConfirmationDialog>, 'confirmButtonLoading' | 'cancelButtonLoading'>) => { |
| 103 | + const [isOpen, setIsOpen] = useState(false) |
| 104 | + const buttonRef = useRef<HTMLButtonElement>(null) |
| 105 | + const onDialogClose = useCallback(() => setIsOpen(false), []) |
| 106 | + return ( |
| 107 | + <ThemeProvider theme={theme}> |
| 108 | + <BaseStyles> |
| 109 | + <Button ref={buttonRef} onClick={() => setIsOpen(!isOpen)}> |
| 110 | + Show dialog |
| 111 | + </Button> |
| 112 | + {isOpen && ( |
| 113 | + <ConfirmationDialog |
| 114 | + title="Confirm" |
| 115 | + onClose={onDialogClose} |
| 116 | + cancelButtonContent="Cancel" |
| 117 | + confirmButtonContent="Delete" |
| 118 | + confirmButtonType="danger" |
| 119 | + confirmButtonLoading={confirmButtonLoading} |
| 120 | + cancelButtonLoading={cancelButtonLoading} |
| 121 | + > |
| 122 | + Are you sure you want to delete this? |
| 123 | + </ConfirmationDialog> |
| 124 | + )} |
| 125 | + </BaseStyles> |
| 126 | + </ThemeProvider> |
| 127 | + ) |
| 128 | +} |
| 129 | + |
99 | 130 | describe('ConfirmationDialog', () => {
|
100 | 131 | it('focuses the primary action when opened and the confirmButtonType is not set', async () => {
|
101 | 132 | const {getByText, getByRole} = render(<Basic />)
|
@@ -155,4 +186,117 @@ describe('ConfirmationDialog', () => {
|
155 | 186 | const dialog = getByRole('alertdialog')
|
156 | 187 | expect(dialog.getAttribute('data-height')).toBe('small')
|
157 | 188 | })
|
| 189 | + |
| 190 | + describe('loading states', () => { |
| 191 | + it('applies loading state to confirm button when confirmButtonLoading is true', async () => { |
| 192 | + const {getByText, getByRole} = render(<LoadingStates confirmButtonLoading={true} />) |
| 193 | + |
| 194 | + fireEvent.click(getByText('Show dialog')) |
| 195 | + |
| 196 | + const confirmButton = getByRole('button', {name: 'Delete'}) |
| 197 | + const cancelButton = getByRole('button', {name: 'Cancel'}) |
| 198 | + |
| 199 | + expect(confirmButton).toHaveAttribute('data-loading', 'true') |
| 200 | + expect(cancelButton).not.toHaveAttribute('data-loading', 'true') |
| 201 | + }) |
| 202 | + |
| 203 | + it('applies loading state to cancel button when cancelButtonLoading is true', async () => { |
| 204 | + const {getByText, getByRole} = render(<LoadingStates cancelButtonLoading={true} />) |
| 205 | + |
| 206 | + fireEvent.click(getByText('Show dialog')) |
| 207 | + |
| 208 | + const confirmButton = getByRole('button', {name: 'Delete'}) |
| 209 | + const cancelButton = getByRole('button', {name: 'Cancel'}) |
| 210 | + |
| 211 | + expect(cancelButton).toHaveAttribute('data-loading', 'true') |
| 212 | + expect(confirmButton).not.toHaveAttribute('data-loading', 'true') |
| 213 | + }) |
| 214 | + |
| 215 | + it('applies loading state to both buttons when both loading props are true', async () => { |
| 216 | + const {getByText, getByRole} = render(<LoadingStates confirmButtonLoading={true} cancelButtonLoading={true} />) |
| 217 | + |
| 218 | + fireEvent.click(getByText('Show dialog')) |
| 219 | + |
| 220 | + const confirmButton = getByRole('button', {name: 'Delete'}) |
| 221 | + const cancelButton = getByRole('button', {name: 'Cancel'}) |
| 222 | + |
| 223 | + expect(confirmButton).toHaveAttribute('data-loading', 'true') |
| 224 | + expect(cancelButton).toHaveAttribute('data-loading', 'true') |
| 225 | + }) |
| 226 | + |
| 227 | + it('disables button clicks when button is loading', async () => { |
| 228 | + const mockOnClose = vi.fn() |
| 229 | + const {getByRole} = render( |
| 230 | + <ThemeProvider theme={theme}> |
| 231 | + <BaseStyles> |
| 232 | + <ConfirmationDialog |
| 233 | + title="Confirm" |
| 234 | + onClose={mockOnClose} |
| 235 | + confirmButtonLoading={true} |
| 236 | + confirmButtonContent="Delete" |
| 237 | + cancelButtonContent="Cancel" |
| 238 | + > |
| 239 | + Test content |
| 240 | + </ConfirmationDialog> |
| 241 | + </BaseStyles> |
| 242 | + </ThemeProvider>, |
| 243 | + ) |
| 244 | + |
| 245 | + const confirmButton = getByRole('button', {name: 'Delete'}) |
| 246 | + |
| 247 | + fireEvent.click(confirmButton) |
| 248 | + |
| 249 | + // onClose should not be called when button is loading |
| 250 | + expect(mockOnClose).not.toHaveBeenCalled() |
| 251 | + }) |
| 252 | + |
| 253 | + it('shows loading spinner in confirm button when loading', async () => { |
| 254 | + const {getByText, getByRole} = render(<LoadingStates confirmButtonLoading={true} />) |
| 255 | + |
| 256 | + fireEvent.click(getByText('Show dialog')) |
| 257 | + |
| 258 | + const confirmButton = getByRole('button', {name: 'Delete'}) |
| 259 | + |
| 260 | + // Check for loading spinner (Spinner component renders as an SVG) |
| 261 | + const spinner = confirmButton.querySelector('svg') |
| 262 | + expect(spinner).toBeInTheDocument() |
| 263 | + expect(confirmButton.contains(spinner)).toBe(true) |
| 264 | + }) |
| 265 | + |
| 266 | + it('shows loading spinner in cancel button when loading', async () => { |
| 267 | + const {getByText, getByRole} = render(<LoadingStates cancelButtonLoading={true} />) |
| 268 | + |
| 269 | + fireEvent.click(getByText('Show dialog')) |
| 270 | + |
| 271 | + const cancelButton = getByRole('button', {name: 'Cancel'}) |
| 272 | + |
| 273 | + // Check for loading spinner in cancel button |
| 274 | + const spinner = cancelButton.querySelector('svg') |
| 275 | + expect(spinner).toBeInTheDocument() |
| 276 | + expect(cancelButton.contains(spinner)).toBe(true) |
| 277 | + }) |
| 278 | + |
| 279 | + it('maintains proper focus management when confirm button is loading', async () => { |
| 280 | + const {getByText, getByRole} = render(<LoadingStates confirmButtonLoading={true} />) |
| 281 | + |
| 282 | + fireEvent.click(getByText('Show dialog')) |
| 283 | + |
| 284 | + const cancelButton = getByRole('button', {name: 'Cancel'}) |
| 285 | + |
| 286 | + // When confirm button is loading and dangerous, focus should be on cancel button |
| 287 | + expect(cancelButton).toEqual(document.activeElement) |
| 288 | + }) |
| 289 | + |
| 290 | + it('does not apply loading state when loading props are false', async () => { |
| 291 | + const {getByText, getByRole} = render(<LoadingStates confirmButtonLoading={false} cancelButtonLoading={false} />) |
| 292 | + |
| 293 | + fireEvent.click(getByText('Show dialog')) |
| 294 | + |
| 295 | + const confirmButton = getByRole('button', {name: 'Delete'}) |
| 296 | + const cancelButton = getByRole('button', {name: 'Cancel'}) |
| 297 | + |
| 298 | + expect(confirmButton).not.toHaveAttribute('data-loading', 'true') |
| 299 | + expect(cancelButton).not.toHaveAttribute('data-loading', 'true') |
| 300 | + }) |
| 301 | + }) |
158 | 302 | })
|
0 commit comments