|
| 1 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing' |
| 2 | +import { IgxChatComponent } from './chat.component' |
| 3 | +import { Component, signal, TemplateRef, viewChild } from '@angular/core'; |
| 4 | +import type { IgcChatMessage } from 'igniteui-webcomponents'; |
| 5 | + |
| 6 | +describe('Chat wrapper', () => { |
| 7 | + function getShadowRoot(element: HTMLElement) { |
| 8 | + return element.shadowRoot; |
| 9 | + } |
| 10 | + |
| 11 | + let chatComponent: IgxChatComponent; |
| 12 | + let chatElement: HTMLElement; |
| 13 | + let fixture: ComponentFixture<IgxChatComponent>; |
| 14 | + |
| 15 | + beforeEach(waitForAsync(() => { |
| 16 | + TestBed.configureTestingModule({ |
| 17 | + imports: [IgxChatComponent] |
| 18 | + }).compileComponents(); |
| 19 | + })); |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + fixture = TestBed.createComponent(IgxChatComponent); |
| 23 | + chatComponent = fixture.componentInstance; |
| 24 | + chatElement = (fixture.nativeElement as HTMLElement).querySelector('igc-chat'); |
| 25 | + fixture.detectChanges(); |
| 26 | + }) |
| 27 | + |
| 28 | + it('is created', () => { |
| 29 | + expect(chatComponent).toBeDefined(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('has correct initial empty state', () => { |
| 33 | + const draft = chatComponent.draftMessage(); |
| 34 | + |
| 35 | + expect(chatComponent.messages().length).toEqual(0); |
| 36 | + expect(draft.text).toEqual(''); |
| 37 | + expect(draft.attachments).toBeUndefined(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('correct bindings for messages', async () => { |
| 41 | + fixture.componentRef.setInput('messages', [{ id: '1', sender: 'user', text: 'Hello' }]); |
| 42 | + |
| 43 | + fixture.detectChanges(); |
| 44 | + await fixture.whenStable(); |
| 45 | + |
| 46 | + const messageElement = getShadowRoot(chatElement).querySelector<HTMLElement>('igc-chat-message'); |
| 47 | + expect(messageElement).toBeDefined(); |
| 48 | + expect(getShadowRoot(messageElement).textContent.trim()).toEqual(chatComponent.messages()[0].text); |
| 49 | + }); |
| 50 | + |
| 51 | + it('correct bindings for draft message', async () => { |
| 52 | + fixture.componentRef.setInput('draftMessage', { text: 'Hello world' }); |
| 53 | + |
| 54 | + fixture.detectChanges(); |
| 55 | + await fixture.whenStable(); |
| 56 | + |
| 57 | + const textarea = getShadowRoot(getShadowRoot(chatElement).querySelector('igc-chat-input')).querySelector('igc-textarea'); |
| 58 | + expect(textarea.value).toEqual(chatComponent.draftMessage().text); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe('Chat templates', () => { |
| 63 | + function getShadowRoot(element: HTMLElement) { |
| 64 | + return element.shadowRoot; |
| 65 | + } |
| 66 | + |
| 67 | + let fixture: ComponentFixture<ChatTemplatesBed>; |
| 68 | + let chatElement: HTMLElement; |
| 69 | + |
| 70 | + beforeEach(waitForAsync(() => { |
| 71 | + TestBed.configureTestingModule({ |
| 72 | + imports: [IgxChatComponent, ChatTemplatesBed] |
| 73 | + }).compileComponents(); |
| 74 | + })); |
| 75 | + |
| 76 | + beforeEach(() => { |
| 77 | + fixture = TestBed.createComponent(ChatTemplatesBed); |
| 78 | + fixture.detectChanges(); |
| 79 | + chatElement = (fixture.nativeElement as HTMLElement).querySelector('igc-chat'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('has correct initially bound template', async () => { |
| 83 | + await fixture.whenStable(); |
| 84 | + |
| 85 | + // NOTE: This is invoked since in the test bed there is no app ref so fresh embedded view |
| 86 | + // has no change detection ran on it. In an application scenario this is not the case. |
| 87 | + // This is so we don't explicitly invoke `viewRef.detectChanges()` inside the returned closure |
| 88 | + // from the wrapper's `_createTemplateRenderer` call. |
| 89 | + fixture.detectChanges(); |
| 90 | + expect(getShadowRoot(getShadowRoot(chatElement).querySelector('igc-chat-message')).textContent.trim()) |
| 91 | + .toEqual(`Your message: ${fixture.componentInstance.messages()[0].text}`); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | + |
| 96 | +@Component({ |
| 97 | + template: ` |
| 98 | + <igx-chat [messages]="messages()" [templates]="{messageContent: messageTemplate()}"/> |
| 99 | + <ng-template #message let-message> |
| 100 | + <h3>Your message: {{ message.text }}</h3> |
| 101 | + </ng-template> |
| 102 | + `, |
| 103 | + imports: [IgxChatComponent] |
| 104 | +}) |
| 105 | +class ChatTemplatesBed { |
| 106 | + public messages = signal<IgcChatMessage[]>([{ |
| 107 | + id: '1', |
| 108 | + sender: 'user', |
| 109 | + text: 'Hello world' |
| 110 | + }]); |
| 111 | + public messageTemplate = viewChild.required<TemplateRef<any>>('message'); |
| 112 | +} |
0 commit comments