]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
2 | import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
3 | import { ReactiveFormsModule } from '@angular/forms'; | |
494da23a | 4 | import { ActivatedRoute, Router } from '@angular/router'; |
11fdf7f2 TL |
5 | import { RouterTestingModule } from '@angular/router/testing'; |
6 | import { TooltipModule } from 'ngx-bootstrap/tooltip'; | |
7 | ||
494da23a | 8 | import { ToastrModule } from 'ngx-toastr'; |
11fdf7f2 TL |
9 | |
10 | import { By } from '@angular/platform-browser'; | |
494da23a | 11 | import { of } from 'rxjs'; |
11fdf7f2 TL |
12 | import { ActivatedRouteStub } from '../../../../testing/activated-route-stub'; |
13 | import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper'; | |
14 | import { RbdService } from '../../../shared/api/rbd.service'; | |
15 | import { SharedModule } from '../../../shared/shared.module'; | |
16 | import { RbdConfigurationFormComponent } from '../rbd-configuration-form/rbd-configuration-form.component'; | |
17 | import { RbdFormMode } from './rbd-form-mode.enum'; | |
18 | import { RbdFormComponent } from './rbd-form.component'; | |
19 | ||
20 | describe('RbdFormComponent', () => { | |
21 | let component: RbdFormComponent; | |
22 | let fixture: ComponentFixture<RbdFormComponent>; | |
23 | let activatedRoute: ActivatedRouteStub; | |
24 | ||
494da23a TL |
25 | const queryNativeElement = (cssSelector) => |
26 | fixture.debugElement.query(By.css(cssSelector)).nativeElement; | |
27 | ||
11fdf7f2 TL |
28 | configureTestBed({ |
29 | imports: [ | |
30 | HttpClientTestingModule, | |
31 | ReactiveFormsModule, | |
32 | RouterTestingModule, | |
494da23a | 33 | ToastrModule.forRoot(), |
11fdf7f2 TL |
34 | SharedModule, |
35 | TooltipModule | |
36 | ], | |
37 | declarations: [RbdFormComponent, RbdConfigurationFormComponent], | |
38 | providers: [ | |
39 | { | |
40 | provide: ActivatedRoute, | |
41 | useValue: new ActivatedRouteStub({ pool: 'foo', name: 'bar', snap: undefined }) | |
42 | }, | |
494da23a TL |
43 | i18nProviders, |
44 | RbdService | |
11fdf7f2 TL |
45 | ] |
46 | }); | |
47 | ||
48 | beforeEach(() => { | |
49 | fixture = TestBed.createComponent(RbdFormComponent); | |
50 | component = fixture.componentInstance; | |
51 | activatedRoute = TestBed.get(ActivatedRoute); | |
52 | }); | |
53 | ||
54 | it('should create', () => { | |
55 | expect(component).toBeTruthy(); | |
56 | }); | |
57 | ||
58 | describe('should test decodeURIComponent of params', () => { | |
59 | let rbdService: RbdService; | |
60 | ||
61 | beforeEach(() => { | |
62 | rbdService = TestBed.get(RbdService); | |
63 | component.mode = RbdFormMode.editing; | |
64 | fixture.detectChanges(); | |
65 | spyOn(rbdService, 'get').and.callThrough(); | |
66 | }); | |
67 | ||
68 | it('without snapName', () => { | |
69 | activatedRoute.setParams({ pool: 'foo%2Ffoo', name: 'bar%2Fbar', snap: undefined }); | |
70 | ||
71 | expect(rbdService.get).toHaveBeenCalledWith('foo/foo', 'bar/bar'); | |
72 | expect(component.snapName).toBeUndefined(); | |
73 | }); | |
74 | ||
75 | it('with snapName', () => { | |
76 | activatedRoute.setParams({ pool: 'foo%2Ffoo', name: 'bar%2Fbar', snap: 'baz%2Fbaz' }); | |
77 | ||
78 | expect(rbdService.get).toHaveBeenCalledWith('foo/foo', 'bar/bar'); | |
79 | expect(component.snapName).toBe('baz/baz'); | |
80 | }); | |
81 | }); | |
82 | ||
83 | describe('test image configuration component', () => { | |
84 | it('is visible', () => { | |
85 | fixture.detectChanges(); | |
494da23a TL |
86 | expect(queryNativeElement('cd-rbd-configuration-form').parentElement.hidden).toBe(false); |
87 | }); | |
88 | }); | |
89 | ||
90 | describe('tests for feature flags', () => { | |
91 | let deepFlatten, layering, exclusiveLock, objectMap, journaling, fastDiff; | |
92 | const defaultFeatures = [ | |
93 | // Supposed to be enabled by default | |
94 | 'deep-flatten', | |
95 | 'exclusive-lock', | |
96 | 'fast-diff', | |
97 | 'layering', | |
98 | 'object-map' | |
99 | ]; | |
100 | const allFeatureNames = [ | |
101 | 'deep-flatten', | |
102 | 'layering', | |
103 | 'exclusive-lock', | |
104 | 'object-map', | |
105 | 'journaling', | |
106 | 'fast-diff' | |
107 | ]; | |
108 | const setFeatures = (features) => { | |
109 | component.features = features; | |
110 | component.featuresList = component.objToArray(features); | |
111 | component.createForm(); | |
112 | }; | |
113 | const getFeatureNativeElements = () => allFeatureNames.map((f) => queryNativeElement(`#${f}`)); | |
114 | ||
115 | it('should convert feature flags correctly in the constructor', () => { | |
116 | setFeatures({ | |
117 | one: { desc: 'one', allowEnable: true, allowDisable: true }, | |
118 | two: { desc: 'two', allowEnable: true, allowDisable: true }, | |
119 | three: { desc: 'three', allowEnable: true, allowDisable: true } | |
120 | }); | |
121 | expect(component.featuresList).toEqual([ | |
122 | { desc: 'one', key: 'one', allowDisable: true, allowEnable: true }, | |
123 | { desc: 'two', key: 'two', allowDisable: true, allowEnable: true }, | |
124 | { desc: 'three', key: 'three', allowDisable: true, allowEnable: true } | |
125 | ]); | |
126 | }); | |
127 | ||
128 | describe('test edit form flags', () => { | |
129 | const prepare = (pool: string, image: string, enabledFeatures: string[]): void => { | |
130 | const rbdService = TestBed.get(RbdService); | |
131 | spyOn(rbdService, 'get').and.returnValue( | |
132 | of({ | |
133 | name: image, | |
134 | pool_name: pool, | |
135 | features_name: enabledFeatures | |
136 | }) | |
137 | ); | |
138 | spyOn(rbdService, 'defaultFeatures').and.returnValue(of(defaultFeatures)); | |
139 | component.router = { url: `/block/rbd/edit/${pool}/${image}` } as Router; | |
140 | fixture.detectChanges(); | |
141 | [ | |
142 | deepFlatten, | |
143 | layering, | |
144 | exclusiveLock, | |
145 | objectMap, | |
146 | journaling, | |
147 | fastDiff | |
148 | ] = getFeatureNativeElements(); | |
149 | }; | |
150 | ||
151 | it('should have the interlock feature for flags disabled, if one feature is not set', () => { | |
152 | prepare('rbd', 'foobar', ['deep-flatten', 'exclusive-lock', 'layering', 'object-map']); | |
153 | ||
154 | expect(objectMap.disabled).toBe(false); | |
155 | expect(fastDiff.disabled).toBe(false); | |
156 | ||
157 | expect(objectMap.checked).toBe(true); | |
158 | expect(fastDiff.checked).toBe(false); | |
159 | ||
160 | fastDiff.click(); | |
161 | fastDiff.click(); | |
162 | ||
163 | expect(objectMap.checked).toBe(true); // Shall not be disabled by `fast-diff`! | |
164 | }); | |
165 | ||
166 | it('should not disable object-map when fast-diff is unchecked', () => { | |
167 | prepare('rbd', 'foobar', ['deep-flatten', 'exclusive-lock', 'layering', 'object-map']); | |
168 | ||
169 | fastDiff.click(); | |
170 | fastDiff.click(); | |
171 | ||
172 | expect(objectMap.checked).toBe(true); // Shall not be disabled by `fast-diff`! | |
173 | }); | |
174 | ||
175 | it('should not enable fast-diff when object-map is checked', () => { | |
176 | prepare('rbd', 'foobar', ['deep-flatten', 'exclusive-lock', 'layering', 'object-map']); | |
177 | ||
178 | objectMap.click(); | |
179 | objectMap.click(); | |
180 | ||
181 | expect(fastDiff.checked).toBe(false); // Shall not be disabled by `fast-diff`! | |
182 | }); | |
183 | }); | |
184 | ||
185 | describe('test create form flags', () => { | |
186 | beforeEach(() => { | |
187 | const rbdService = TestBed.get(RbdService); | |
188 | spyOn(rbdService, 'defaultFeatures').and.returnValue(of(defaultFeatures)); | |
189 | component.router = { url: '/block/rbd/create' } as Router; | |
190 | fixture.detectChanges(); | |
191 | [ | |
192 | deepFlatten, | |
193 | layering, | |
194 | exclusiveLock, | |
195 | objectMap, | |
196 | journaling, | |
197 | fastDiff | |
198 | ] = getFeatureNativeElements(); | |
199 | }); | |
200 | ||
201 | it('should initialize the checkboxes correctly', () => { | |
202 | expect(deepFlatten.disabled).toBe(false); | |
203 | expect(layering.disabled).toBe(false); | |
204 | expect(exclusiveLock.disabled).toBe(false); | |
205 | expect(objectMap.disabled).toBe(false); | |
206 | expect(journaling.disabled).toBe(false); | |
207 | expect(fastDiff.disabled).toBe(false); | |
208 | ||
209 | expect(deepFlatten.checked).toBe(true); | |
210 | expect(layering.checked).toBe(true); | |
211 | expect(exclusiveLock.checked).toBe(true); | |
212 | expect(objectMap.checked).toBe(true); | |
213 | expect(journaling.checked).toBe(false); | |
214 | expect(fastDiff.checked).toBe(true); | |
215 | }); | |
216 | ||
217 | it('should disable features if their requirements are not met (exclusive-lock)', () => { | |
218 | exclusiveLock.click(); // unchecks exclusive-lock | |
219 | expect(objectMap.disabled).toBe(true); | |
220 | expect(journaling.disabled).toBe(true); | |
221 | expect(fastDiff.disabled).toBe(true); | |
222 | }); | |
223 | ||
224 | it('should disable features if their requirements are not met (object-map)', () => { | |
225 | objectMap.click(); // unchecks object-map | |
226 | expect(fastDiff.disabled).toBe(true); | |
227 | }); | |
11fdf7f2 TL |
228 | }); |
229 | }); | |
230 | }); |