@@ -2,6 +2,7 @@ import { render, screen, fireEvent } from '@testing-library/react'
22import { describe , it , expect , vi } from 'vitest'
33import userEvent from '@testing-library/user-event'
44import React from 'react'
5+ import '@testing-library/jest-dom'
56import {
67 Dialog ,
78 DialogTrigger ,
@@ -117,7 +118,7 @@ describe('Dialog Components', () => {
117118
118119 it ( 'applies proper classes to dialog content' , async ( ) => {
119120 const user = userEvent . setup ( )
120-
121+
121122 render (
122123 < Dialog >
123124 < DialogTrigger > Open Dialog</ DialogTrigger >
@@ -128,27 +129,38 @@ describe('Dialog Components', () => {
128129 </ DialogContent >
129130 </ Dialog >
130131 )
131-
132+
132133 await user . click ( screen . getByText ( 'Open Dialog' ) )
133-
134+
134135 const dialogContent = screen . getByRole ( 'dialog' )
135136 expect ( dialogContent ) . toHaveClass (
136137 'bg-main-view' ,
138+ 'max-h-[calc(100%-80px)]' ,
139+ 'overflow-auto' ,
140+ 'border-main-view-fg/10' ,
141+ 'text-main-view-fg' ,
137142 'fixed' ,
138143 'top-[50%]' ,
139144 'left-[50%]' ,
140- 'z-50' ,
145+ 'z-[90]' ,
146+ 'grid' ,
147+ 'w-full' ,
148+ 'max-w-[calc(100%-2rem)]' ,
141149 'translate-x-[-50%]' ,
142150 'translate-y-[-50%]' ,
143- 'border ' ,
151+ 'gap-4 ' ,
144152 'rounded-lg' ,
145- 'shadow-lg'
153+ 'border' ,
154+ 'p-6' ,
155+ 'shadow-lg' ,
156+ 'duration-200' ,
157+ 'sm:max-w-lg'
146158 )
147159 } )
148160
149161 it ( 'applies proper classes to dialog header' , async ( ) => {
150162 const user = userEvent . setup ( )
151-
163+
152164 render (
153165 < Dialog >
154166 < DialogTrigger > Open Dialog</ DialogTrigger >
@@ -159,11 +171,11 @@ describe('Dialog Components', () => {
159171 </ DialogContent >
160172 </ Dialog >
161173 )
162-
174+
163175 await user . click ( screen . getByText ( 'Open Dialog' ) )
164-
176+
165177 const dialogHeader = screen . getByText ( 'Dialog Title' ) . closest ( 'div' )
166- expect ( dialogHeader ) . toHaveClass ( 'flex' , 'flex-col' , 'gap-2' , 'text-center' )
178+ expect ( dialogHeader ) . toHaveClass ( 'flex' , 'flex-col' , 'gap-2' , 'text-center' , 'sm:text-left' )
167179 } )
168180
169181 it ( 'applies proper classes to dialog title' , async ( ) => {
@@ -299,7 +311,7 @@ describe('Dialog Components', () => {
299311 it ( 'supports onOpenChange callback' , async ( ) => {
300312 const onOpenChange = vi . fn ( )
301313 const user = userEvent . setup ( )
302-
314+
303315 render (
304316 < Dialog onOpenChange = { onOpenChange } >
305317 < DialogTrigger > Open Dialog</ DialogTrigger >
@@ -310,9 +322,98 @@ describe('Dialog Components', () => {
310322 </ DialogContent >
311323 </ Dialog >
312324 )
313-
325+
314326 await user . click ( screen . getByText ( 'Open Dialog' ) )
315-
327+
316328 expect ( onOpenChange ) . toHaveBeenCalledWith ( true )
317329 } )
330+
331+ it ( 'can hide close button when showCloseButton is false' , async ( ) => {
332+ const user = userEvent . setup ( )
333+
334+ render (
335+ < Dialog >
336+ < DialogTrigger > Open Dialog</ DialogTrigger >
337+ < DialogContent showCloseButton = { false } >
338+ < DialogHeader >
339+ < DialogTitle > Dialog Title</ DialogTitle >
340+ </ DialogHeader >
341+ </ DialogContent >
342+ </ Dialog >
343+ )
344+
345+ await user . click ( screen . getByText ( 'Open Dialog' ) )
346+
347+ expect ( screen . queryByRole ( 'button' , { name : / c l o s e / i } ) ) . not . toBeInTheDocument ( )
348+ } )
349+
350+ it ( 'shows close button by default' , async ( ) => {
351+ const user = userEvent . setup ( )
352+
353+ render (
354+ < Dialog >
355+ < DialogTrigger > Open Dialog</ DialogTrigger >
356+ < DialogContent >
357+ < DialogHeader >
358+ < DialogTitle > Dialog Title</ DialogTitle >
359+ </ DialogHeader >
360+ </ DialogContent >
361+ </ Dialog >
362+ )
363+
364+ await user . click ( screen . getByText ( 'Open Dialog' ) )
365+
366+ expect ( screen . getByRole ( 'button' , { name : / c l o s e / i } ) ) . toBeInTheDocument ( )
367+ } )
368+
369+ it ( 'accepts aria-describedby prop' , async ( ) => {
370+ const user = userEvent . setup ( )
371+
372+ render (
373+ < Dialog >
374+ < DialogTrigger > Open Dialog</ DialogTrigger >
375+ < DialogContent aria-describedby = "custom-description" >
376+ < DialogHeader >
377+ < DialogTitle > Dialog Title</ DialogTitle >
378+ </ DialogHeader >
379+ < p id = "custom-description" > Custom description text</ p >
380+ </ DialogContent >
381+ </ Dialog >
382+ )
383+
384+ await user . click ( screen . getByText ( 'Open Dialog' ) )
385+
386+ const dialogContent = screen . getByRole ( 'dialog' )
387+ expect ( dialogContent ) . toHaveAttribute ( 'aria-describedby' , 'custom-description' )
388+ } )
389+
390+ it ( 'applies data-slot attributes to components' , async ( ) => {
391+ const user = userEvent . setup ( )
392+
393+ render (
394+ < Dialog >
395+ < DialogTrigger > Open Dialog</ DialogTrigger >
396+ < DialogContent >
397+ < DialogHeader >
398+ < DialogTitle > Dialog Title</ DialogTitle >
399+ < DialogDescription > Dialog description</ DialogDescription >
400+ </ DialogHeader >
401+ < div > Dialog body content</ div >
402+ < DialogFooter >
403+ < button > Footer button</ button >
404+ </ DialogFooter >
405+ </ DialogContent >
406+ </ Dialog >
407+ )
408+
409+ expect ( screen . getByText ( 'Open Dialog' ) ) . toHaveAttribute ( 'data-slot' , 'dialog-trigger' )
410+
411+ await user . click ( screen . getByText ( 'Open Dialog' ) )
412+
413+ expect ( screen . getByRole ( 'dialog' ) ) . toHaveAttribute ( 'data-slot' , 'dialog-content' )
414+ expect ( screen . getByText ( 'Dialog Title' ) . closest ( 'div' ) ) . toHaveAttribute ( 'data-slot' , 'dialog-header' )
415+ expect ( screen . getByText ( 'Dialog Title' ) ) . toHaveAttribute ( 'data-slot' , 'dialog-title' )
416+ expect ( screen . getByText ( 'Dialog description' ) ) . toHaveAttribute ( 'data-slot' , 'dialog-description' )
417+ expect ( screen . getByText ( 'Footer button' ) . closest ( 'div' ) ) . toHaveAttribute ( 'data-slot' , 'dialog-footer' )
418+ } )
318419} )
0 commit comments