]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-actions/table-actions.component.spec.ts
Import ceph 15.2.8
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / datatable / table-actions / table-actions.component.spec.ts
1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2 import { RouterTestingModule } from '@angular/router/testing';
3
4 import { configureTestBed, PermissionHelper } from '../../../../testing/unit-test-helper';
5 import { ComponentsModule } from '../../components/components.module';
6 import { CdTableAction } from '../../models/cd-table-action';
7 import { CdTableSelection } from '../../models/cd-table-selection';
8 import { Permission } from '../../models/permissions';
9 import { TableActionsComponent } from './table-actions.component';
10
11 describe('TableActionsComponent', () => {
12 let component: TableActionsComponent;
13 let fixture: ComponentFixture<TableActionsComponent>;
14 let addAction: CdTableAction;
15 let editAction: CdTableAction;
16 let protectAction: CdTableAction;
17 let unprotectAction: CdTableAction;
18 let deleteAction: CdTableAction;
19 let copyAction: CdTableAction;
20 let permissionHelper: PermissionHelper;
21
22 configureTestBed({
23 declarations: [TableActionsComponent],
24 imports: [ComponentsModule, RouterTestingModule]
25 });
26
27 beforeEach(() => {
28 addAction = {
29 permission: 'create',
30 icon: 'fa-plus',
31 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection,
32 name: 'Add'
33 };
34 editAction = {
35 permission: 'update',
36 icon: 'fa-pencil',
37 name: 'Edit'
38 };
39 copyAction = {
40 permission: 'create',
41 icon: 'fa-copy',
42 canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
43 disable: (selection: CdTableSelection) =>
44 !selection.hasSingleSelection || selection.first().cdExecuting,
45 name: 'Copy'
46 };
47 deleteAction = {
48 permission: 'delete',
49 icon: 'fa-times',
50 canBePrimary: (selection: CdTableSelection) => selection.hasSelection,
51 disable: (selection: CdTableSelection) =>
52 !selection.hasSelection || selection.first().cdExecuting,
53 name: 'Delete'
54 };
55 protectAction = {
56 permission: 'update',
57 icon: 'fa-lock',
58 canBePrimary: () => false,
59 visible: (selection: CdTableSelection) => selection.hasSingleSelection,
60 name: 'Protect'
61 };
62 unprotectAction = {
63 permission: 'update',
64 icon: 'fa-unlock',
65 canBePrimary: () => false,
66 visible: (selection: CdTableSelection) => !selection.hasSingleSelection,
67 name: 'Unprotect'
68 };
69 fixture = TestBed.createComponent(TableActionsComponent);
70 component = fixture.componentInstance;
71 component.selection = new CdTableSelection();
72 component.permission = new Permission();
73 component.permission.read = true;
74 component.tableActions = [
75 addAction,
76 editAction,
77 protectAction,
78 unprotectAction,
79 copyAction,
80 deleteAction
81 ];
82 permissionHelper = new PermissionHelper(component.permission);
83 permissionHelper.setPermissionsAndGetActions(component.tableActions);
84 });
85
86 it('should create', () => {
87 expect(component).toBeTruthy();
88 });
89
90 it('should call ngInit without permissions', () => {
91 component.permission = undefined;
92 component.ngOnInit();
93 expect(component.tableActions).toEqual([]);
94 expect(component.dropDownActions).toEqual([]);
95 });
96
97 describe('useRouterLink', () => {
98 const testLink = '/api/some/link';
99 it('should use a link generated from a function', () => {
100 addAction.routerLink = () => testLink;
101 expect(component.useRouterLink(addAction)).toBe(testLink);
102 });
103
104 it('should use the link as it is because it is a string', () => {
105 addAction.routerLink = testLink;
106 expect(component.useRouterLink(addAction)).toBe(testLink);
107 });
108
109 it('should not return anything because no link is defined', () => {
110 expect(component.useRouterLink(addAction)).toBe(undefined);
111 });
112
113 it('should not return anything because the action is disabled', () => {
114 editAction.routerLink = testLink;
115 expect(component.useRouterLink(editAction)).toBe(undefined);
116 });
117 });
118
119 it('should test all TableActions combinations', () => {
120 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
121 component.tableActions
122 );
123 expect(tableActions).toEqual({
124 'create,update,delete': {
125 actions: ['Add', 'Edit', 'Protect', 'Unprotect', 'Copy', 'Delete'],
126 primary: { multiple: 'Delete', executing: 'Edit', single: 'Edit', no: 'Add' }
127 },
128 'create,update': {
129 actions: ['Add', 'Edit', 'Protect', 'Unprotect', 'Copy'],
130 primary: { multiple: 'Add', executing: 'Edit', single: 'Edit', no: 'Add' }
131 },
132 'create,delete': {
133 actions: ['Add', 'Copy', 'Delete'],
134 primary: { multiple: 'Delete', executing: 'Copy', single: 'Copy', no: 'Add' }
135 },
136 create: {
137 actions: ['Add', 'Copy'],
138 primary: { multiple: 'Add', executing: 'Copy', single: 'Copy', no: 'Add' }
139 },
140 'update,delete': {
141 actions: ['Edit', 'Protect', 'Unprotect', 'Delete'],
142 primary: { multiple: 'Delete', executing: 'Edit', single: 'Edit', no: 'Edit' }
143 },
144 update: {
145 actions: ['Edit', 'Protect', 'Unprotect'],
146 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
147 },
148 delete: {
149 actions: ['Delete'],
150 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
151 },
152 'no-permissions': {
153 actions: [],
154 primary: { multiple: '', executing: '', single: '', no: '' }
155 }
156 });
157 });
158
159 it('should convert any name to a proper CSS class', () => {
160 expect(component.toClassName('Create')).toBe('create');
161 expect(component.toClassName('Mark x down')).toBe('mark-x-down');
162 expect(component.toClassName('?Su*per!')).toBe('super');
163 });
164
165 describe('useDisableDesc', () => {
166 it('should return a description if disable method returns a string', () => {
167 const deleteWithDescAction: CdTableAction = {
168 permission: 'delete',
169 icon: 'fa-times',
170 canBePrimary: (selection: CdTableSelection) => selection.hasSelection,
171 disable: () => {
172 return 'Delete action disabled description';
173 },
174 name: 'DeleteDesc'
175 };
176
177 expect(component.useDisableDesc(deleteWithDescAction)).toBe(
178 'Delete action disabled description'
179 );
180 });
181
182 it('should return no description if disable does not return string', () => {
183 expect(component.useDisableDesc(deleteAction)).toBeUndefined();
184 });
185 });
186
187 describe('useClickAction', () => {
188 const editClickAction: CdTableAction = {
189 permission: 'update',
190 icon: 'fa-pencil',
191 name: 'Edit',
192 click: () => {
193 return 'Edit action click';
194 }
195 };
196
197 it('should call click action if action is not disabled', () => {
198 editClickAction.disable = () => {
199 return false;
200 };
201 expect(component.useClickAction(editClickAction)).toBe('Edit action click');
202 });
203
204 it('should not call click action if action is disabled', () => {
205 editClickAction.disable = () => {
206 return true;
207 };
208 expect(component.useClickAction(editClickAction)).toBeFalsy();
209 });
210 });
211 });