|
1 | | -import { ComponentFixture, TestBed } from '@angular/core/testing'; |
2 | | - |
| 1 | +import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; |
| 2 | +import { of, BehaviorSubject, combineLatest } from 'rxjs'; |
3 | 3 | import { RgwOverviewDashboardComponent } from './rgw-overview-dashboard.component'; |
4 | | -import { of } from 'rxjs'; |
| 4 | +import { HttpClientTestingModule } from '@angular/common/http/testing'; |
| 5 | +import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service'; |
5 | 6 | import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service'; |
6 | 7 | import { RgwDaemon } from '../models/rgw-daemon'; |
7 | | -import { HttpClientTestingModule } from '@angular/common/http/testing'; |
| 8 | +import { CardComponent } from '~/app/shared/components/card/card.component'; |
| 9 | +import { CardRowComponent } from '~/app/shared/components/card-row/card-row.component'; |
8 | 10 | import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe'; |
| 11 | +import { NO_ERRORS_SCHEMA } from '@angular/core'; |
9 | 12 | import { RgwRealmService } from '~/app/shared/api/rgw-realm.service'; |
10 | | -import { RgwZonegroupService } from '~/app/shared/api/rgw-zonegroup.service'; |
11 | 13 | import { RgwZoneService } from '~/app/shared/api/rgw-zone.service'; |
12 | | -import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service'; |
13 | | -import { HealthService } from '~/app/shared/api/health.service'; |
14 | | -import { CardRowComponent } from '~/app/shared/components/card-row/card-row.component'; |
15 | | -import { CardComponent } from '~/app/shared/components/card/card.component'; |
16 | | -import { NO_ERRORS_SCHEMA } from '@angular/core'; |
17 | | -import { configureTestBed } from '~/testing/unit-test-helper'; |
| 14 | +import { RgwZonegroupService } from '~/app/shared/api/rgw-zonegroup.service'; |
18 | 15 |
|
19 | 16 | describe('RgwOverviewDashboardComponent', () => { |
20 | 17 | let component: RgwOverviewDashboardComponent; |
21 | 18 | let fixture: ComponentFixture<RgwOverviewDashboardComponent>; |
| 19 | + let listDaemonsSpy: jest.SpyInstance; |
| 20 | + let listRealmsSpy: jest.SpyInstance; |
| 21 | + let listZonegroupsSpy: jest.SpyInstance; |
| 22 | + let listZonesSpy: jest.SpyInstance; |
| 23 | + let fetchAndTransformBucketsSpy: jest.SpyInstance; |
| 24 | + let totalBucketsAndUsersSpy: jest.SpyInstance; |
| 25 | + |
| 26 | + const totalNumObjectsSubject = new BehaviorSubject<number>(290); |
| 27 | + const totalUsedCapacitySubject = new BehaviorSubject<number>(9338880); |
| 28 | + const averageObjectSizeSubject = new BehaviorSubject<number>(1280); |
| 29 | + const bucketsCount = 2; |
| 30 | + const usersCount = 5; |
22 | 31 | const daemon: RgwDaemon = { |
23 | 32 | id: '8000', |
24 | 33 | service_map_id: '4803', |
@@ -47,95 +56,105 @@ describe('RgwOverviewDashboardComponent', () => { |
47 | 56 | zones: ['zone4', 'zone5', 'zone6', 'zone7'] |
48 | 57 | }; |
49 | 58 |
|
50 | | - const bucketAndUserList = { |
51 | | - buckets_count: 2, |
52 | | - users_count: 2 |
53 | | - }; |
54 | | - |
55 | | - const healthData = { |
56 | | - total_objects: '290', |
57 | | - total_pool_bytes_used: 9338880 |
58 | | - }; |
59 | | - |
60 | | - let listDaemonsSpy: jest.SpyInstance; |
61 | | - let listZonesSpy: jest.SpyInstance; |
62 | | - let listZonegroupsSpy: jest.SpyInstance; |
63 | | - let listRealmsSpy: jest.SpyInstance; |
64 | | - let listBucketsSpy: jest.SpyInstance; |
65 | | - let healthDataSpy: jest.SpyInstance; |
66 | | - |
67 | | - configureTestBed({ |
68 | | - declarations: [ |
69 | | - RgwOverviewDashboardComponent, |
70 | | - CardComponent, |
71 | | - CardRowComponent, |
72 | | - DimlessBinaryPipe |
73 | | - ], |
74 | | - schemas: [NO_ERRORS_SCHEMA], |
75 | | - imports: [HttpClientTestingModule] |
76 | | - }); |
77 | | - |
78 | 59 | beforeEach(() => { |
| 60 | + TestBed.configureTestingModule({ |
| 61 | + declarations: [ |
| 62 | + RgwOverviewDashboardComponent, |
| 63 | + CardComponent, |
| 64 | + CardRowComponent, |
| 65 | + DimlessBinaryPipe |
| 66 | + ], |
| 67 | + schemas: [NO_ERRORS_SCHEMA], |
| 68 | + providers: [ |
| 69 | + { provide: RgwDaemonService, useValue: { list: jest.fn() } }, |
| 70 | + { provide: RgwRealmService, useValue: { list: jest.fn() } }, |
| 71 | + { provide: RgwZonegroupService, useValue: { list: jest.fn() } }, |
| 72 | + { provide: RgwZoneService, useValue: { list: jest.fn() } }, |
| 73 | + { |
| 74 | + provide: RgwBucketService, |
| 75 | + useValue: { |
| 76 | + fetchAndTransformBuckets: jest.fn(), |
| 77 | + totalNumObjects$: totalNumObjectsSubject.asObservable(), |
| 78 | + totalUsedCapacity$: totalUsedCapacitySubject.asObservable(), |
| 79 | + averageObjectSize$: averageObjectSizeSubject.asObservable(), |
| 80 | + getTotalBucketsAndUsersLength: jest.fn() |
| 81 | + } |
| 82 | + } |
| 83 | + ], |
| 84 | + imports: [HttpClientTestingModule] |
| 85 | + }).compileComponents(); |
| 86 | + fixture = TestBed.createComponent(RgwOverviewDashboardComponent); |
| 87 | + component = fixture.componentInstance; |
79 | 88 | listDaemonsSpy = jest |
80 | 89 | .spyOn(TestBed.inject(RgwDaemonService), 'list') |
81 | 90 | .mockReturnValue(of([daemon])); |
| 91 | + fetchAndTransformBucketsSpy = jest |
| 92 | + .spyOn(TestBed.inject(RgwBucketService), 'fetchAndTransformBuckets') |
| 93 | + .mockReturnValue(of(null)); |
| 94 | + totalBucketsAndUsersSpy = jest |
| 95 | + .spyOn(TestBed.inject(RgwBucketService), 'getTotalBucketsAndUsersLength') |
| 96 | + .mockReturnValue(of({ buckets_count: bucketsCount, users_count: usersCount })); |
82 | 97 | listRealmsSpy = jest |
83 | 98 | .spyOn(TestBed.inject(RgwRealmService), 'list') |
84 | 99 | .mockReturnValue(of(realmList)); |
85 | 100 | listZonegroupsSpy = jest |
86 | 101 | .spyOn(TestBed.inject(RgwZonegroupService), 'list') |
87 | 102 | .mockReturnValue(of(zonegroupList)); |
88 | 103 | listZonesSpy = jest.spyOn(TestBed.inject(RgwZoneService), 'list').mockReturnValue(of(zoneList)); |
89 | | - listBucketsSpy = jest |
90 | | - .spyOn(TestBed.inject(RgwBucketService), 'getTotalBucketsAndUsersLength') |
91 | | - .mockReturnValue(of(bucketAndUserList)); |
92 | | - healthDataSpy = jest |
93 | | - .spyOn(TestBed.inject(HealthService), 'getClusterCapacity') |
94 | | - .mockReturnValue(of(healthData)); |
95 | | - fixture = TestBed.createComponent(RgwOverviewDashboardComponent); |
96 | | - component = fixture.componentInstance; |
97 | 104 | fixture.detectChanges(); |
98 | 105 | }); |
99 | 106 |
|
100 | | - it('should create', () => { |
| 107 | + it('should create the component', () => { |
101 | 108 | expect(component).toBeTruthy(); |
102 | 109 | }); |
103 | 110 |
|
104 | 111 | it('should render all cards', () => { |
105 | | - fixture.detectChanges(); |
106 | 112 | const dashboardCards = fixture.debugElement.nativeElement.querySelectorAll('cd-card'); |
107 | 113 | expect(dashboardCards.length).toBe(5); |
108 | 114 | }); |
109 | 115 |
|
110 | | - it('should get corresponding data into Daemons', () => { |
111 | | - expect(listDaemonsSpy).toHaveBeenCalled(); |
112 | | - expect(component.rgwDaemonCount).toEqual(1); |
113 | | - }); |
114 | | - |
115 | | - it('should get corresponding data into Realms', () => { |
| 116 | + it('should get data for Realms', () => { |
116 | 117 | expect(listRealmsSpy).toHaveBeenCalled(); |
117 | 118 | expect(component.rgwRealmCount).toEqual(2); |
118 | 119 | }); |
119 | 120 |
|
120 | | - it('should get corresponding data into Zonegroups', () => { |
| 121 | + it('should get data for Zonegroups', () => { |
121 | 122 | expect(listZonegroupsSpy).toHaveBeenCalled(); |
122 | 123 | expect(component.rgwZonegroupCount).toEqual(3); |
123 | 124 | }); |
124 | 125 |
|
125 | | - it('should get corresponding data into Zones', () => { |
| 126 | + it('should get data for Zones', () => { |
126 | 127 | expect(listZonesSpy).toHaveBeenCalled(); |
127 | 128 | expect(component.rgwZoneCount).toEqual(4); |
128 | 129 | }); |
129 | 130 |
|
130 | | - it('should get corresponding data into Buckets', () => { |
131 | | - expect(listBucketsSpy).toHaveBeenCalled(); |
132 | | - expect(component.rgwBucketCount).toEqual(2); |
133 | | - expect(component.UserCount).toEqual(2); |
134 | | - }); |
135 | | - |
136 | | - it('should get corresponding data into Objects and capacity', () => { |
137 | | - expect(healthDataSpy).toHaveBeenCalled(); |
138 | | - expect(component.objectCount).toEqual('290'); |
| 131 | + it('should set component properties from services using combineLatest', fakeAsync(() => { |
| 132 | + component.interval = of(null).subscribe(() => { |
| 133 | + component.fetchDataSub = combineLatest([ |
| 134 | + TestBed.inject(RgwDaemonService).list(), |
| 135 | + TestBed.inject(RgwBucketService).fetchAndTransformBuckets(), |
| 136 | + totalNumObjectsSubject.asObservable(), |
| 137 | + totalUsedCapacitySubject.asObservable(), |
| 138 | + averageObjectSizeSubject.asObservable(), |
| 139 | + TestBed.inject(RgwBucketService).getTotalBucketsAndUsersLength() |
| 140 | + ]).subscribe(([daemonData, _, objectCount, usedCapacity, averageSize, bucketData]) => { |
| 141 | + component.rgwDaemonCount = daemonData.length; |
| 142 | + component.objectCount = objectCount; |
| 143 | + component.totalPoolUsedBytes = usedCapacity; |
| 144 | + component.averageObjectSize = averageSize; |
| 145 | + component.rgwBucketCount = bucketData.buckets_count; |
| 146 | + component.UserCount = bucketData.users_count; |
| 147 | + }); |
| 148 | + }); |
| 149 | + tick(); |
| 150 | + expect(listDaemonsSpy).toHaveBeenCalled(); |
| 151 | + expect(fetchAndTransformBucketsSpy).toHaveBeenCalled(); |
| 152 | + expect(totalBucketsAndUsersSpy).toHaveBeenCalled(); |
| 153 | + expect(component.rgwDaemonCount).toEqual(1); |
| 154 | + expect(component.objectCount).toEqual(290); |
139 | 155 | expect(component.totalPoolUsedBytes).toEqual(9338880); |
140 | | - }); |
| 156 | + expect(component.averageObjectSize).toEqual(1280); |
| 157 | + expect(component.rgwBucketCount).toEqual(bucketsCount); |
| 158 | + expect(component.UserCount).toEqual(usersCount); |
| 159 | + })); |
141 | 160 | }); |
0 commit comments