]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
2d4736dd71ab7226e86008d00a82f117c69b8255
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { ModalModule } from 'ngx-bootstrap/modal';
7 import { TabsModule } from 'ngx-bootstrap/tabs';
8
9 import {
10   configureTestBed,
11   i18nProviders,
12   PermissionHelper
13 } from '../../../../testing/unit-test-helper';
14 import { ActionLabels } from '../../../shared/constants/app.constants';
15 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
16 import { SharedModule } from '../../../shared/shared.module';
17 import { RgwBucketDetailsComponent } from '../rgw-bucket-details/rgw-bucket-details.component';
18 import { RgwBucketListComponent } from './rgw-bucket-list.component';
19
20 describe('RgwBucketListComponent', () => {
21   let component: RgwBucketListComponent;
22   let fixture: ComponentFixture<RgwBucketListComponent>;
23
24   configureTestBed({
25     declarations: [RgwBucketListComponent, RgwBucketDetailsComponent],
26     imports: [
27       RouterTestingModule,
28       ModalModule.forRoot(),
29       SharedModule,
30       TabsModule.forRoot(),
31       HttpClientTestingModule
32     ],
33     providers: i18nProviders
34   });
35
36   beforeEach(() => {
37     fixture = TestBed.createComponent(RgwBucketListComponent);
38     component = fixture.componentInstance;
39   });
40
41   it('should create', () => {
42     fixture.detectChanges();
43     expect(component).toBeTruthy();
44   });
45
46   describe('show action buttons and drop down actions depending on permissions', () => {
47     let tableActions: TableActionsComponent;
48     let scenario: { fn; empty; single };
49     let permissionHelper: PermissionHelper;
50
51     const getTableActionComponent = (): TableActionsComponent => {
52       fixture.detectChanges();
53       return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance;
54     };
55
56     beforeEach(() => {
57       permissionHelper = new PermissionHelper(component.permission, () =>
58         getTableActionComponent()
59       );
60       scenario = {
61         fn: () => tableActions.getCurrentButton().name,
62         single: ActionLabels.EDIT,
63         empty: ActionLabels.CREATE
64       };
65     });
66
67     describe('with all', () => {
68       beforeEach(() => {
69         tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1);
70       });
71
72       it(`shows 'Edit' for single selection else 'Add' as main action`, () =>
73         permissionHelper.testScenarios(scenario));
74
75       it('shows all actions', () => {
76         expect(tableActions.tableActions.length).toBe(3);
77         expect(tableActions.tableActions).toEqual(component.tableActions);
78       });
79     });
80
81     describe('with read, create and update', () => {
82       beforeEach(() => {
83         tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0);
84       });
85
86       it(`shows 'Edit' for single selection else 'Add' as main action`, () =>
87         permissionHelper.testScenarios(scenario));
88
89       it(`shows 'Add' and 'Edit' action`, () => {
90         expect(tableActions.tableActions.length).toBe(2);
91         component.tableActions.pop();
92         expect(tableActions.tableActions).toEqual(component.tableActions);
93       });
94     });
95
96     describe('with read, create and delete', () => {
97       beforeEach(() => {
98         tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1);
99       });
100
101       it(`shows 'Delete' for single selection else 'Add' as main action`, () => {
102         scenario.single = 'Delete';
103         permissionHelper.testScenarios(scenario);
104       });
105
106       it(`shows 'Add' and 'Delete' action`, () => {
107         expect(tableActions.tableActions.length).toBe(2);
108         expect(tableActions.tableActions).toEqual([
109           component.tableActions[0],
110           component.tableActions[2]
111         ]);
112       });
113     });
114
115     describe('with read, edit and delete', () => {
116       beforeEach(() => {
117         tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1);
118       });
119
120       it(`shows always 'Edit' as main action`, () => {
121         scenario.empty = ActionLabels.EDIT;
122         permissionHelper.testScenarios(scenario);
123       });
124
125       it(`shows 'Edit' and 'Delete' action`, () => {
126         expect(tableActions.tableActions.length).toBe(2);
127         expect(tableActions.tableActions).toEqual([
128           component.tableActions[1],
129           component.tableActions[2]
130         ]);
131       });
132     });
133
134     describe('with read and create', () => {
135       beforeEach(() => {
136         tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0);
137       });
138
139       it(`shows always 'Add' as main action`, () => {
140         scenario.single = ActionLabels.CREATE;
141         permissionHelper.testScenarios(scenario);
142       });
143
144       it(`shows only 'Add' action`, () => {
145         expect(tableActions.tableActions.length).toBe(1);
146         expect(tableActions.tableActions).toEqual([component.tableActions[0]]);
147       });
148     });
149
150     describe('with read and update', () => {
151       beforeEach(() => {
152         tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0);
153       });
154
155       it(`shows always 'Edit' as main action`, () => {
156         scenario.empty = ActionLabels.EDIT;
157         permissionHelper.testScenarios(scenario);
158       });
159
160       it(`shows only 'Edit' action`, () => {
161         expect(tableActions.tableActions.length).toBe(1);
162         expect(tableActions.tableActions).toEqual([component.tableActions[1]]);
163       });
164     });
165
166     describe('with read and delete', () => {
167       beforeEach(() => {
168         tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1);
169       });
170
171       it(`shows always 'Delete' as main action`, () => {
172         scenario.single = 'Delete';
173         scenario.empty = 'Delete';
174         permissionHelper.testScenarios(scenario);
175       });
176
177       it(`shows only 'Delete' action`, () => {
178         expect(tableActions.tableActions.length).toBe(1);
179         expect(tableActions.tableActions).toEqual([component.tableActions[2]]);
180       });
181     });
182
183     describe('with only read', () => {
184       beforeEach(() => {
185         tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0);
186       });
187
188       it('shows no main action', () => {
189         permissionHelper.testScenarios({
190           fn: () => tableActions.getCurrentButton(),
191           single: undefined,
192           empty: undefined
193         });
194       });
195
196       it('shows no actions', () => {
197         expect(tableActions.tableActions.length).toBe(0);
198         expect(tableActions.tableActions).toEqual([]);
199       });
200     });
201   });
202 });