juice-shop

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

6
import { TranslateModule } from '@ngx-translate/core'
7
import { MatDividerModule } from '@angular/material/divider'
8
import { HttpClientTestingModule } from '@angular/common/http/testing'
9
import { type ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing'
10
import { AccountingComponent } from './accounting.component'
11
import { ProductService } from '../Services/product.service'
12
import { RouterTestingModule } from '@angular/router/testing'
13
import { MatGridListModule } from '@angular/material/grid-list'
14
import { MatCardModule } from '@angular/material/card'
15
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
16
import { MatTableModule } from '@angular/material/table'
17
import { MatPaginatorModule } from '@angular/material/paginator'
18
import { of } from 'rxjs'
19
import { QuantityService } from '../Services/quantity.service'
20
import { MatFormFieldModule } from '@angular/material/form-field'
21
import { throwError } from 'rxjs/internal/observable/throwError'
22
import { OrderHistoryService } from '../Services/order-history.service'
23
import { MatIconModule } from '@angular/material/icon'
24
import { MatTooltipModule } from '@angular/material/tooltip'
25
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar'
26

27
describe('AccountingComponent', () => {
28
  let component: AccountingComponent
29
  let fixture: ComponentFixture<AccountingComponent>
30
  let productService
31
  let quantityService
32
  let orderHistoryService
33
  let snackBar: any
34

35
  beforeEach(waitForAsync(() => {
36
    quantityService = jasmine.createSpyObj('QuantityService', ['getAll', 'put'])
37
    quantityService.getAll.and.returnValue(of([]))
38
    quantityService.put.and.returnValue(of({}))
39
    productService = jasmine.createSpyObj('ProductService', ['search', 'get', 'put'])
40
    productService.search.and.returnValue(of([]))
41
    productService.get.and.returnValue(of({}))
42
    productService.put.and.returnValue(of({}))
43
    orderHistoryService = jasmine.createSpyObj('OrderHistoryService', ['getAll', 'toggleDeliveryStatus'])
44
    orderHistoryService.getAll.and.returnValue(of([]))
45
    orderHistoryService.toggleDeliveryStatus.and.returnValue(of({}))
46
    snackBar = jasmine.createSpyObj('MatSnackBar', ['open'])
47
    snackBar.open.and.returnValue(null)
48

49
    TestBed.configureTestingModule({
50
      declarations: [AccountingComponent],
51
      imports: [
52
        RouterTestingModule,
53
        HttpClientTestingModule,
54
        TranslateModule.forRoot(),
55
        BrowserAnimationsModule,
56
        MatTableModule,
57
        MatPaginatorModule,
58
        MatFormFieldModule,
59
        MatDividerModule,
60
        MatGridListModule,
61
        MatCardModule,
62
        MatIconModule,
63
        MatTooltipModule,
64
        MatSnackBarModule
65
      ],
66
      providers: [
67
        { provide: ProductService, useValue: productService },
68
        { provide: QuantityService, useValue: quantityService },
69
        { provide: OrderHistoryService, useValue: orderHistoryService },
70
        { provide: MatSnackBar, useValue: snackBar }
71
      ]
72
    })
73
      .compileComponents()
74
  }))
75

76
  beforeEach(() => {
77
    fixture = TestBed.createComponent(AccountingComponent)
78
    component = fixture.componentInstance
79
    component.ngAfterViewInit()
80
    fixture.detectChanges()
81
  })
82

83
  it('should create', () => {
84
    expect(component).toBeTruthy()
85
  })
86

87
  it('should load products, quantitites and orders when initiated', () => {
88
    quantityService.getAll.and.returnValue(of([]))
89
    productService.search.and.returnValue(of([]))
90
    orderHistoryService.getAll.and.returnValue(of([]))
91
    component.ngAfterViewInit()
92
    expect(quantityService.getAll).toHaveBeenCalled()
93
    expect(productService.search).toHaveBeenCalled()
94
    expect(orderHistoryService.getAll).toHaveBeenCalled()
95
  })
96

97
  it('should hold no products when product search API call fails', () => {
98
    productService.search.and.returnValue(throwError('Error'))
99
    component.loadProducts()
100
    fixture.detectChanges()
101
    expect(component.tableData).toEqual([])
102
  })
103

104
  it('should hold no orders when getAll orders API call fails', () => {
105
    orderHistoryService.getAll.and.returnValue(throwError('Error'))
106
    component.loadOrders()
107
    fixture.detectChanges()
108
    expect(component.orderData).toEqual([])
109
  })
110

111
  it('should hold no quantities when getAll quanitity API call fails', () => {
112
    quantityService.getAll.and.returnValue(throwError('Error'))
113
    component.loadQuantity()
114
    fixture.detectChanges()
115
    expect(component.quantityMap).toEqual({})
116
  })
117

118
  it('should log error from product search API call directly to browser console', fakeAsync(() => {
119
    productService.search.and.returnValue(throwError('Error'))
120
    console.log = jasmine.createSpy('log')
121
    component.loadProducts()
122
    expect(console.log).toHaveBeenCalledWith('Error')
123
  }))
124

125
  it('should log error from getAll orders API call directly to browser console', fakeAsync(() => {
126
    orderHistoryService.getAll.and.returnValue(throwError('Error'))
127
    console.log = jasmine.createSpy('log')
128
    component.loadOrders()
129
    expect(console.log).toHaveBeenCalledWith('Error')
130
  }))
131

132
  it('should load orders when toggleDeliveryStatus gets called', () => {
133
    orderHistoryService.getAll.and.returnValue(throwError('Error'))
134
    orderHistoryService.toggleDeliveryStatus.and.returnValue(of({}))
135
    component.changeDeliveryStatus(true, 1)
136
    expect(orderHistoryService.getAll).toHaveBeenCalled()
137
  })
138

139
  it('should log error from toggleDeliveryStatus API call directly to browser console', fakeAsync(() => {
140
    orderHistoryService.toggleDeliveryStatus.and.returnValue(throwError('Error'))
141
    console.log = jasmine.createSpy('log')
142
    component.changeDeliveryStatus(true, 1)
143
    expect(snackBar.open).toHaveBeenCalled()
144
    expect(console.log).toHaveBeenCalledWith('Error')
145
  }))
146

147
  it('should log error from getAll quantity API call directly to browser console', fakeAsync(() => {
148
    quantityService.getAll.and.returnValue(throwError('Error'))
149
    console.log = jasmine.createSpy('log')
150
    component.loadQuantity()
151
    expect(console.log).toHaveBeenCalledWith('Error')
152
  }))
153

154
  it('should log and display errors while modifying price', fakeAsync(() => {
155
    productService.put.and.returnValue(throwError({ error: 'Error' }))
156
    console.log = jasmine.createSpy('log')
157
    component.modifyPrice(1, 100)
158
    fixture.detectChanges()
159
    expect(snackBar.open).toHaveBeenCalled()
160
    expect(console.log).toHaveBeenCalledWith({ error: 'Error' })
161
  }))
162

163
  it('should log and display errors while modifying quantity', fakeAsync(() => {
164
    quantityService.put.and.returnValue(throwError({ error: 'Error' }))
165
    console.log = jasmine.createSpy('log')
166
    component.modifyQuantity(1, 100)
167
    fixture.detectChanges()
168
    expect(snackBar.open).toHaveBeenCalled()
169
    expect(console.log).toHaveBeenCalledWith({ error: 'Error' })
170
  }))
171

172
  it('should show confirmation on modifying quantity of a product', fakeAsync(() => {
173
    quantityService.put.and.returnValue(of({ ProductId: 1 }))
174
    component.tableData = [{ id: 1, name: 'Apple Juice' }]
175
    component.modifyQuantity(1, 100)
176
    fixture.detectChanges()
177
    expect(snackBar.open).toHaveBeenCalled()
178
  }))
179

180
  it('should show confirmation on modifying price of a product', fakeAsync(() => {
181
    productService.put.and.returnValue(of({ name: 'Apple Juice' }))
182
    component.modifyPrice(1, 100)
183
    fixture.detectChanges()
184
    expect(snackBar.open).toHaveBeenCalled()
185
  }))
186

187
  it('should modify quantity of a product', () => {
188
    quantityService.put.and.returnValue(of({ ProductId: 1 }))
189
    component.tableData = [{ id: 1, name: 'Apple Juice' }]
190
    quantityService.getAll.and.returnValue(of([]))
191
    component.modifyQuantity(1, 100)
192
    expect(quantityService.put).toHaveBeenCalled()
193
    expect(quantityService.getAll).toHaveBeenCalled()
194
  })
195

196
  it('should modify price of a product', () => {
197
    productService.search.and.returnValue(of([]))
198
    productService.put.and.returnValue(of({ name: 'Apple Juice' }))
199
    component.modifyPrice(1, 100)
200
    expect(productService.put).toHaveBeenCalled()
201
    expect(productService.search).toHaveBeenCalled()
202
  })
203
})
204

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

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

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

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