]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
2 | import { fakeAsync, TestBed, tick } from '@angular/core/testing'; | |
3 | ||
f67539c2 | 4 | import { configureTestBed } from '~/testing/unit-test-helper'; |
aee94f69 | 5 | import { CdTableFetchDataContext } from '../models/cd-table-fetch-data-context'; |
11fdf7f2 TL |
6 | import { HostService } from './host.service'; |
7 | ||
8 | describe('HostService', () => { | |
9 | let service: HostService; | |
10 | let httpTesting: HttpTestingController; | |
11 | ||
12 | configureTestBed({ | |
13 | providers: [HostService], | |
14 | imports: [HttpClientTestingModule] | |
15 | }); | |
16 | ||
17 | beforeEach(() => { | |
f67539c2 TL |
18 | service = TestBed.inject(HostService); |
19 | httpTesting = TestBed.inject(HttpTestingController); | |
11fdf7f2 TL |
20 | }); |
21 | ||
22 | afterEach(() => { | |
23 | httpTesting.verify(); | |
24 | }); | |
25 | ||
26 | it('should be created', () => { | |
27 | expect(service).toBeTruthy(); | |
28 | }); | |
29 | ||
30 | it('should call list', fakeAsync(() => { | |
aee94f69 TL |
31 | let result: any[] = [{}, {}]; |
32 | const hostContext = new CdTableFetchDataContext(() => undefined); | |
33 | service.list(hostContext.toParams(), 'true').subscribe((resp) => (result = resp)); | |
34 | const req = httpTesting.expectOne('api/host?offset=0&limit=10&search=&sort=%2Bname&facts=true'); | |
11fdf7f2 | 35 | expect(req.request.method).toBe('GET'); |
aee94f69 | 36 | req.flush([{ foo: 1 }, { bar: 2 }]); |
11fdf7f2 | 37 | tick(); |
aee94f69 TL |
38 | expect(result[0].foo).toEqual(1); |
39 | expect(result[1].bar).toEqual(2); | |
11fdf7f2 | 40 | })); |
9f95a23c TL |
41 | |
42 | it('should make a GET request on the devices endpoint when requesting devices', () => { | |
43 | const hostname = 'hostname'; | |
44 | service.getDevices(hostname).subscribe(); | |
45 | const req = httpTesting.expectOne(`api/host/${hostname}/devices`); | |
46 | expect(req.request.method).toBe('GET'); | |
47 | }); | |
f6b5b4d7 TL |
48 | |
49 | it('should update host', fakeAsync(() => { | |
20effc67 | 50 | service.update('mon0', true, ['foo', 'bar'], true, false).subscribe(); |
f6b5b4d7 TL |
51 | const req = httpTesting.expectOne('api/host/mon0'); |
52 | expect(req.request.method).toBe('PUT'); | |
f67539c2 TL |
53 | expect(req.request.body).toEqual({ |
54 | force: false, | |
55 | labels: ['foo', 'bar'], | |
20effc67 TL |
56 | maintenance: true, |
57 | update_labels: true, | |
58 | drain: false | |
59 | }); | |
60 | })); | |
61 | ||
62 | it('should test host drain call', fakeAsync(() => { | |
63 | service.update('host0', false, null, false, false, true).subscribe(); | |
64 | const req = httpTesting.expectOne('api/host/host0'); | |
65 | expect(req.request.method).toBe('PUT'); | |
66 | expect(req.request.body).toEqual({ | |
67 | force: false, | |
68 | labels: null, | |
f67539c2 | 69 | maintenance: false, |
20effc67 TL |
70 | update_labels: false, |
71 | drain: true | |
f67539c2 | 72 | }); |
f6b5b4d7 | 73 | })); |
f67539c2 TL |
74 | |
75 | it('should call getInventory', () => { | |
76 | service.getInventory('host-0').subscribe(); | |
77 | let req = httpTesting.expectOne('api/host/host-0/inventory'); | |
78 | expect(req.request.method).toBe('GET'); | |
79 | ||
80 | service.getInventory('host-0', true).subscribe(); | |
81 | req = httpTesting.expectOne('api/host/host-0/inventory?refresh=true'); | |
82 | expect(req.request.method).toBe('GET'); | |
83 | }); | |
84 | ||
85 | it('should call inventoryList', () => { | |
86 | service.inventoryList().subscribe(); | |
87 | let req = httpTesting.expectOne('ui-api/host/inventory'); | |
88 | expect(req.request.method).toBe('GET'); | |
89 | ||
90 | service.inventoryList(true).subscribe(); | |
91 | req = httpTesting.expectOne('ui-api/host/inventory?refresh=true'); | |
92 | expect(req.request.method).toBe('GET'); | |
93 | }); | |
11fdf7f2 | 94 | }); |