juice-shop

Форк
0
/
product.service.spec.ts 
65 строк · 2.3 Кб
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 { ProductService } from './product.service'
10

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

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

23
  it('should search for products directly from the rest api', inject([ProductService, HttpTestingController],
24
    fakeAsync((service: ProductService, httpMock: HttpTestingController) => {
25
      let res: any
26
      service.search('1').subscribe((data) => (res = data))
27
      const req = httpMock.expectOne('http://localhost:3000/rest/products/search?q=1')
28
      req.flush({ data: 'apiResponse' })
29

30
      tick()
31
      expect(req.request.method).toBe('GET')
32
      expect(res).toBe('apiResponse')
33
      httpMock.verify()
34
    })
35
  ))
36

37
  it('should get all products directly from the rest api', inject([ProductService, HttpTestingController],
38
    fakeAsync((service: ProductService, httpMock: HttpTestingController) => {
39
      let res: any
40
      service.find(null).subscribe((data) => (res = data))
41
      const req = httpMock.expectOne('http://localhost:3000/api/Products/')
42
      req.flush({ data: 'apiResponse' })
43

44
      tick()
45
      expect(req.request.method).toBe('GET')
46
      expect(req.request.params.toString()).toBeFalsy()
47
      expect(res).toBe('apiResponse')
48
      httpMock.verify()
49
    })
50
  ))
51

52
  it('should get single product directly from the rest api', inject([ProductService, HttpTestingController],
53
    fakeAsync((service: ProductService, httpMock: HttpTestingController) => {
54
      let res: any
55
      service.get(1).subscribe((data) => (res = data))
56
      const req = httpMock.expectOne('http://localhost:3000/api/Products/1?d=' + encodeURIComponent(new Date().toDateString()))
57
      req.flush({ data: 'apiResponse' })
58

59
      tick()
60
      expect(req.request.method).toBe('GET')
61
      expect(res).toBe('apiResponse')
62
      httpMock.verify()
63
    })
64
  ))
65
})
66

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

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

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

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