|
| 1 | +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
| 2 | +import { inject, TestBed } from '@angular/core/testing'; |
| 3 | +import { PetAPIClient, PetAPIClientModule } from '../api-all-tags/pet'; |
| 4 | +import { DUMMY_DOMAIN } from './tests-constants'; |
| 5 | + |
| 6 | +describe('PetService', () => { |
| 7 | + beforeEach(() => { |
| 8 | + TestBed.configureTestingModule({ |
| 9 | + imports: [ |
| 10 | + HttpClientTestingModule, |
| 11 | + PetAPIClientModule.forRoot({ |
| 12 | + domain: DUMMY_DOMAIN, |
| 13 | + }) |
| 14 | + ], |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should be initialized', inject([PetAPIClient], async (api: PetAPIClient) => { |
| 19 | + await expect(api).toBeTruthy(); |
| 20 | + await expect(api.domain).toBe(DUMMY_DOMAIN); |
| 21 | + })); |
| 22 | + |
| 23 | + it('should set id to path', inject( |
| 24 | + [PetAPIClient, HttpTestingController], |
| 25 | + async (api, backend) => { |
| 26 | + api.getPetById({petId: 42}).subscribe(async (data) => { |
| 27 | + await expect(data).toBeNull(); |
| 28 | + }); |
| 29 | + |
| 30 | + backend |
| 31 | + .expectOne({ |
| 32 | + method: 'GET', |
| 33 | + url: '/pet/42' |
| 34 | + }) |
| 35 | + .flush(null); |
| 36 | + } |
| 37 | + )); |
| 38 | + |
| 39 | + it('should set name and status to query', inject( |
| 40 | + [PetAPIClient, HttpTestingController], |
| 41 | + async (api, backend) => { |
| 42 | + api.updatePetWithQuery({ |
| 43 | + petId: 42, |
| 44 | + name: 'wololo', |
| 45 | + status: 'OK' |
| 46 | + }).subscribe(() => {}); |
| 47 | + |
| 48 | + const req = backend |
| 49 | + .expectOne(req => req.method === 'POST' && req.url === '/pet/42'); |
| 50 | + |
| 51 | + await expect(req.request.params.getAll('name')).toEqual(['wololo']); |
| 52 | + await expect(req.request.params.getAll('status')).toEqual(['OK']); |
| 53 | + } |
| 54 | + )); |
| 55 | + |
| 56 | + it('should delete pet with ID and set api key header', inject( |
| 57 | + [PetAPIClient, HttpTestingController], |
| 58 | + async (api, backend) => { |
| 59 | + api.deletePet({ |
| 60 | + apiKey: 'DUMMY_API_KEY', |
| 61 | + petId: 42, |
| 62 | + }).subscribe(() => {}); |
| 63 | + |
| 64 | + const req = backend |
| 65 | + .expectOne({ |
| 66 | + method: 'DELETE', |
| 67 | + url: '/pet/42', |
| 68 | + }); |
| 69 | + |
| 70 | + await expect(req.request.headers.get('api_key')).toEqual('DUMMY_API_KEY') |
| 71 | + } |
| 72 | + )); |
| 73 | +}); |
0 commit comments