|
1 | 1 | import { TestBed, async } from '@angular/core/testing'; |
2 | 2 | import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
3 | 3 | import { CustomerService } from './customer-service'; |
| 4 | +import { HttpClientModule } from '@angular/common/http'; |
| 5 | +import { mockData, mockApiparam } from '../test/mockData'; |
| 6 | +import { Customer } from '../model/customer'; |
| 7 | +import { apiParam } from '../model/param'; |
| 8 | +import { inject } from '@angular/core/src/di'; |
4 | 9 |
|
5 | 10 | describe('CustomerServiceService', () => { |
| 11 | + let service: CustomerService; |
6 | 12 | beforeEach(() => TestBed.configureTestingModule({})); |
7 | 13 | beforeEach(async(() => { |
8 | 14 | TestBed.configureTestingModule({ |
9 | | - imports: [ |
10 | | - HttpClientTestingModule, |
11 | | - ], |
| 15 | + imports: [HttpClientModule, HttpClientTestingModule], |
| 16 | + providers: [CustomerService] |
12 | 17 | }).compileComponents(); |
| 18 | + service = TestBed.get(CustomerService); |
13 | 19 | })); |
14 | 20 | it('should be created', () => { |
15 | 21 | const service: CustomerService = TestBed.get(CustomerService); |
16 | 22 | expect(service).toBeTruthy(); |
17 | 23 | }); |
| 24 | + |
| 25 | + it('be able to retrieve Customer Data from the API bia GET', () => { |
| 26 | + const dummyPosts: Customer = mockData; |
| 27 | + service.getCustomer().subscribe(posts => { |
| 28 | + expect(posts.servers.length).toBe(2); |
| 29 | + expect(posts).toEqual(dummyPosts); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('be able to retrieve Customer filter data', () => { |
| 34 | + const dummyPosts: Customer = mockData; |
| 35 | + const param: apiParam = mockApiparam; |
| 36 | + service.getFilteredCustomerList(param).subscribe(posts => { |
| 37 | + expect(posts.servers[0].location).toEqual('AmsterdamAMS-01'); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('correctly handles error', () => { |
| 42 | + const spy = spyOn(console, 'log'); |
| 43 | + const error: Error = new Error('ERROR'); |
| 44 | + service.handleError(error); |
| 45 | + }); |
18 | 46 | }); |
| 47 | + |
| 48 | + |
0 commit comments