users-delete-dialog.test.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { useState } from 'react'
  2. import { beforeEach, describe, expect, it, vi } from 'vitest'
  3. import { render } from 'vitest-browser-react'
  4. import { userEvent } from 'vitest/browser'
  5. import { showSubmittedData } from '@/lib/show-submitted-data'
  6. import { type User } from '../data/schema'
  7. import { UsersDeleteDialog } from './users-delete-dialog'
  8. vi.mock('@/lib/show-submitted-data', () => ({ showSubmittedData: vi.fn() }))
  9. const MOCK_USER: User = {
  10. id: 'user-delete-test',
  11. firstName: 'John',
  12. lastName: 'Doe',
  13. username: 'john_doe',
  14. email: 'johndoe@shadcn-admin.com',
  15. phoneNumber: '+959123456789',
  16. status: 'active',
  17. role: 'manager',
  18. createdAt: new Date('2026-01-01'),
  19. updatedAt: new Date('2026-02-02'),
  20. }
  21. describe('UsersDeleteDialog', () => {
  22. beforeEach(() => vi.clearAllMocks())
  23. it('renders the dialog with the correct title, description, input and buttons', async () => {
  24. const { getByText, getByRole } = await render(
  25. <UsersDeleteDialog open onOpenChange={vi.fn()} currentRow={MOCK_USER} />
  26. )
  27. const title = getByRole('heading', {
  28. level: 2,
  29. name: /Delete User/i,
  30. })
  31. const desc = getByText(
  32. new RegExp(`Are you sure you want to delete ${MOCK_USER.username}?`, 'i')
  33. )
  34. const usernameInput = getByRole('textbox', { name: /Username/i })
  35. const cancelButton = getByRole('button', { name: /Cancel/i })
  36. const deleteButton = getByRole('button', { name: /Delete/i })
  37. expect(title).toBeInTheDocument()
  38. expect(desc).toBeInTheDocument()
  39. expect(usernameInput).toBeInTheDocument()
  40. expect(cancelButton).toBeInTheDocument()
  41. expect(deleteButton).toBeInTheDocument()
  42. expect(deleteButton).toBeDisabled()
  43. })
  44. it('keeps the delete button disabled until the username input is filled correctly', async () => {
  45. const { getByRole } = await render(
  46. <UsersDeleteDialog open onOpenChange={vi.fn()} currentRow={MOCK_USER} />
  47. )
  48. const usernameInput = getByRole('textbox', { name: /Username/i })
  49. const deleteButton = getByRole('button', { name: /Delete/i })
  50. expect(deleteButton).toBeDisabled()
  51. await userEvent.fill(usernameInput, 'wrong-username')
  52. expect(deleteButton).toBeDisabled()
  53. await userEvent.fill(usernameInput, MOCK_USER.username)
  54. expect(deleteButton).toBeEnabled()
  55. })
  56. it('closes the dialog when the cancel button is clicked', async () => {
  57. const onOpenChange = vi.fn()
  58. const { getByRole } = await render(
  59. <UsersDeleteDialog
  60. open
  61. onOpenChange={onOpenChange}
  62. currentRow={MOCK_USER}
  63. />
  64. )
  65. const cancelButton = getByRole('button', { name: /Cancel/i })
  66. await userEvent.click(cancelButton)
  67. expect(onOpenChange).toHaveBeenCalledOnce()
  68. expect(onOpenChange).toHaveBeenCalledWith(false)
  69. })
  70. it('resets the username input when the dialog is closed and reopened', async () => {
  71. function Harness() {
  72. const [open, setOpen] = useState(true)
  73. return (
  74. <>
  75. <button type='button' onClick={() => setOpen(true)}>
  76. Reopen
  77. </button>
  78. {open ? (
  79. <UsersDeleteDialog
  80. open={open}
  81. onOpenChange={setOpen}
  82. currentRow={MOCK_USER}
  83. />
  84. ) : null}
  85. </>
  86. )
  87. }
  88. const { getByRole } = await render(<Harness />)
  89. const usernameInput = getByRole('textbox', { name: /Username/i })
  90. await userEvent.fill(usernameInput, MOCK_USER.username)
  91. expect(usernameInput).toHaveValue(MOCK_USER.username)
  92. const closeButton = getByRole('button', { name: /Cancel/i })
  93. await userEvent.click(closeButton)
  94. const reopenButton = getByRole('button', { name: /Reopen/i })
  95. await userEvent.click(reopenButton)
  96. expect(usernameInput).toHaveValue('')
  97. })
  98. it('shows the submitted data when deleted successfully', async () => {
  99. const onOpenChange = vi.fn()
  100. const { getByRole } = await render(
  101. <UsersDeleteDialog
  102. open
  103. onOpenChange={onOpenChange}
  104. currentRow={MOCK_USER}
  105. />
  106. )
  107. const usernameInput = getByRole('textbox', { name: /Username/i })
  108. const deleteButton = getByRole('button', { name: /Delete/i })
  109. expect(deleteButton).toBeDisabled()
  110. await userEvent.fill(usernameInput, MOCK_USER.username)
  111. expect(deleteButton).toBeEnabled()
  112. await userEvent.click(deleteButton)
  113. expect(onOpenChange).toHaveBeenCalledOnce()
  114. expect(onOpenChange).toHaveBeenCalledWith(false)
  115. expect(showSubmittedData).toHaveBeenCalledOnce()
  116. expect(showSubmittedData).toHaveBeenCalledWith(
  117. MOCK_USER,
  118. 'The following user has been deleted:'
  119. )
  120. })
  121. it('deletes successfully when press Enter key on the username input', async () => {
  122. const onOpenChange = vi.fn()
  123. const { getByRole } = await render(
  124. <UsersDeleteDialog
  125. open
  126. onOpenChange={onOpenChange}
  127. currentRow={MOCK_USER}
  128. />
  129. )
  130. const usernameInput = getByRole('textbox', { name: /Username/i })
  131. const deleteButton = getByRole('button', { name: /Delete/i })
  132. expect(deleteButton).toBeDisabled()
  133. await userEvent.fill(usernameInput, MOCK_USER.username)
  134. expect(deleteButton).toBeEnabled()
  135. await userEvent.keyboard('{Enter}')
  136. expect(onOpenChange).toHaveBeenCalledOnce()
  137. expect(onOpenChange).toHaveBeenCalledWith(false)
  138. expect(showSubmittedData).toHaveBeenCalledOnce()
  139. expect(showSubmittedData).toHaveBeenCalledWith(
  140. MOCK_USER,
  141. 'The following user has been deleted:'
  142. )
  143. })
  144. })