dialog.test.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { render, fireEvent } from '@testing-library/react'
  2. import {
  3. Dialog,
  4. DialogContent,
  5. DialogHeader,
  6. DialogTitle,
  7. DialogDescription,
  8. DialogFooter
  9. } from '@/components/ui/dialog'
  10. describe('Dialog 组件', () => {
  11. const mockOnOpenChange = jest.fn()
  12. beforeEach(() => {
  13. jest.clearAllMocks()
  14. })
  15. describe('Dialog 主组件', () => {
  16. it('应该渲染对话框当 open 为 true 时', () => {
  17. const { getByText } = render(
  18. <Dialog open={true} onOpenChange={mockOnOpenChange}>
  19. <div>对话框内容</div>
  20. </Dialog>
  21. )
  22. expect(getByText('对话框内容')).toBeTruthy()
  23. })
  24. it('不应该渲染对话框当 open 为 false 时', () => {
  25. const { queryByText } = render(
  26. <Dialog open={false} onOpenChange={mockOnOpenChange}>
  27. <div>对话框内容</div>
  28. </Dialog>
  29. )
  30. expect(queryByText('对话框内容')).toBeNull()
  31. })
  32. it('应该调用 onOpenChange(false) 当点击背景遮罩时', () => {
  33. const { container } = render(
  34. <Dialog open={true} onOpenChange={mockOnOpenChange}>
  35. <div>对话框内容</div>
  36. </Dialog>
  37. )
  38. // 找到背景遮罩元素
  39. const backdrop = container.querySelector('.fixed')
  40. expect(backdrop).toBeTruthy()
  41. if (backdrop) {
  42. fireEvent.click(backdrop)
  43. expect(mockOnOpenChange).toHaveBeenCalledWith(false)
  44. }
  45. })
  46. it('不应该调用 onOpenChange 当点击内容区域时', () => {
  47. const { getByText } = render(
  48. <Dialog open={true} onOpenChange={mockOnOpenChange}>
  49. <div>对话框内容</div>
  50. </Dialog>
  51. )
  52. const content = getByText('对话框内容')
  53. fireEvent.click(content)
  54. expect(mockOnOpenChange).not.toHaveBeenCalled()
  55. })
  56. it('应该应用正确的样式类名', () => {
  57. const { container } = render(
  58. <Dialog open={true} onOpenChange={mockOnOpenChange}>
  59. <div>对话框内容</div>
  60. </Dialog>
  61. )
  62. const backdrop = container.querySelector('.fixed')
  63. const content = container.querySelector('.bg-white')
  64. expect(backdrop).toBeTruthy()
  65. expect(content).toBeTruthy()
  66. expect(backdrop).toHaveClass('fixed', 'inset-0', 'z-50', 'flex', 'items-center', 'justify-center', 'bg-black/50')
  67. expect(content).toHaveClass('bg-white', 'rounded-lg', 'shadow-lg', 'max-w-md', 'w-full', 'mx-4')
  68. })
  69. })
  70. describe('DialogContent 组件', () => {
  71. it('应该渲染内容并应用类名', () => {
  72. const { getByText, container } = render(
  73. <DialogContent className="custom-class">
  74. 对话框内容
  75. </DialogContent>
  76. )
  77. const content = getByText('对话框内容')
  78. expect(content).toBeTruthy()
  79. // 直接检查包含内容的div元素
  80. const contentDiv = container.querySelector('div')
  81. expect(contentDiv).toBeTruthy()
  82. expect(contentDiv).toHaveClass('p-6', 'custom-class')
  83. })
  84. it('应该使用默认类名', () => {
  85. const { getByText, container } = render(
  86. <DialogContent>
  87. 对话框内容
  88. </DialogContent>
  89. )
  90. const content = getByText('对话框内容')
  91. expect(content).toBeTruthy()
  92. // 直接检查包含内容的div元素
  93. const contentDiv = container.querySelector('div')
  94. expect(contentDiv).toBeTruthy()
  95. expect(contentDiv).toHaveClass('p-6')
  96. })
  97. })
  98. describe('DialogHeader 组件', () => {
  99. it('应该渲染头部并应用类名', () => {
  100. const { getByText, container } = render(
  101. <DialogHeader className="custom-header">
  102. 对话框头部
  103. </DialogHeader>
  104. )
  105. const header = getByText('对话框头部')
  106. expect(header).toBeTruthy()
  107. // 直接检查包含头部的div元素
  108. const headerDiv = container.querySelector('div')
  109. expect(headerDiv).toBeTruthy()
  110. expect(headerDiv).toHaveClass('mb-4', 'custom-header')
  111. })
  112. it('应该使用默认类名', () => {
  113. const { getByText, container } = render(
  114. <DialogHeader>
  115. 对话框头部
  116. </DialogHeader>
  117. )
  118. const header = getByText('对话框头部')
  119. expect(header).toBeTruthy()
  120. // 直接检查包含头部的div元素
  121. const headerDiv = container.querySelector('div')
  122. expect(headerDiv).toBeTruthy()
  123. expect(headerDiv).toHaveClass('mb-4')
  124. })
  125. })
  126. describe('DialogTitle 组件', () => {
  127. it('应该渲染标题并应用类名', () => {
  128. const { getByText } = render(
  129. <DialogTitle className="custom-title">
  130. 对话框标题
  131. </DialogTitle>
  132. )
  133. const title = getByText('对话框标题')
  134. expect(title).toBeTruthy()
  135. expect(title).toHaveClass('text-lg', 'font-semibold', 'text-gray-900', 'custom-title')
  136. })
  137. it('应该使用默认类名', () => {
  138. const { getByText } = render(
  139. <DialogTitle>
  140. 对话框标题
  141. </DialogTitle>
  142. )
  143. const title = getByText('对话框标题')
  144. expect(title).toHaveClass('text-lg', 'font-semibold', 'text-gray-900')
  145. })
  146. })
  147. describe('DialogDescription 组件', () => {
  148. it('应该渲染描述并应用类名', () => {
  149. const { getByText } = render(
  150. <DialogDescription className="custom-desc">
  151. 对话框描述
  152. </DialogDescription>
  153. )
  154. const description = getByText('对话框描述')
  155. expect(description).toBeTruthy()
  156. expect(description).toHaveClass('text-sm', 'text-gray-600', 'custom-desc')
  157. })
  158. it('应该使用默认类名', () => {
  159. const { getByText } = render(
  160. <DialogDescription>
  161. 对话框描述
  162. </DialogDescription>
  163. )
  164. const description = getByText('对话框描述')
  165. expect(description).toHaveClass('text-sm', 'text-gray-600')
  166. })
  167. })
  168. describe('DialogFooter 组件', () => {
  169. it('应该渲染底部并应用类名', () => {
  170. const { getByText, container } = render(
  171. <DialogFooter className="custom-footer">
  172. 对话框底部
  173. </DialogFooter>
  174. )
  175. const footer = getByText('对话框底部')
  176. expect(footer).toBeTruthy()
  177. // 直接检查包含底部的div元素
  178. const footerDiv = container.querySelector('div')
  179. expect(footerDiv).toBeTruthy()
  180. expect(footerDiv).toHaveClass('flex', 'justify-end', 'space-x-2', 'custom-footer')
  181. })
  182. it('应该使用默认类名', () => {
  183. const { getByText, container } = render(
  184. <DialogFooter>
  185. 对话框底部
  186. </DialogFooter>
  187. )
  188. const footer = getByText('对话框底部')
  189. expect(footer).toBeTruthy()
  190. // 直接检查包含底部的div元素
  191. const footerDiv = container.querySelector('div')
  192. expect(footerDiv).toBeTruthy()
  193. expect(footerDiv).toHaveClass('flex', 'justify-end', 'space-x-2')
  194. })
  195. })
  196. describe('完整对话框示例', () => {
  197. it('应该渲染完整的对话框结构', () => {
  198. const { getByText } = render(
  199. <Dialog open={true} onOpenChange={mockOnOpenChange}>
  200. <DialogContent>
  201. <DialogHeader>
  202. <DialogTitle>确认操作</DialogTitle>
  203. <DialogDescription>
  204. 您确定要执行此操作吗?
  205. </DialogDescription>
  206. </DialogHeader>
  207. <DialogFooter>
  208. <button>取消</button>
  209. <button>确认</button>
  210. </DialogFooter>
  211. </DialogContent>
  212. </Dialog>
  213. )
  214. expect(getByText('确认操作')).toBeTruthy()
  215. expect(getByText('您确定要执行此操作吗?')).toBeTruthy()
  216. expect(getByText('取消')).toBeTruthy()
  217. expect(getByText('确认')).toBeTruthy()
  218. })
  219. it('应该正确处理事件冒泡', () => {
  220. const { getByText } = render(
  221. <Dialog open={true} onOpenChange={mockOnOpenChange}>
  222. <DialogContent>
  223. <DialogHeader>
  224. <DialogTitle>测试事件</DialogTitle>
  225. </DialogHeader>
  226. <button onClick={() => {}}>测试按钮</button>
  227. </DialogContent>
  228. </Dialog>
  229. )
  230. const button = getByText('测试按钮')
  231. fireEvent.click(button)
  232. // 点击按钮不应该触发对话框关闭
  233. expect(mockOnOpenChange).not.toHaveBeenCalled()
  234. })
  235. })
  236. })