@@ -36,22 +36,26 @@ describe("ComposedModal", () => {
3636 } ) ;
3737
3838 it ( "should handle open state" , async ( ) => {
39- const consoleLog = vi . spyOn ( console , "log" ) ;
4039 const { component } = render ( ComposedModalTest , {
4140 props : {
4241 open : false ,
4342 headerTitle : "Test Modal" ,
4443 } ,
4544 } ) ;
4645
46+ const openHandler = vi . fn ( ) ;
47+ const closeHandler = vi . fn ( ) ;
48+ component . $on ( "open" , openHandler ) ;
49+ component . $on ( "close" , closeHandler ) ;
50+
4751 component . $set ( { open : true } ) ;
4852 await tick ( ) ;
4953 expect ( screen . getByRole ( "dialog" ) ) . toBeInTheDocument ( ) ;
50- expect ( consoleLog ) . toHaveBeenCalledWith ( "open" ) ;
54+ expect ( openHandler ) . toHaveBeenCalledTimes ( 1 ) ;
5155
5256 component . $set ( { open : false } ) ;
5357 await tick ( ) ;
54- expect ( consoleLog ) . toHaveBeenCalledWith ( "close" ) ;
58+ expect ( closeHandler ) . toHaveBeenCalledTimes ( 1 ) ;
5559 } ) ;
5660
5761 it ( "should handle size variants" , ( ) => {
@@ -106,21 +110,6 @@ describe("ComposedModal", () => {
106110 expect ( modalWrapper ) . not . toHaveClass ( "is-visible" ) ;
107111 } ) ;
108112
109- it ( "should close on outside click" , async ( ) => {
110- const consoleLog = vi . spyOn ( console , "log" ) ;
111- const { container } = render ( ComposedModalTest , {
112- props : {
113- open : true ,
114- headerTitle : "Test Modal" ,
115- } ,
116- } ) ;
117-
118- const modalWrapper = container . querySelector ( ".bx--modal" ) ;
119- assert ( modalWrapper ) ;
120- await user . click ( modalWrapper ) ;
121- expect ( consoleLog ) . toHaveBeenCalledWith ( "close" ) ;
122- } ) ;
123-
124113 it ( "should not close on inside click" , async ( ) => {
125114 const consoleLog = vi . spyOn ( console , "log" ) ;
126115 render ( ComposedModalTest , {
@@ -151,20 +140,6 @@ describe("ComposedModal", () => {
151140 expect ( consoleLog ) . not . toHaveBeenCalledWith ( "close" ) ;
152141 } ) ;
153142
154- it ( "should handle close button click" , async ( ) => {
155- const consoleLog = vi . spyOn ( console , "log" ) ;
156- render ( ComposedModalTest , {
157- props : {
158- open : true ,
159- headerTitle : "Test Modal" ,
160- } ,
161- } ) ;
162-
163- const closeButton = screen . getByRole ( "button" , { name : "Close" } ) ;
164- await user . click ( closeButton ) ;
165- expect ( consoleLog ) . toHaveBeenCalledWith ( "close" ) ;
166- } ) ;
167-
168143 it ( "should render header with title and label" , ( ) => {
169144 render ( ComposedModalTest , {
170145 props : {
@@ -207,23 +182,6 @@ describe("ComposedModal", () => {
207182 expect ( consoleLog ) . toHaveBeenCalledWith ( "click:button--primary" ) ;
208183 } ) ;
209184
210- it ( "should handle secondary button click" , async ( ) => {
211- const consoleLog = vi . spyOn ( console , "log" ) ;
212- render ( ComposedModalTest , {
213- props : {
214- open : true ,
215- headerTitle : "Test Modal" ,
216- footerSecondaryButtonText : "Cancel" ,
217- } ,
218- } ) ;
219-
220- await user . click ( screen . getByRole ( "button" , { name : "Cancel" } ) ) ;
221- expect ( consoleLog ) . toHaveBeenCalledWith ( "click:button--secondary" , {
222- text : "Cancel" ,
223- } ) ;
224- expect ( consoleLog ) . toHaveBeenCalledWith ( "close" ) ;
225- } ) ;
226-
227185 it ( "should disable primary button when configured" , ( ) => {
228186 render ( ComposedModalTest , {
229187 props : {
@@ -343,4 +301,76 @@ describe("ComposedModal", () => {
343301 const modalWrapper = container . querySelector ( ".bx--modal" ) ;
344302 expect ( modalWrapper ) . toHaveClass ( "is-visible" ) ;
345303 } ) ;
304+
305+ it ( "dispatches close event with outside-click trigger" , async ( ) => {
306+ const { container, component } = render ( ComposedModalTest , {
307+ props : {
308+ open : true ,
309+ headerTitle : "Outside Click Test" ,
310+ } ,
311+ } ) ;
312+
313+ const closeHandler = vi . fn ( ) ;
314+ component . $on ( "close" , closeHandler ) ;
315+
316+ const modalOverlay = container . querySelector ( ".bx--modal" ) ;
317+ assert ( modalOverlay ) ;
318+ await user . click ( modalOverlay ) ;
319+ await tick ( ) ;
320+
321+ expect ( closeHandler ) . toHaveBeenCalledTimes ( 1 ) ;
322+ expect ( closeHandler . mock . calls [ 0 ] [ 0 ] . detail ) . toEqual ( {
323+ trigger : "outside-click" ,
324+ } ) ;
325+ } ) ;
326+
327+ it ( "dispatches close event with close-button trigger" , async ( ) => {
328+ const { component } = render ( ComposedModalTest , {
329+ props : {
330+ open : true ,
331+ headerTitle : "Close Button Test" ,
332+ } ,
333+ } ) ;
334+
335+ const closeHandler = vi . fn ( ) ;
336+ component . $on ( "close" , closeHandler ) ;
337+
338+ const closeButton = screen . getByLabelText ( "Close" ) ;
339+ await user . click ( closeButton ) ;
340+ await tick ( ) ;
341+
342+ expect ( closeHandler ) . toHaveBeenCalledTimes ( 1 ) ;
343+ expect ( closeHandler . mock . calls [ 0 ] [ 0 ] . detail ) . toEqual ( {
344+ trigger : "close-button" ,
345+ } ) ;
346+ } ) ;
347+
348+ it ( "prevents closing when preventDefault is called on close event" , async ( ) => {
349+ const { container, component } = render ( ComposedModalTest , {
350+ props : {
351+ open : true ,
352+ headerTitle : "Prevent Close Test" ,
353+ } ,
354+ } ) ;
355+
356+ const closeHandler = vi . fn ( ( e ) => {
357+ e . preventDefault ( ) ;
358+ } ) ;
359+ component . $on ( "close" , closeHandler ) ;
360+
361+ // Close via outside click.
362+ const modalOverlay = container . querySelector ( ".bx--modal" ) ;
363+ assert ( modalOverlay ) ;
364+ await user . click ( modalOverlay ) ;
365+ await tick ( ) ;
366+ expect ( closeHandler ) . toHaveBeenCalledTimes ( 1 ) ;
367+ expect ( screen . getByRole ( "dialog" ) ) . toBeInTheDocument ( ) ;
368+
369+ // Close via close button.
370+ const closeButton = screen . getByLabelText ( "Close" ) ;
371+ await user . click ( closeButton ) ;
372+ await tick ( ) ;
373+ expect ( closeHandler ) . toHaveBeenCalledTimes ( 2 ) ;
374+ expect ( screen . getByRole ( "dialog" ) ) . toBeInTheDocument ( ) ;
375+ } ) ;
346376} ) ;
0 commit comments