]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClient } from '@angular/common/http'; |
2 | import { fakeAsync, TestBed, tick } from '@angular/core/testing'; | |
3 | import { RouterTestingModule } from '@angular/router/testing'; | |
4 | ||
1911f103 | 5 | import { of as observableOf, Subscriber, Subscription } from 'rxjs'; |
11fdf7f2 | 6 | |
f67539c2 | 7 | import { configureTestBed } from '~/testing/unit-test-helper'; |
11fdf7f2 | 8 | import { ExecutingTask } from '../models/executing-task'; |
f6b5b4d7 | 9 | import { Summary } from '../models/summary.model'; |
11fdf7f2 TL |
10 | import { AuthStorageService } from './auth-storage.service'; |
11 | import { SummaryService } from './summary.service'; | |
12 | ||
13 | describe('SummaryService', () => { | |
14 | let summaryService: SummaryService; | |
15 | let authStorageService: AuthStorageService; | |
1911f103 | 16 | let subs: Subscription; |
11fdf7f2 | 17 | |
f6b5b4d7 | 18 | const summary: Summary = { |
11fdf7f2 TL |
19 | executing_tasks: [], |
20 | health_status: 'HEALTH_OK', | |
21 | mgr_id: 'x', | |
22 | rbd_mirroring: { errors: 0, warnings: 0 }, | |
23 | rbd_pools: [], | |
24 | have_mon_connection: true, | |
25 | finished_tasks: [], | |
26 | filesystems: [{ id: 1, name: 'cephfs_a' }] | |
27 | }; | |
28 | ||
29 | const httpClientSpy = { | |
30 | get: () => observableOf(summary) | |
31 | }; | |
32 | ||
f6b5b4d7 TL |
33 | const nextSummary = (newData: any) => summaryService['summaryDataSource'].next(newData); |
34 | ||
11fdf7f2 TL |
35 | configureTestBed({ |
36 | imports: [RouterTestingModule], | |
37 | providers: [ | |
38 | SummaryService, | |
39 | AuthStorageService, | |
40 | { provide: HttpClient, useValue: httpClientSpy } | |
41 | ] | |
42 | }); | |
43 | ||
44 | beforeEach(() => { | |
f67539c2 TL |
45 | summaryService = TestBed.inject(SummaryService); |
46 | authStorageService = TestBed.inject(AuthStorageService); | |
11fdf7f2 TL |
47 | }); |
48 | ||
49 | it('should be created', () => { | |
50 | expect(summaryService).toBeTruthy(); | |
51 | }); | |
52 | ||
53 | it('should call refresh', fakeAsync(() => { | |
11fdf7f2 | 54 | authStorageService.set('foobar', undefined, undefined); |
9f95a23c | 55 | const calledWith: any[] = []; |
1911f103 TL |
56 | subs = new Subscription(); |
57 | subs.add(summaryService.startPolling()); | |
58 | tick(); | |
59 | subs.add( | |
60 | summaryService.subscribe((data) => { | |
61 | calledWith.push(data); | |
62 | }) | |
63 | ); | |
11fdf7f2 | 64 | expect(calledWith).toEqual([summary]); |
1911f103 | 65 | subs.add(summaryService.refresh()); |
11fdf7f2 | 66 | expect(calledWith).toEqual([summary, summary]); |
1911f103 | 67 | tick(summaryService.REFRESH_INTERVAL * 2); |
11fdf7f2 | 68 | expect(calledWith.length).toEqual(4); |
1911f103 | 69 | subs.unsubscribe(); |
11fdf7f2 TL |
70 | })); |
71 | ||
f6b5b4d7 TL |
72 | describe('Should test subscribe without initial value', () => { |
73 | let result: Summary; | |
74 | let i: number; | |
75 | ||
76 | const callback = (response: Summary) => { | |
77 | i++; | |
78 | result = response; | |
79 | }; | |
80 | ||
11fdf7f2 | 81 | beforeEach(() => { |
f6b5b4d7 TL |
82 | i = 0; |
83 | result = undefined; | |
84 | nextSummary(undefined); | |
11fdf7f2 TL |
85 | }); |
86 | ||
f6b5b4d7 TL |
87 | it('should call subscribeOnce', () => { |
88 | const subscriber = summaryService.subscribeOnce(callback); | |
89 | ||
90 | expect(subscriber).toEqual(jasmine.any(Subscriber)); | |
91 | expect(i).toBe(0); | |
92 | expect(result).toEqual(undefined); | |
93 | ||
94 | nextSummary(undefined); | |
95 | expect(i).toBe(0); | |
96 | expect(result).toEqual(undefined); | |
97 | expect(subscriber.closed).toBe(false); | |
98 | ||
99 | nextSummary(summary); | |
100 | expect(result).toEqual(summary); | |
101 | expect(i).toBe(1); | |
102 | expect(subscriber.closed).toBe(true); | |
103 | ||
104 | nextSummary(summary); | |
105 | expect(result).toEqual(summary); | |
106 | expect(i).toBe(1); | |
11fdf7f2 TL |
107 | }); |
108 | ||
109 | it('should call subscribe', () => { | |
f6b5b4d7 TL |
110 | const subscriber = summaryService.subscribe(callback); |
111 | ||
11fdf7f2 | 112 | expect(subscriber).toEqual(jasmine.any(Subscriber)); |
f6b5b4d7 TL |
113 | expect(i).toBe(0); |
114 | expect(result).toEqual(undefined); | |
115 | ||
116 | nextSummary(undefined); | |
117 | expect(i).toBe(0); | |
118 | expect(result).toEqual(undefined); | |
119 | expect(subscriber.closed).toBe(false); | |
120 | ||
121 | nextSummary(summary); | |
122 | expect(result).toEqual(summary); | |
123 | expect(i).toBe(1); | |
124 | expect(subscriber.closed).toBe(false); | |
125 | ||
126 | nextSummary(summary); | |
11fdf7f2 | 127 | expect(result).toEqual(summary); |
f6b5b4d7 TL |
128 | expect(i).toBe(2); |
129 | expect(subscriber.closed).toBe(false); | |
130 | }); | |
131 | }); | |
132 | ||
133 | describe('Should test methods after first refresh', () => { | |
134 | beforeEach(() => { | |
135 | authStorageService.set('foobar', undefined, undefined); | |
136 | summaryService.refresh(); | |
11fdf7f2 TL |
137 | }); |
138 | ||
139 | it('should call addRunningTask', () => { | |
140 | summaryService.addRunningTask( | |
141 | new ExecutingTask('rbd/delete', { | |
142 | pool_name: 'somePool', | |
143 | image_name: 'someImage' | |
144 | }) | |
145 | ); | |
f6b5b4d7 TL |
146 | let result: any; |
147 | summaryService.subscribeOnce((response) => { | |
148 | result = response; | |
149 | }); | |
150 | ||
11fdf7f2 TL |
151 | expect(result.executing_tasks.length).toBe(1); |
152 | expect(result.executing_tasks[0]).toEqual({ | |
153 | metadata: { image_name: 'someImage', pool_name: 'somePool' }, | |
154 | name: 'rbd/delete' | |
155 | }); | |
156 | }); | |
157 | ||
158 | it('should call addRunningTask with duplicate task', () => { | |
f6b5b4d7 TL |
159 | let result: any; |
160 | summaryService.subscribe((response) => { | |
161 | result = response; | |
162 | }); | |
163 | ||
11fdf7f2 TL |
164 | const exec_task = new ExecutingTask('rbd/delete', { |
165 | pool_name: 'somePool', | |
166 | image_name: 'someImage' | |
167 | }); | |
168 | ||
169 | result.executing_tasks = [exec_task]; | |
f6b5b4d7 TL |
170 | nextSummary(result); |
171 | ||
11fdf7f2 TL |
172 | expect(result.executing_tasks.length).toBe(1); |
173 | ||
174 | summaryService.addRunningTask(exec_task); | |
f6b5b4d7 | 175 | |
11fdf7f2 TL |
176 | expect(result.executing_tasks.length).toBe(1); |
177 | }); | |
178 | }); | |
179 | }); |