11import { aTimeout , elementUpdated , expect , fixture } from '@open-wc/testing' ;
22import { html } from 'lit' ;
3- import { type SinonFakeTimers , useFakeTimers } from 'sinon' ;
3+ import { type SinonFakeTimers , spy , useFakeTimers } from 'sinon' ;
44import { defineComponents } from '../common/definitions/defineComponents.js' ;
55import { simulateClick , simulateFocus } from '../common/utils.spec.js' ;
66import IgcChatComponent from './chat.js' ;
@@ -129,6 +129,10 @@ describe('Chat', () => {
129129 </div>
130130 <igc-chat-message-list>
131131 </igc-chat-message-list>
132+ <div class="suggestions-container">
133+ <slot name="suggestions" part="suggestions">
134+ </slot>
135+ </div>
132136 <igc-chat-input>
133137 </igc-chat-input>
134138 </div>`
@@ -450,6 +454,38 @@ describe('Chat', () => {
450454 }
451455 } ) ;
452456 } ) ;
457+
458+ it ( 'should render suggestions' , async ( ) => {
459+ chat . options = {
460+ suggestions : [ 'Suggestion 1' , 'Suggestion 2' ] ,
461+ } ;
462+ await elementUpdated ( chat ) ;
463+
464+ const suggestionsContainer = chat . shadowRoot ?. querySelector (
465+ '.suggestions-container'
466+ ) ;
467+
468+ expect ( suggestionsContainer ) . dom . to . equal (
469+ `<div class="suggestions-container">
470+ <slot name="suggestions" part="suggestions">
471+ <slot name="suggestion" part="suggestion">
472+ <igc-chip>
473+ <span>
474+ Suggestion 1
475+ </span>
476+ </igc-chip>
477+ </slot>
478+ <slot name="suggestion" part="suggestion">
479+ <igc-chip>
480+ <span>
481+ Suggestion 2
482+ </span>
483+ </igc-chip>
484+ </slot>
485+ </slot>
486+ </div>`
487+ ) ;
488+ } ) ;
453489 } ) ;
454490
455491 describe ( 'Slots' , ( ) => {
@@ -470,6 +506,7 @@ describe('Chat', () => {
470506 it ( 'should slot header prefix' , ( ) => { } ) ;
471507 it ( 'should slot header title' , ( ) => { } ) ;
472508 it ( 'should slot header action buttons area' , ( ) => { } ) ;
509+ it ( 'should slot suggetions area' , ( ) => { } ) ;
473510 } ) ;
474511
475512 describe ( 'Templates' , ( ) => {
@@ -597,6 +634,7 @@ describe('Chat', () => {
597634 describe ( 'Interactions' , ( ) => {
598635 describe ( 'Click' , ( ) => {
599636 it ( 'should update messages properly on send button click' , async ( ) => {
637+ const eventSpy = spy ( chat , 'emitEvent' ) ;
600638 const inputArea = chat . shadowRoot ?. querySelector ( 'igc-chat-input' ) ;
601639 const sendButton = inputArea ?. shadowRoot ?. querySelector (
602640 'igc-icon-button[name="send-message"]'
@@ -610,11 +648,49 @@ describe('Chat', () => {
610648 simulateClick ( sendButton ) ;
611649 await elementUpdated ( chat ) ;
612650 await clock . tickAsync ( 500 ) ;
651+
652+ expect ( eventSpy ) . calledWith ( 'igcMessageCreated' ) ;
653+ const eventArgs = eventSpy . getCall ( 0 ) . args [ 1 ] ?. detail ;
654+ const args =
655+ eventArgs && typeof eventArgs === 'object'
656+ ? { ...eventArgs , text : 'Hello!' , sender : 'user' }
657+ : { text : 'Hello!' , sender : 'user' } ;
658+ expect ( eventArgs ) . to . deep . equal ( args ) ;
613659 expect ( chat . messages . length ) . to . equal ( 1 ) ;
614660 expect ( chat . messages [ 0 ] . text ) . to . equal ( 'Hello!' ) ;
615661 expect ( chat . messages [ 0 ] . sender ) . to . equal ( 'user' ) ;
616662 }
617663 } ) ;
664+
665+ it ( 'should update messages properly on suggestion chip click' , async ( ) => {
666+ const eventSpy = spy ( chat , 'emitEvent' ) ;
667+ chat . options = {
668+ suggestions : [ 'Suggestion 1' , 'Suggestion 2' ] ,
669+ } ;
670+ await elementUpdated ( chat ) ;
671+
672+ const suggestionChips = chat . shadowRoot
673+ ?. querySelector ( '.suggestions-container' )
674+ ?. querySelectorAll ( 'igc-chip' ) ;
675+
676+ expect ( suggestionChips ?. length ) . to . equal ( 2 ) ;
677+ if ( suggestionChips ) {
678+ simulateClick ( suggestionChips [ 0 ] ) ;
679+ await elementUpdated ( chat ) ;
680+
681+ expect ( eventSpy ) . calledWith ( 'igcMessageCreated' ) ;
682+ const eventArgs = eventSpy . getCall ( 0 ) . args [ 1 ] ?. detail ;
683+ const args =
684+ eventArgs && typeof eventArgs === 'object'
685+ ? { ...eventArgs , text : 'Suggestion 1' , sender : 'user' }
686+ : { text : 'Suggestion 1' , sender : 'user' } ;
687+ expect ( eventArgs ) . to . deep . equal ( args ) ;
688+ expect ( chat . messages . length ) . to . equal ( 1 ) ;
689+ expect ( chat . messages [ 0 ] . text ) . to . equal ( 'Suggestion 1' ) ;
690+ expect ( chat . messages [ 0 ] . sender ) . to . equal ( 'user' ) ;
691+ }
692+ } ) ;
693+
618694 it ( 'should remove attachement on chip remove button click' , ( ) => { } ) ;
619695 } ) ;
620696
@@ -624,6 +700,7 @@ describe('Chat', () => {
624700
625701 describe ( 'Keyboard' , ( ) => {
626702 it ( 'should update messages properly on `Enter` keypress when the textarea is focused' , async ( ) => {
703+ const eventSpy = spy ( chat , 'emitEvent' ) ;
627704 const inputArea = chat . shadowRoot ?. querySelector ( 'igc-chat-input' ) ;
628705 const sendButton = inputArea ?. shadowRoot ?. querySelector (
629706 'igc-icon-button[name="send-message"]'
@@ -644,6 +721,14 @@ describe('Chat', () => {
644721 ) ;
645722 await elementUpdated ( chat ) ;
646723 await clock . tickAsync ( 500 ) ;
724+
725+ expect ( eventSpy ) . calledWith ( 'igcMessageCreated' ) ;
726+ const eventArgs = eventSpy . getCall ( 0 ) . args [ 1 ] ?. detail ;
727+ const args =
728+ eventArgs && typeof eventArgs === 'object'
729+ ? { ...eventArgs , text : 'Hello!' , sender : 'user' }
730+ : { text : 'Hello!' , sender : 'user' } ;
731+ expect ( eventArgs ) . to . deep . equal ( args ) ;
647732 expect ( chat . messages . length ) . to . equal ( 1 ) ;
648733 expect ( chat . messages [ 0 ] . text ) . to . equal ( 'Hello!' ) ;
649734 expect ( chat . messages [ 0 ] . sender ) . to . equal ( 'user' ) ;
@@ -653,7 +738,6 @@ describe('Chat', () => {
653738 } ) ;
654739
655740 describe ( 'Events' , ( ) => {
656- it ( 'emits igcMessageCreated' , async ( ) => { } ) ;
657741 it ( 'emits igcAttachmentClick' , async ( ) => { } ) ;
658742 it ( 'emits igcAttachmentChange' , async ( ) => { } ) ;
659743 it ( 'emits igcTypingChange' , async ( ) => { } ) ;
0 commit comments