juice-shop

Форк
0
/
chatbot.component.spec.ts 
107 строк · 3.3 Кб
1
/*
2
 * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import { ChatbotService } from '../Services/chatbot.service'
7
import { ReactiveFormsModule } from '@angular/forms'
8
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
9
import { MatCardModule } from '@angular/material/card'
10
import { MatFormFieldModule } from '@angular/material/form-field'
11
import { TranslateModule, TranslateService } from '@ngx-translate/core'
12
import { MatInputModule } from '@angular/material/input'
13
import { MatButtonModule } from '@angular/material/button'
14

15
import { type ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'
16
import { ChatbotComponent } from './chatbot.component'
17
import { of } from 'rxjs'
18

19
import { HttpClientTestingModule } from '@angular/common/http/testing'
20
import { EventEmitter } from '@angular/core'
21
import { CookieModule } from 'ngx-cookie'
22

23
enum MessageSources {
24
  user = 'user',
25
  bot = 'bot'
26
}
27

28
describe('ComplaintComponent', () => {
29
  let component: ChatbotComponent
30
  let fixture: ComponentFixture<ChatbotComponent>
31
  let chatbotService: any
32
  let translateService
33

34
  beforeEach(waitForAsync(() => {
35
    chatbotService = jasmine.createSpyObj('ChatbotService', ['getChatbotStatus', 'getResponse'])
36
    chatbotService.getChatbotStatus.and.returnValue(of({
37
      status: true,
38
      body: 'hello there'
39
    }))
40
    chatbotService.getResponse.and.returnValue(of({
41
      action: 'response',
42
      body: 'hello there'
43
    }))
44
    translateService = jasmine.createSpyObj('TranslateService', ['get'])
45
    translateService.get.and.returnValue(of({}))
46
    translateService.onLangChange = new EventEmitter()
47
    translateService.onTranslationChange = new EventEmitter()
48
    translateService.onDefaultLangChange = new EventEmitter()
49

50
    TestBed.configureTestingModule({
51
      imports: [
52
        HttpClientTestingModule,
53
        ReactiveFormsModule,
54
        CookieModule.forRoot(),
55
        TranslateModule.forRoot(),
56
        BrowserAnimationsModule,
57
        MatCardModule,
58
        MatFormFieldModule,
59
        MatInputModule,
60
        MatButtonModule
61
      ],
62
      declarations: [ChatbotComponent],
63
      providers: [
64
        { provide: ChatbotService, useValue: chatbotService },
65
        { provide: TranslateService, useValue: translateService }
66
      ]
67
    })
68
      .compileComponents()
69
  }))
70

71
  beforeEach(() => {
72
    fixture = TestBed.createComponent(ChatbotComponent)
73
    component = fixture.componentInstance
74
    fixture.detectChanges()
75
  })
76

77
  it('should create', () => {
78
    expect(component).toBeTruthy()
79
  })
80

81
  it('should initially have 1 message intially', () => {
82
    expect(component.messages.length).toEqual(1)
83
    expect(component.messages[0]).toEqual({
84
      author: MessageSources.bot,
85
      body: 'hello there'
86
    })
87
  })
88

89
  it('should record and display user messages', () => {
90
    component.messageControl.setValue('Message')
91
    component.sendMessage()
92
    expect(component.messages[1]).toEqual({
93
      author: MessageSources.user,
94
      body: 'Message'
95
    })
96
  })
97

98
  it('Responds to user messages', () => {
99
    component.messageControl.setValue('Message')
100
    component.sendMessage()
101
    expect(component.messages.length).toEqual(3)
102
    expect(component.messages[2]).toEqual({
103
      author: MessageSources.bot,
104
      body: 'hello there'
105
    })
106
  })
107
})
108

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.