juice-shop
107 строк · 3.3 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import { ChatbotService } from '../Services/chatbot.service'
7import { ReactiveFormsModule } from '@angular/forms'
8import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
9import { MatCardModule } from '@angular/material/card'
10import { MatFormFieldModule } from '@angular/material/form-field'
11import { TranslateModule, TranslateService } from '@ngx-translate/core'
12import { MatInputModule } from '@angular/material/input'
13import { MatButtonModule } from '@angular/material/button'
14
15import { type ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'
16import { ChatbotComponent } from './chatbot.component'
17import { of } from 'rxjs'
18
19import { HttpClientTestingModule } from '@angular/common/http/testing'
20import { EventEmitter } from '@angular/core'
21import { CookieModule } from 'ngx-cookie'
22
23enum MessageSources {
24user = 'user',
25bot = 'bot'
26}
27
28describe('ComplaintComponent', () => {
29let component: ChatbotComponent
30let fixture: ComponentFixture<ChatbotComponent>
31let chatbotService: any
32let translateService
33
34beforeEach(waitForAsync(() => {
35chatbotService = jasmine.createSpyObj('ChatbotService', ['getChatbotStatus', 'getResponse'])
36chatbotService.getChatbotStatus.and.returnValue(of({
37status: true,
38body: 'hello there'
39}))
40chatbotService.getResponse.and.returnValue(of({
41action: 'response',
42body: 'hello there'
43}))
44translateService = jasmine.createSpyObj('TranslateService', ['get'])
45translateService.get.and.returnValue(of({}))
46translateService.onLangChange = new EventEmitter()
47translateService.onTranslationChange = new EventEmitter()
48translateService.onDefaultLangChange = new EventEmitter()
49
50TestBed.configureTestingModule({
51imports: [
52HttpClientTestingModule,
53ReactiveFormsModule,
54CookieModule.forRoot(),
55TranslateModule.forRoot(),
56BrowserAnimationsModule,
57MatCardModule,
58MatFormFieldModule,
59MatInputModule,
60MatButtonModule
61],
62declarations: [ChatbotComponent],
63providers: [
64{ provide: ChatbotService, useValue: chatbotService },
65{ provide: TranslateService, useValue: translateService }
66]
67})
68.compileComponents()
69}))
70
71beforeEach(() => {
72fixture = TestBed.createComponent(ChatbotComponent)
73component = fixture.componentInstance
74fixture.detectChanges()
75})
76
77it('should create', () => {
78expect(component).toBeTruthy()
79})
80
81it('should initially have 1 message intially', () => {
82expect(component.messages.length).toEqual(1)
83expect(component.messages[0]).toEqual({
84author: MessageSources.bot,
85body: 'hello there'
86})
87})
88
89it('should record and display user messages', () => {
90component.messageControl.setValue('Message')
91component.sendMessage()
92expect(component.messages[1]).toEqual({
93author: MessageSources.user,
94body: 'Message'
95})
96})
97
98it('Responds to user messages', () => {
99component.messageControl.setValue('Message')
100component.sendMessage()
101expect(component.messages.length).toEqual(3)
102expect(component.messages[2]).toEqual({
103author: MessageSources.bot,
104body: 'hello there'
105})
106})
107})
108