juice-shop

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

6
import { TranslateModule, TranslateService } from '@ngx-translate/core'
7
import { MatInputModule } from '@angular/material/input'
8
import { BasketService } from '../Services/basket.service'
9
import { type ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing'
10
import { MatCardModule } from '@angular/material/card'
11
import { MatTableModule } from '@angular/material/table'
12
import { MatButtonModule } from '@angular/material/button'
13
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
14
import { HttpClientTestingModule } from '@angular/common/http/testing'
15
import { ReactiveFormsModule } from '@angular/forms'
16
import { of } from 'rxjs'
17
import { throwError } from 'rxjs/internal/observable/throwError'
18
import { MatButtonToggleModule } from '@angular/material/button-toggle'
19
import { PurchaseBasketComponent } from '../purchase-basket/purchase-basket.component'
20
import { UserService } from '../Services/user.service'
21
import { DeluxeGuard } from '../app.guard'
22
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar'
23
import { EventEmitter } from '@angular/core'
24

25
describe('PurchaseBasketComponent', () => {
26
  let component: PurchaseBasketComponent
27
  let fixture: ComponentFixture<PurchaseBasketComponent>
28
  let basketService
29
  let userService
30
  let translateService: any
31
  let deluxeGuard
32
  let snackBar: any
33

34
  beforeEach(waitForAsync(() => {
35
    basketService = jasmine.createSpyObj('BasketService', ['find', 'del', 'get', 'put', 'updateNumberOfCartItems'])
36
    basketService.find.and.returnValue(of({ Products: [] }))
37
    basketService.del.and.returnValue(of({}))
38
    basketService.get.and.returnValue(of({}))
39
    basketService.put.and.returnValue(of({}))
40
    basketService.updateNumberOfCartItems.and.returnValue(of({}))
41
    userService = jasmine.createSpyObj('UserService', ['whoAmI'])
42
    userService.whoAmI.and.returnValue(of({}))
43
    translateService = jasmine.createSpyObj('TranslateService', ['get'])
44
    translateService.get.and.returnValue(of({}))
45
    translateService.onLangChange = new EventEmitter()
46
    translateService.onTranslationChange = new EventEmitter()
47
    translateService.onDefaultLangChange = new EventEmitter()
48
    deluxeGuard = jasmine.createSpyObj('', ['isDeluxe'])
49
    deluxeGuard.isDeluxe.and.returnValue(false)
50
    snackBar = jasmine.createSpyObj('MatSnackBar', ['open'])
51

52
    TestBed.configureTestingModule({
53
      declarations: [PurchaseBasketComponent],
54
      imports: [
55
        HttpClientTestingModule,
56
        TranslateModule.forRoot(),
57
        BrowserAnimationsModule,
58
        ReactiveFormsModule,
59
        MatInputModule,
60
        MatCardModule,
61
        MatTableModule,
62
        MatButtonModule,
63
        MatButtonToggleModule,
64
        MatSnackBarModule
65
      ],
66
      providers: [
67
        { provide: TranslateService, useValue: translateService },
68
        { provide: BasketService, useValue: basketService },
69
        { provide: MatSnackBar, useValue: snackBar },
70
        { provide: UserService, useValue: userService },
71
        { provide: DeluxeGuard, useValue: deluxeGuard }
72
      ]
73
    })
74
      .compileComponents()
75
  }))
76

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

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

87
  it('should load user email when being created', () => {
88
    userService.whoAmI.and.returnValue(of({ email: 'a@a' }))
89
    component.ngOnInit()
90
    expect(component.userEmail).toBe('(a@a)')
91
  })
92

93
  it('should log an error if userService fails to fetch the user', fakeAsync(() => {
94
    userService.whoAmI.and.returnValue(throwError('Error'))
95
    console.log = jasmine.createSpy('log')
96
    component.ngOnInit()
97
    expect(console.log).toHaveBeenCalledWith('Error')
98
  }))
99

100
  it('should hold products returned by backend API', () => {
101
    basketService.find.and.returnValue(of({ Products: [{ name: 'Product1', price: 1, deluxePrice: 1, BasketItem: { quantity: 1 } }, { name: 'Product2', price: 2, deluxePrice: 2, BasketItem: { quantity: 2 } }] }))
102
    component.load()
103
    expect(component.dataSource.length).toBe(2)
104
    expect(component.dataSource[0].name).toBe('Product1')
105
    expect(component.dataSource[0].price).toBe(1)
106
    expect(component.dataSource[0].BasketItem.quantity).toBe(1)
107
    expect(component.dataSource[1].name).toBe('Product2')
108
    expect(component.dataSource[1].price).toBe(2)
109
    expect(component.dataSource[1].BasketItem.quantity).toBe(2)
110
  })
111

112
  it('should have price equal to deluxePrice for deluxe users', () => {
113
    deluxeGuard.isDeluxe.and.returnValue(true)
114
    basketService.find.and.returnValue(of({ Products: [{ name: 'Product1', price: 2, deluxePrice: 1, BasketItem: { quantity: 1 } }] }))
115
    component.load()
116
    expect(component.dataSource.length).toBe(1)
117
    expect(component.dataSource[0].name).toBe('Product1')
118
    expect(component.dataSource[0].price).toBe(1)
119
  })
120

121
  it('should have price different from deluxePrice for non-deluxe users', () => {
122
    deluxeGuard.isDeluxe.and.returnValue(false)
123
    basketService.find.and.returnValue(of({ Products: [{ name: 'Product1', price: 2, deluxePrice: 1, BasketItem: { quantity: 1 } }] }))
124
    component.load()
125
    expect(component.dataSource.length).toBe(1)
126
    expect(component.dataSource[0].name).toBe('Product1')
127
    expect(component.dataSource[0].price).toBe(2)
128
  })
129

130
  it('should hold no products on error in backend API', fakeAsync(() => {
131
    basketService.find.and.returnValue(throwError('Error'))
132
    component.load()
133
    expect(component.dataSource.length).toBe(0)
134
  }))
135

136
  it('should hold no products when none are returned by backend API', () => {
137
    basketService.find.and.returnValue(of({ Products: [] }))
138
    component.load()
139
    expect(component.dataSource).toEqual([])
140
  })
141

142
  it('should log error while getting Products from backend API directly to browser console', fakeAsync(() => {
143
    basketService.find.and.returnValue(throwError('Error'))
144
    console.log = jasmine.createSpy('log')
145
    component.load()
146
    expect(console.log).toHaveBeenCalledWith('Error')
147
  }))
148

149
  it('should pass delete request for basket item via BasketService', () => {
150
    component.delete(1)
151
    expect(basketService.del).toHaveBeenCalledWith(1)
152
  })
153

154
  it('should load again after deleting a basket item', () => {
155
    basketService.find.and.returnValue(of({ Products: [{ name: 'Product1', price: 1, deluxePrice: 1, BasketItem: { quantity: 1 } }, { name: 'Product2', price: 2, deluxePrice: 2, BasketItem: { quantity: 2 } }] }))
156
    component.delete(1)
157
    expect(component.dataSource.length).toBe(2)
158
    expect(component.dataSource[0].name).toBe('Product1')
159
    expect(component.dataSource[0].price).toBe(1)
160
    expect(component.dataSource[0].BasketItem.quantity).toBe(1)
161
    expect(component.dataSource[1].name).toBe('Product2')
162
    expect(component.dataSource[1].price).toBe(2)
163
    expect(component.dataSource[1].BasketItem.quantity).toBe(2)
164
  })
165

166
  it('should log error while deleting basket item directly to browser console', fakeAsync(() => {
167
    basketService.del.and.returnValue(throwError('Error'))
168
    console.log = jasmine.createSpy('log')
169
    component.delete(1)
170
    expect(console.log).toHaveBeenCalledWith('Error')
171
  }))
172

173
  it('should update basket item with increased quantity after adding another item of same type', () => {
174
    basketService.find.and.returnValue(of({ Products: [{ name: 'Product1', price: 1, deluxePrice: 1, BasketItem: { id: 1, quantity: 1 } }] }))
175
    basketService.get.and.returnValue(of({ id: 1, quantity: 1 }))
176
    component.inc(1)
177
    expect(basketService.get).toHaveBeenCalledWith(1)
178
    expect(basketService.put).toHaveBeenCalledWith(1, { quantity: 2 })
179
  })
180

181
  it('should not increase quantity on error retrieving basket item and log the error', fakeAsync(() => {
182
    basketService.get.and.returnValue(throwError('Error'))
183
    console.log = jasmine.createSpy('log')
184
    component.inc(1)
185
    expect(console.log).toHaveBeenCalledWith('Error')
186
    expect(basketService.put).not.toHaveBeenCalled()
187
  }))
188

189
  it('should not increase quantity on error updating basket item and log the error', fakeAsync(() => {
190
    basketService.find.and.returnValue(of({ Products: [{ BasketItem: { id: 1, quantity: 1 } }] }))
191
    basketService.get.and.returnValue(of({ id: 1, quantity: 1 }))
192
    basketService.put.and.returnValue(throwError('Error'))
193
    console.log = jasmine.createSpy('log')
194
    component.inc(1)
195
    expect(console.log).toHaveBeenCalledWith('Error')
196
  }))
197

198
  it('should load again after increasing product quantity', () => {
199
    basketService.find.and.returnValue(of({ Products: [{ BasketItem: { id: 1, quantity: 2 } }] }))
200
    basketService.get.and.returnValue(of({ id: 1, quantity: 2 }))
201
    basketService.put.and.returnValue(of({ id: 1, quantity: 3 }))
202
    component.inc(1)
203
    expect(basketService.find).toHaveBeenCalled()
204
  })
205

206
  it('should update basket item with decreased quantity after removing an item', () => {
207
    basketService.find.and.returnValue(of({ Products: [{ BasketItem: { id: 1, quantity: 2 } }] }))
208
    basketService.get.and.returnValue(of({ id: 1, quantity: 2 }))
209
    basketService.put.and.returnValue(of({ id: 1, quantity: 1 }))
210
    component.dec(1)
211
    expect(basketService.get).toHaveBeenCalledWith(1)
212
    expect(basketService.put).toHaveBeenCalledWith(1, { quantity: 1 })
213
  })
214

215
  it('should always keep one item of any product in the basket when reducing quantity via UI', () => {
216
    basketService.find.and.returnValue(of({ Products: [{ BasketItem: { id: 1, quantity: 1 } }] }))
217
    basketService.get.and.returnValue(of({ id: 1, quantity: 1 }))
218
    basketService.put.and.returnValue(of({ id: 1, quantity: 1 }))
219
    component.dec(1)
220
    expect(basketService.get).toHaveBeenCalledWith(1)
221
    expect(basketService.put).toHaveBeenCalledWith(1, { quantity: 1 })
222
  })
223

224
  it('should not decrease quantity on error retrieving basket item and log the error', fakeAsync(() => {
225
    basketService.get.and.returnValue(throwError('Error'))
226
    console.log = jasmine.createSpy('log')
227
    component.dec(1)
228
    expect(console.log).toHaveBeenCalledWith('Error')
229
    expect(basketService.put).not.toHaveBeenCalled()
230
  }))
231

232
  it('should not decrease quantity on error updating basket item and log the error', fakeAsync(() => {
233
    basketService.find.and.returnValue(of({ Products: [{ BasketItem: { id: 1, quantity: 1 } }] }))
234
    basketService.get.and.returnValue(of({ id: 1, quantity: 1 }))
235
    basketService.put.and.returnValue(throwError('Error'))
236
    console.log = jasmine.createSpy('log')
237
    component.dec(1)
238
    expect(console.log).toHaveBeenCalledWith('Error')
239
  }))
240

241
  it('should load again after decreasing product quantity', () => {
242
    basketService.find.and.returnValue(of({ Products: [{ BasketItem: { id: 1, quantity: 2 } }] }))
243
    basketService.get.and.returnValue(of({ id: 1, quantity: 2 }))
244
    basketService.put.and.returnValue(of({ id: 1, quantity: 1 }))
245
    component.dec(1)
246
    expect(basketService.find).toHaveBeenCalled()
247
  })
248

249
  it('should reset quantity to 1 when decreasing for quantity tampered to be negative', () => {
250
    basketService.find.and.returnValue(of({ Products: [{ BasketItem: { id: 1, quantity: -100 } }] }))
251
    basketService.get.and.returnValue(of({ id: 1, quantity: -100 }))
252
    basketService.put.and.returnValue(of({ id: 1, quantity: 1 }))
253
    component.dec(1)
254
    expect(basketService.get).toHaveBeenCalledWith(1)
255
    expect(basketService.put).toHaveBeenCalledWith(1, { quantity: 1 })
256
  })
257

258
  it('should reset quantity to 1 when increasing for quantity tampered to be negative', () => {
259
    basketService.find.and.returnValue(of({ Products: [{ BasketItem: { id: 1, quantity: -100 } }] }))
260
    basketService.get.and.returnValue(of({ id: 1, quantity: -100 }))
261
    basketService.put.and.returnValue(of({ id: 1, quantity: 1 }))
262
    component.inc(1)
263
    expect(basketService.get).toHaveBeenCalledWith(1)
264
    expect(basketService.put).toHaveBeenCalledWith(1, { quantity: 1 })
265
  })
266
})
267

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

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

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

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