| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- import { useState } from 'react'
- import { beforeEach, describe, expect, it, vi } from 'vitest'
- import { render } from 'vitest-browser-react'
- import { userEvent } from 'vitest/browser'
- import { showSubmittedData } from '@/lib/show-submitted-data'
- import { type User } from '../data/schema'
- import { UsersDeleteDialog } from './users-delete-dialog'
- vi.mock('@/lib/show-submitted-data', () => ({ showSubmittedData: vi.fn() }))
- const MOCK_USER: User = {
- id: 'user-delete-test',
- firstName: 'John',
- lastName: 'Doe',
- username: 'john_doe',
- email: 'johndoe@shadcn-admin.com',
- phoneNumber: '+959123456789',
- status: 'active',
- role: 'manager',
- createdAt: new Date('2026-01-01'),
- updatedAt: new Date('2026-02-02'),
- }
- describe('UsersDeleteDialog', () => {
- beforeEach(() => vi.clearAllMocks())
- it('renders the dialog with the correct title, description, input and buttons', async () => {
- const { getByText, getByRole } = await render(
- <UsersDeleteDialog open onOpenChange={vi.fn()} currentRow={MOCK_USER} />
- )
- const title = getByRole('heading', {
- level: 2,
- name: /Delete User/i,
- })
- const desc = getByText(
- new RegExp(`Are you sure you want to delete ${MOCK_USER.username}?`, 'i')
- )
- const usernameInput = getByRole('textbox', { name: /Username/i })
- const cancelButton = getByRole('button', { name: /Cancel/i })
- const deleteButton = getByRole('button', { name: /Delete/i })
- expect(title).toBeInTheDocument()
- expect(desc).toBeInTheDocument()
- expect(usernameInput).toBeInTheDocument()
- expect(cancelButton).toBeInTheDocument()
- expect(deleteButton).toBeInTheDocument()
- expect(deleteButton).toBeDisabled()
- })
- it('keeps the delete button disabled until the username input is filled correctly', async () => {
- const { getByRole } = await render(
- <UsersDeleteDialog open onOpenChange={vi.fn()} currentRow={MOCK_USER} />
- )
- const usernameInput = getByRole('textbox', { name: /Username/i })
- const deleteButton = getByRole('button', { name: /Delete/i })
- expect(deleteButton).toBeDisabled()
- await userEvent.fill(usernameInput, 'wrong-username')
- expect(deleteButton).toBeDisabled()
- await userEvent.fill(usernameInput, MOCK_USER.username)
- expect(deleteButton).toBeEnabled()
- })
- it('closes the dialog when the cancel button is clicked', async () => {
- const onOpenChange = vi.fn()
- const { getByRole } = await render(
- <UsersDeleteDialog
- open
- onOpenChange={onOpenChange}
- currentRow={MOCK_USER}
- />
- )
- const cancelButton = getByRole('button', { name: /Cancel/i })
- await userEvent.click(cancelButton)
- expect(onOpenChange).toHaveBeenCalledOnce()
- expect(onOpenChange).toHaveBeenCalledWith(false)
- })
- it('resets the username input when the dialog is closed and reopened', async () => {
- function Harness() {
- const [open, setOpen] = useState(true)
- return (
- <>
- <button type='button' onClick={() => setOpen(true)}>
- Reopen
- </button>
- {open ? (
- <UsersDeleteDialog
- open={open}
- onOpenChange={setOpen}
- currentRow={MOCK_USER}
- />
- ) : null}
- </>
- )
- }
- const { getByRole } = await render(<Harness />)
- const usernameInput = getByRole('textbox', { name: /Username/i })
- await userEvent.fill(usernameInput, MOCK_USER.username)
- expect(usernameInput).toHaveValue(MOCK_USER.username)
- const closeButton = getByRole('button', { name: /Cancel/i })
- await userEvent.click(closeButton)
- const reopenButton = getByRole('button', { name: /Reopen/i })
- await userEvent.click(reopenButton)
- expect(usernameInput).toHaveValue('')
- })
- it('shows the submitted data when deleted successfully', async () => {
- const onOpenChange = vi.fn()
- const { getByRole } = await render(
- <UsersDeleteDialog
- open
- onOpenChange={onOpenChange}
- currentRow={MOCK_USER}
- />
- )
- const usernameInput = getByRole('textbox', { name: /Username/i })
- const deleteButton = getByRole('button', { name: /Delete/i })
- expect(deleteButton).toBeDisabled()
- await userEvent.fill(usernameInput, MOCK_USER.username)
- expect(deleteButton).toBeEnabled()
- await userEvent.click(deleteButton)
- expect(onOpenChange).toHaveBeenCalledOnce()
- expect(onOpenChange).toHaveBeenCalledWith(false)
- expect(showSubmittedData).toHaveBeenCalledOnce()
- expect(showSubmittedData).toHaveBeenCalledWith(
- MOCK_USER,
- 'The following user has been deleted:'
- )
- })
- it('deletes successfully when press Enter key on the username input', async () => {
- const onOpenChange = vi.fn()
- const { getByRole } = await render(
- <UsersDeleteDialog
- open
- onOpenChange={onOpenChange}
- currentRow={MOCK_USER}
- />
- )
- const usernameInput = getByRole('textbox', { name: /Username/i })
- const deleteButton = getByRole('button', { name: /Delete/i })
- expect(deleteButton).toBeDisabled()
- await userEvent.fill(usernameInput, MOCK_USER.username)
- expect(deleteButton).toBeEnabled()
- await userEvent.keyboard('{Enter}')
- expect(onOpenChange).toHaveBeenCalledOnce()
- expect(onOpenChange).toHaveBeenCalledWith(false)
- expect(showSubmittedData).toHaveBeenCalledOnce()
- expect(showSubmittedData).toHaveBeenCalledWith(
- MOCK_USER,
- 'The following user has been deleted:'
- )
- })
- })
|