]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
1b53db811a9c27627c9bc3a857f364d25a3407a8
[ceph-ci.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 { TabsModule } from 'ngx-bootstrap/tabs';
7 import { ToastrModule } from 'ngx-toastr';
8
9 import {
10   configureTestBed,
11   i18nProviders,
12   PermissionHelper
13 } from '../../../../../testing/unit-test-helper';
14 import { TableActionsComponent } from '../../../../shared/datatable/table-actions/table-actions.component';
15 import { SharedModule } from '../../../../shared/shared.module';
16 import { PrometheusTabsComponent } from '../prometheus-tabs/prometheus-tabs.component';
17 import { AlertListComponent } from './alert-list.component';
18
19 describe('PrometheusListComponent', () => {
20   let component: AlertListComponent;
21   let fixture: ComponentFixture<AlertListComponent>;
22
23   configureTestBed({
24     imports: [
25       HttpClientTestingModule,
26       TabsModule.forRoot(),
27       RouterTestingModule,
28       ToastrModule.forRoot(),
29       SharedModule
30     ],
31     declarations: [AlertListComponent, PrometheusTabsComponent],
32     providers: [i18nProviders]
33   });
34
35   beforeEach(() => {
36     fixture = TestBed.createComponent(AlertListComponent);
37     component = fixture.componentInstance;
38   });
39
40   it('should create', () => {
41     fixture.detectChanges();
42     expect(component).toBeTruthy();
43   });
44
45   describe('show action buttons and drop down actions depending on permissions', () => {
46     let tableActions: TableActionsComponent;
47     let scenario: { fn; empty; single };
48     let permissionHelper: PermissionHelper;
49     let combinations: number[][];
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: 'Create silence',
63         empty: 'Create silence'
64       };
65       tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1);
66     });
67
68     const permissionSwitch = (combination) => {
69       tableActions = permissionHelper.setPermissionsAndGetActions(
70         combination[0],
71         combination[1],
72         combination[2]
73       );
74       tableActions.tableActions = component.tableActions;
75       tableActions.ngOnInit();
76     };
77
78     const testCombinations = (test: Function) => {
79       combinations.forEach((combination) => {
80         permissionSwitch(combination);
81         test();
82       });
83     };
84
85     describe('with every permission combination that includes create', () => {
86       beforeEach(() => {
87         combinations = [[1, 1, 1], [1, 1, 0], [1, 0, 1], [1, 0, 0]];
88       });
89
90       it(`always shows 'Create silence' as main action`, () => {
91         testCombinations(() => permissionHelper.testScenarios(scenario));
92       });
93
94       it('shows all actions', () => {
95         testCombinations(() => {
96           expect(tableActions.tableActions.length).toBe(1);
97           expect(tableActions.tableActions).toEqual(component.tableActions);
98         });
99       });
100     });
101
102     describe('with every permission combination that does not include create', () => {
103       beforeEach(() => {
104         combinations = [[0, 1, 1], [0, 1, 0], [0, 0, 1], [0, 0, 0]];
105       });
106
107       it(`won't show any action`, () => {
108         testCombinations(() => {
109           permissionHelper.testScenarios({
110             fn: () => tableActions.getCurrentButton(),
111             single: undefined,
112             empty: undefined
113           });
114         });
115       });
116
117       it('shows no actions', () => {
118         testCombinations(() => {
119           expect(tableActions.tableActions.length).toBe(0);
120           expect(tableActions.tableActions).toEqual([]);
121         });
122       });
123     });
124   });
125 });