@@ -312,3 +312,257 @@ describe("Color Palette Item - tooltip", () => {
312312 . and ( "not.contain" , "#d60d5a" ) ;
313313 } ) ;
314314} ) ;
315+
316+ describe ( "Color Palette Item: click event" , ( ) => {
317+ it ( "should fire item-click event when item is clicked" , ( ) => {
318+ const clickSpy = cy . spy ( ) . as ( "clickSpy" ) ;
319+ const itemClickSpy = cy . spy ( ) . as ( "itemClickSpy" ) ;
320+
321+ cy . mount (
322+ < ColorPalette >
323+ < ColorPaletteItem id = "item1" value = "red" selected > </ ColorPaletteItem >
324+ < ColorPaletteItem id = "item2" value = "blue" > </ ColorPaletteItem >
325+ </ ColorPalette >
326+ ) ;
327+
328+ cy . get < ColorPalette > ( "[ui5-color-palette]" )
329+ . then ( $el => {
330+ $el [ 0 ] . addEventListener ( "item-click" , itemClickSpy ) ;
331+ } ) ;
332+
333+ cy . get ( "#item2" )
334+ . then ( $el => {
335+ $el [ 0 ] . addEventListener ( "click" , clickSpy ) ;
336+ } ) ;
337+
338+ cy . get ( "#item2" )
339+ . realClick ( ) ;
340+
341+ cy . get ( "@clickSpy" )
342+ . should ( "have.been.calledOnce" ) ;
343+
344+ cy . get ( "@itemClickSpy" )
345+ . should ( "have.been.calledOnce" ) ;
346+ } ) ;
347+
348+ it ( "should prevent selection when preventDefault is called" , ( ) => {
349+ cy . mount (
350+ < ColorPalette >
351+ < ColorPaletteItem id = "item1" value = "red" > </ ColorPaletteItem >
352+ < ColorPaletteItem id = "item2" value = "blue" > </ ColorPaletteItem >
353+ </ ColorPalette >
354+ ) ;
355+
356+ cy . get < ColorPalette > ( "[ui5-color-palette]" )
357+ . then ( $el => {
358+ $el [ 0 ] . addEventListener ( "item-click" , cy . spy ( ) . as ( "itemClickSpy" ) ) ;
359+ } ) ;
360+
361+ // First, select item1
362+ cy . get ( "#item1" )
363+ . realClick ( ) ;
364+
365+ cy . get ( "#item1" )
366+ . should ( "have.attr" , "selected" ) ;
367+ cy . get ( "#item2" )
368+ . should ( "not.have.attr" , "selected" ) ;
369+
370+ // Now add preventDefault to item2
371+ cy . get ( "#item2" )
372+ . then ( $el => {
373+ $el [ 0 ] . addEventListener ( "click" , ( e : Event ) => {
374+ e . preventDefault ( ) ;
375+ } ) ;
376+ } ) ;
377+
378+ // Try to click item2 with preventDefault
379+ cy . get ( "#item2" )
380+ . realClick ( ) ;
381+
382+ // Item1 should still be selected because we called preventDefault on item2
383+ cy . get ( "#item1" )
384+ . should ( "have.attr" , "selected" ) ;
385+ cy . get ( "#item2" )
386+ . should ( "not.have.attr" , "selected" ) ;
387+
388+ // The item-click event on ColorPalette should only have been called once (for item1)
389+ cy . get ( "@itemClickSpy" )
390+ . should ( "have.been.calledOnce" ) ;
391+ } ) ;
392+
393+ it ( "should provide correct modifier keys in click event detail" , ( ) => {
394+ cy . mount (
395+ < ColorPalette >
396+ < ColorPaletteItem id = "item1" value = "red" > </ ColorPaletteItem >
397+ < ColorPaletteItem id = "item2" value = "blue" > </ ColorPaletteItem >
398+ </ ColorPalette >
399+ ) ;
400+
401+ cy . get ( "#item2" )
402+ . then ( $el => {
403+ $el [ 0 ] . addEventListener ( "click" , cy . spy ( ( e : CustomEvent ) => {
404+ // Check that event detail contains item and originalEvent
405+ expect ( e . detail ) . to . have . property ( "item" ) ;
406+ expect ( e . detail ) . to . have . property ( "originalEvent" ) ;
407+
408+ // Check item properties
409+ expect ( e . detail . item . value ) . to . equal ( "blue" ) ;
410+
411+ // Check modifier keys from originalEvent
412+ const originalEvent = e . detail . originalEvent ;
413+ expect ( originalEvent . altKey ) . to . be . false ;
414+ expect ( originalEvent . ctrlKey ) . to . be . false ;
415+ expect ( originalEvent . metaKey ) . to . be . false ;
416+ expect ( originalEvent . shiftKey ) . to . be . false ;
417+ } ) . as ( "clickSpy" ) ) ;
418+ } ) ;
419+
420+ cy . get ( "#item2" )
421+ . realClick ( ) ;
422+
423+ cy . get ( "@clickSpy" )
424+ . should ( "have.been.calledOnce" ) ;
425+ } ) ;
426+
427+ it ( "should provide correct modifier keys when Ctrl is pressed" , ( ) => {
428+ let eventDetail : any ;
429+
430+ cy . mount (
431+ < ColorPalette >
432+ < ColorPaletteItem id = "item1" value = "red" > </ ColorPaletteItem >
433+ < ColorPaletteItem id = "item2" value = "blue" > </ ColorPaletteItem >
434+ </ ColorPalette >
435+ ) ;
436+
437+ cy . get ( "#item2" )
438+ . then ( $el => {
439+ $el [ 0 ] . addEventListener ( "click" , ( e : Event ) => {
440+ eventDetail = ( e as CustomEvent ) . detail ;
441+ } ) ;
442+ } ) ;
443+
444+ // Manually dispatch a MouseEvent with ctrlKey
445+ cy . get ( "#item2" )
446+ . shadow ( )
447+ . find ( ".ui5-cp-item" )
448+ . then ( $item => {
449+ const mouseEvent = new MouseEvent ( 'click' , {
450+ bubbles : true ,
451+ cancelable : true ,
452+ ctrlKey : true ,
453+ altKey : false ,
454+ metaKey : false ,
455+ shiftKey : false
456+ } ) ;
457+ $item [ 0 ] . dispatchEvent ( mouseEvent ) ;
458+ } ) ;
459+
460+ cy . then ( ( ) => eventDetail )
461+ . then ( ( detail ) => {
462+ expect ( detail , "event detail should exist" ) . to . exist ;
463+ expect ( detail . item . value , "item value should be blue" ) . to . equal ( "blue" ) ;
464+ const originalEvent = detail . originalEvent ;
465+ expect ( originalEvent . ctrlKey , "ctrlKey should be true" ) . to . be . true ;
466+ expect ( originalEvent . altKey , "altKey should be false" ) . to . be . false ;
467+ expect ( originalEvent . metaKey , "metaKey should be false" ) . to . be . false ;
468+ expect ( originalEvent . shiftKey , "shiftKey should be false" ) . to . be . false ;
469+ } ) ;
470+ } ) ;
471+
472+ it ( "should provide correct modifier keys when Alt is pressed" , ( ) => {
473+ cy . mount (
474+ < ColorPalette >
475+ < ColorPaletteItem id = "item1" value = "red" > </ ColorPaletteItem >
476+ < ColorPaletteItem id = "item2" value = "blue" > </ ColorPaletteItem >
477+ </ ColorPalette >
478+ ) ;
479+
480+ cy . get ( "#item2" )
481+ . then ( $el => {
482+ $el [ 0 ] . addEventListener ( "click" , cy . spy ( ( e : CustomEvent ) => {
483+ const originalEvent = e . detail . originalEvent ;
484+ expect ( originalEvent . altKey ) . to . be . true ;
485+ expect ( originalEvent . ctrlKey ) . to . be . false ;
486+ expect ( originalEvent . metaKey ) . to . be . false ;
487+ expect ( originalEvent . shiftKey ) . to . be . false ;
488+ } ) . as ( "clickSpyAlt" ) ) ;
489+ } ) ;
490+
491+ cy . get ( "#item2" )
492+ . realClick ( { altKey : true } ) ;
493+
494+ cy . get ( "@clickSpyAlt" )
495+ . should ( "have.been.calledOnce" ) ;
496+ } ) ;
497+
498+ it ( "should provide correct modifier keys when Shift is pressed" , ( ) => {
499+ cy . mount (
500+ < ColorPalette >
501+ < ColorPaletteItem id = "item1" value = "red" > </ ColorPaletteItem >
502+ < ColorPaletteItem id = "item2" value = "blue" > </ ColorPaletteItem >
503+ </ ColorPalette >
504+ ) ;
505+
506+ cy . get ( "#item2" )
507+ . then ( $el => {
508+ $el [ 0 ] . addEventListener ( "click" , cy . spy ( ( e : CustomEvent ) => {
509+ const originalEvent = e . detail . originalEvent ;
510+ expect ( originalEvent . shiftKey ) . to . be . true ;
511+ expect ( originalEvent . altKey ) . to . be . false ;
512+ expect ( originalEvent . ctrlKey ) . to . be . false ;
513+ expect ( originalEvent . metaKey ) . to . be . false ;
514+ } ) . as ( "clickSpyShift" ) ) ;
515+ } ) ;
516+
517+ cy . get ( "#item2" )
518+ . realClick ( { shiftKey : true } ) ;
519+
520+ cy . get ( "@clickSpyShift" )
521+ . should ( "have.been.calledOnce" ) ;
522+ } ) ;
523+
524+ it ( "should provide correct modifier keys when multiple modifiers are pressed" , ( ) => {
525+ let eventDetail : any ;
526+
527+ cy . mount (
528+ < ColorPalette >
529+ < ColorPaletteItem id = "item1" value = "red" > </ ColorPaletteItem >
530+ < ColorPaletteItem id = "item2" value = "blue" > </ ColorPaletteItem >
531+ </ ColorPalette >
532+ ) ;
533+
534+ cy . get ( "#item2" )
535+ . then ( $el => {
536+ $el [ 0 ] . addEventListener ( "click" , ( e : Event ) => {
537+ eventDetail = ( e as CustomEvent ) . detail ;
538+ } ) ;
539+ } ) ;
540+
541+ // Manually dispatch a MouseEvent with multiple modifier keys
542+ cy . get ( "#item2" )
543+ . shadow ( )
544+ . find ( ".ui5-cp-item" )
545+ . then ( $item => {
546+ const mouseEvent = new MouseEvent ( 'click' , {
547+ bubbles : true ,
548+ cancelable : true ,
549+ ctrlKey : true ,
550+ altKey : false ,
551+ metaKey : false ,
552+ shiftKey : true
553+ } ) ;
554+ $item [ 0 ] . dispatchEvent ( mouseEvent ) ;
555+ } ) ;
556+
557+ cy . then ( ( ) => eventDetail )
558+ . then ( ( detail ) => {
559+ expect ( detail , "event detail should exist" ) . to . exist ;
560+ expect ( detail . item . value , "item value should be blue" ) . to . equal ( "blue" ) ;
561+ const originalEvent = detail . originalEvent ;
562+ expect ( originalEvent . ctrlKey , "ctrlKey should be true" ) . to . be . true ;
563+ expect ( originalEvent . shiftKey , "shiftKey should be true" ) . to . be . true ;
564+ expect ( originalEvent . altKey , "altKey should be false" ) . to . be . false ;
565+ expect ( originalEvent . metaKey , "metaKey should be false" ) . to . be . false ;
566+ } ) ;
567+ } ) ;
568+ } ) ;
0 commit comments