]>
Commit | Line | Data |
---|---|---|
1911f103 | 1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
f67539c2 | 2 | import { ChangeDetectorRef } from '@angular/core'; |
11fdf7f2 | 3 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
1911f103 TL |
4 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
5 | import { RouterTestingModule } from '@angular/router/testing'; | |
11fdf7f2 | 6 | |
f67539c2 | 7 | import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap'; |
11fdf7f2 | 8 | |
f67539c2 TL |
9 | import { RbdConfigurationListComponent } from '~/app/ceph/block/rbd-configuration-list/rbd-configuration-list.component'; |
10 | import { Permissions } from '~/app/shared/models/permissions'; | |
11 | import { SharedModule } from '~/app/shared/shared.module'; | |
12 | import { configureTestBed, Mocks, TabHelper } from '~/testing/unit-test-helper'; | |
11fdf7f2 TL |
13 | import { PoolDetailsComponent } from './pool-details.component'; |
14 | ||
15 | describe('PoolDetailsComponent', () => { | |
16 | let poolDetailsComponent: PoolDetailsComponent; | |
17 | let fixture: ComponentFixture<PoolDetailsComponent>; | |
18 | ||
f67539c2 TL |
19 | // Needed because of ChangeDetectionStrategy.OnPush |
20 | // https://github.com/angular/angular/issues/12313#issuecomment-444623173 | |
21 | let changeDetector: ChangeDetectorRef; | |
22 | const detectChanges = () => { | |
23 | poolDetailsComponent.ngOnChanges(); | |
24 | changeDetector.detectChanges(); // won't call ngOnChanges on it's own but updates fixture | |
25 | }; | |
26 | ||
27 | const updatePoolSelection = (selection: any) => { | |
28 | poolDetailsComponent.selection = selection; | |
29 | detectChanges(); | |
30 | }; | |
31 | ||
32 | const currentPoolUpdate = () => { | |
33 | updatePoolSelection(poolDetailsComponent.selection); | |
34 | }; | |
35 | ||
11fdf7f2 | 36 | configureTestBed({ |
1911f103 TL |
37 | imports: [ |
38 | BrowserAnimationsModule, | |
f67539c2 | 39 | NgbNavModule, |
1911f103 TL |
40 | SharedModule, |
41 | HttpClientTestingModule, | |
42 | RouterTestingModule | |
43 | ], | |
f67539c2 | 44 | declarations: [PoolDetailsComponent, RbdConfigurationListComponent] |
11fdf7f2 TL |
45 | }); |
46 | ||
47 | beforeEach(() => { | |
48 | fixture = TestBed.createComponent(PoolDetailsComponent); | |
f67539c2 TL |
49 | // Needed because of ChangeDetectionStrategy.OnPush |
50 | // https://github.com/angular/angular/issues/12313#issuecomment-444623173 | |
51 | changeDetector = fixture.componentRef.injector.get(ChangeDetectorRef); | |
11fdf7f2 | 52 | poolDetailsComponent = fixture.componentInstance; |
e306af50 | 53 | poolDetailsComponent.selection = undefined; |
11fdf7f2 TL |
54 | poolDetailsComponent.permissions = new Permissions({ |
55 | grafana: ['read'] | |
56 | }); | |
f67539c2 | 57 | updatePoolSelection({ tiers: [0], pool: 0, pool_name: 'micro_pool' }); |
11fdf7f2 TL |
58 | }); |
59 | ||
60 | it('should create', () => { | |
61 | expect(poolDetailsComponent).toBeTruthy(); | |
62 | }); | |
63 | ||
64 | describe('Pool details tabset', () => { | |
11fdf7f2 | 65 | it('should recognize a tabset child', () => { |
f67539c2 TL |
66 | detectChanges(); |
67 | const ngbNav = TabHelper.getNgbNav(fixture); | |
68 | expect(ngbNav).toBeDefined(); | |
11fdf7f2 TL |
69 | }); |
70 | ||
f67539c2 TL |
71 | it('should not change the tabs active status when selection is the same as before', () => { |
72 | const tabs = TabHelper.getNgbNavItems(fixture); | |
11fdf7f2 | 73 | expect(tabs[0].active).toBeTruthy(); |
f67539c2 | 74 | currentPoolUpdate(); |
11fdf7f2 TL |
75 | expect(tabs[0].active).toBeTruthy(); |
76 | ||
f67539c2 TL |
77 | const ngbNav = TabHelper.getNgbNav(fixture); |
78 | ngbNav.select(tabs[1].id); | |
79 | expect(tabs[1].active).toBeTruthy(); | |
80 | currentPoolUpdate(); | |
11fdf7f2 TL |
81 | expect(tabs[1].active).toBeTruthy(); |
82 | }); | |
eafe8130 | 83 | |
f67539c2 TL |
84 | it('should filter out cdExecuting, cdIsBinary and all stats', () => { |
85 | updatePoolSelection({ | |
86 | prop1: 1, | |
87 | cdIsBinary: true, | |
88 | prop2: 2, | |
89 | cdExecuting: true, | |
90 | prop3: 3, | |
91 | stats: { anyStat: 3, otherStat: [1, 2, 3] } | |
92 | }); | |
eafe8130 | 93 | const expectedPool = { prop1: 1, prop2: 2, prop3: 3 }; |
f67539c2 | 94 | expect(poolDetailsComponent.poolDetails).toEqual(expectedPool); |
eafe8130 TL |
95 | }); |
96 | ||
f67539c2 TL |
97 | describe('Updates of shown data', () => { |
98 | const expectedChange = ( | |
99 | expected: { | |
100 | selectedPoolConfiguration?: object; | |
101 | poolDetails?: object; | |
102 | }, | |
103 | newSelection: object, | |
104 | doesNotEqualOld = true | |
105 | ) => { | |
106 | const getData = () => { | |
107 | const data = {}; | |
108 | Object.keys(expected).forEach((key) => (data[key] = poolDetailsComponent[key])); | |
109 | return data; | |
110 | }; | |
111 | const oldData = getData(); | |
112 | updatePoolSelection(newSelection); | |
113 | const newData = getData(); | |
114 | if (doesNotEqualOld) { | |
115 | expect(expected).not.toEqual(oldData); | |
116 | } else { | |
117 | expect(expected).toEqual(oldData); | |
118 | } | |
119 | expect(expected).toEqual(newData); | |
120 | }; | |
eafe8130 | 121 | |
f67539c2 TL |
122 | it('should update shown data on change', () => { |
123 | expectedChange( | |
124 | { | |
125 | poolDetails: { | |
126 | pg_num: 256, | |
127 | pg_num_target: 256, | |
128 | pg_placement_num: 256, | |
129 | pg_placement_num_target: 256, | |
130 | pool: 2, | |
131 | pool_name: 'somePool', | |
132 | type: 'replicated', | |
133 | size: 3 | |
134 | } | |
135 | }, | |
136 | Mocks.getPool('somePool', 2) | |
137 | ); | |
138 | }); | |
139 | ||
140 | it('should not update shown data if no detail has changed on pool refresh', () => { | |
141 | expectedChange( | |
142 | { | |
143 | poolDetails: { | |
144 | pool: 0, | |
145 | pool_name: 'micro_pool', | |
146 | tiers: [0] | |
147 | } | |
148 | }, | |
149 | poolDetailsComponent.selection, | |
150 | false | |
151 | ); | |
152 | }); | |
153 | ||
154 | it('should show "Cache Tiers Details" tab if selected pool has "tiers"', () => { | |
155 | const tabsItem = TabHelper.getNgbNavItems(fixture); | |
156 | const tabsText = TabHelper.getTextContents(fixture); | |
157 | expect(poolDetailsComponent.selection['tiers'].length).toBe(1); | |
158 | expect(tabsItem.length).toBe(3); | |
159 | expect(tabsText[2]).toBe('Cache Tiers Details'); | |
160 | expect(tabsItem[0].active).toBeTruthy(); | |
161 | }); | |
162 | ||
163 | it('should not show "Cache Tiers Details" tab if selected pool has no "tiers"', () => { | |
164 | updatePoolSelection({ tiers: [] }); | |
165 | const tabs = TabHelper.getNgbNavItems(fixture); | |
166 | expect(tabs.length).toEqual(2); | |
167 | expect(tabs[0].active).toBeTruthy(); | |
168 | }); | |
eafe8130 | 169 | }); |
11fdf7f2 TL |
170 | }); |
171 | }); |