juice-shop

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

6
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'
7
import { fakeAsync, inject, TestBed, tick } from '@angular/core/testing'
8

9
import { BasketService } from './basket.service'
10

11
describe('BasketService', () => {
12
  beforeEach(() => {
13
    TestBed.configureTestingModule({
14
      imports: [HttpClientTestingModule],
15
      providers: [BasketService]
16
    })
17
  })
18

19
  it('should be created', inject([BasketService], (service: BasketService) => {
20
    expect(service).toBeTruthy()
21
  }))
22

23
  it('should get basket directly from the rest api', inject([BasketService, HttpTestingController],
24
    fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
25
      let res: any
26
      service.find(1).subscribe((data) => (res = data))
27
      const req = httpMock.expectOne('http://localhost:3000/rest/basket/1')
28
      req.flush({ data: 'apiResponse' })
29
      tick()
30
      expect(req.request.method).toBe('GET')
31
      expect(res).toBe('apiResponse')
32
      httpMock.verify()
33
    })
34
  ))
35

36
  it('should get single basket item directly from the rest api', inject([BasketService, HttpTestingController],
37
    fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
38
      let res: any
39
      service.get(1).subscribe((data) => (res = data))
40
      const req = httpMock.expectOne('http://localhost:3000/api/BasketItems/1')
41
      req.flush({ data: 'apiResponse' })
42
      tick()
43
      expect(req.request.method).toBe('GET')
44
      expect(res).toBe('apiResponse')
45
      httpMock.verify()
46
    })
47
  ))
48

49
  it('should create basket item directly from the rest api', inject([BasketService, HttpTestingController],
50
    fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
51
      let res: any
52
      service.save().subscribe((data) => (res = data))
53
      const req = httpMock.expectOne('http://localhost:3000/api/BasketItems/')
54
      req.flush({ data: 'apiResponse' })
55
      tick()
56
      expect(req.request.method).toBe('POST')
57
      expect(res).toBe('apiResponse')
58
      httpMock.verify()
59
    })
60
  ))
61

62
  it('should update basket item directly from the rest api', inject([BasketService, HttpTestingController],
63
    fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
64
      let res: any
65
      service.put(1, {}).subscribe((data) => (res = data))
66
      const req = httpMock.expectOne('http://localhost:3000/api/BasketItems/1')
67
      req.flush({ data: 'apiResponse' })
68
      tick()
69
      expect(req.request.method).toBe('PUT')
70
      expect(res).toBe('apiResponse')
71
      httpMock.verify()
72
    })
73
  ))
74

75
  it('should delete basket item directly from the rest api', inject([BasketService, HttpTestingController],
76
    fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
77
      let res: any
78
      service.del(1).subscribe((data) => (res = data))
79
      const req = httpMock.expectOne('http://localhost:3000/api/BasketItems/1')
80
      req.flush({ data: 'apiResponse' })
81
      tick()
82
      expect(req.request.method).toBe('DELETE')
83
      expect(res).toBe('apiResponse')
84
      httpMock.verify()
85
    })
86
  ))
87

88
  it('should place order for basket via the rest api', inject([BasketService, HttpTestingController],
89
    fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
90
      let res: any
91
      service.checkout(1).subscribe((data) => (res = data))
92
      const req = httpMock.expectOne('http://localhost:3000/rest/basket/1/checkout')
93
      req.flush({ orderConfirmation: 'apiResponse' })
94
      tick()
95
      expect(req.request.method).toBe('POST')
96
      expect(res).toBe('apiResponse')
97
      httpMock.verify()
98
    })
99
  ))
100

101
  it('should apply coupon to basket via the rest api', inject([BasketService, HttpTestingController],
102
    fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
103
      let res: any
104
      service.applyCoupon(1, '1234567890').subscribe((data) => (res = data))
105
      const req = httpMock.expectOne('http://localhost:3000/rest/basket/1/coupon/1234567890')
106
      req.flush({ discount: 'apiResponse' })
107
      tick()
108
      expect(req.request.method).toBe('PUT')
109
      expect(res).toBe('apiResponse')
110
      httpMock.verify()
111
    })
112
  ))
113
})
114

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

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

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

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