1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2 import { RouterTestingModule } from '@angular/router/testing';
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';
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;
21 let permissionHelper: PermissionHelper;
23 const setUpTableActions = () => {
24 component.tableActions = [
34 const getTableActionComponent = (): TableActionsComponent => {
41 declarations: [TableActionsComponent],
42 imports: [ComponentsModule, RouterTestingModule]
49 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection,
60 canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
61 disable: (selection: CdTableSelection) =>
62 !selection.hasSingleSelection || selection.first().cdExecuting,
68 canBePrimary: (selection: CdTableSelection) => selection.hasSelection,
69 disable: (selection: CdTableSelection) =>
70 !selection.hasSelection || selection.first().cdExecuting,
76 canBePrimary: () => false,
77 visible: (selection: CdTableSelection) => selection.hasSingleSelection,
83 canBePrimary: () => false,
84 visible: (selection: CdTableSelection) => !selection.hasSingleSelection,
87 fixture = TestBed.createComponent(TableActionsComponent);
88 component = fixture.componentInstance;
89 component.selection = new CdTableSelection();
90 component.permission = new Permission();
91 component.permission.read = true;
92 permissionHelper = new PermissionHelper(component.permission, () => getTableActionComponent());
93 permissionHelper.setPermissionsAndGetActions(1, 1, 1);
96 it('should create', () => {
97 expect(component).toBeTruthy();
100 it('should ngInit should be called with no permissions', () => {
101 component.permission = undefined;
102 component.ngOnInit();
103 expect(component.tableActions).toEqual([]);
104 expect(component.dropDownActions).toEqual([]);
107 describe('useRouterLink', () => {
108 const testLink = '/api/some/link';
109 it('should use a link generated from a function', () => {
110 addAction.routerLink = () => testLink;
111 expect(component.useRouterLink(addAction)).toBe(testLink);
114 it('should use the link as it is because it is a string', () => {
115 addAction.routerLink = testLink;
116 expect(component.useRouterLink(addAction)).toBe(testLink);
119 it('should not return anything because no link is defined', () => {
120 expect(component.useRouterLink(addAction)).toBe(undefined);
123 it('should not return anything because the action is disabled', () => {
124 editAction.routerLink = testLink;
125 expect(component.useRouterLink(editAction)).toBe(undefined);
129 describe('disableSelectionAction', () => {
134 singleExecuting: false,
140 it('tests disabling addAction', () => {
141 scenario.fn = () => component.disableSelectionAction(addAction);
142 permissionHelper.testScenarios(scenario);
145 it('tests disabling editAction', () => {
146 scenario.fn = () => component.disableSelectionAction(editAction);
147 scenario.multiple = true;
148 scenario.empty = true;
149 scenario.singleExecuting = true;
150 permissionHelper.testScenarios(scenario);
153 it('tests disabling deleteAction', () => {
154 scenario.fn = () => component.disableSelectionAction(deleteAction);
155 scenario.multiple = false;
156 scenario.empty = true;
157 scenario.singleExecuting = true;
158 permissionHelper.testScenarios(scenario);
161 it('tests disabling copyAction', () => {
162 scenario.fn = () => component.disableSelectionAction(copyAction);
163 scenario.multiple = true;
164 scenario.empty = true;
165 scenario.singleExecuting = true;
166 permissionHelper.testScenarios(scenario);
170 describe('get current button', () => {
171 const hiddenScenario = () => {
172 scenario.multiple = undefined;
173 scenario.empty = undefined;
174 scenario.singleExecuting = undefined;
175 scenario.single = undefined;
178 const setScenario = (defaultAction, selectionAction) => {
179 scenario.single = selectionAction;
180 scenario.singleExecuting = selectionAction;
181 scenario.multiple = defaultAction;
182 scenario.empty = defaultAction;
187 fn: () => component.getCurrentButton(),
188 singleExecuting: copyAction,
194 it('gets add for no, edit for single and delete for multiple selections', () => {
195 setScenario(addAction, editAction);
196 scenario.multiple = deleteAction;
197 permissionHelper.setPermissionsAndGetActions(1, 1, 1);
198 permissionHelper.testScenarios(scenario);
201 it('gets add action except for selections where it shows edit action', () => {
202 setScenario(addAction, editAction);
203 permissionHelper.setPermissionsAndGetActions(1, 1, 0);
204 permissionHelper.testScenarios(scenario);
207 it('gets add for no, copy for single and delete for multiple selections', () => {
208 setScenario(addAction, copyAction);
209 scenario.multiple = deleteAction;
210 permissionHelper.setPermissionsAndGetActions(1, 0, 1);
211 permissionHelper.testScenarios(scenario);
214 it('gets add action except for selections where it shows copy action', () => {
215 setScenario(addAction, copyAction);
216 permissionHelper.setPermissionsAndGetActions(1, 0, 0);
217 permissionHelper.testScenarios(scenario);
220 it('should always get edit action except delete for multiple items', () => {
221 setScenario(editAction, editAction);
222 scenario.multiple = deleteAction;
223 permissionHelper.setPermissionsAndGetActions(0, 1, 1);
224 permissionHelper.testScenarios(scenario);
227 it('should always get edit action', () => {
228 setScenario(editAction, editAction);
229 permissionHelper.setPermissionsAndGetActions(0, 1, 0);
230 permissionHelper.testScenarios(scenario);
233 it('should always get delete action', () => {
234 setScenario(deleteAction, deleteAction);
235 permissionHelper.setPermissionsAndGetActions(0, 0, 1);
236 permissionHelper.testScenarios(scenario);
239 it('should not get any button with no permissions', () => {
241 permissionHelper.setPermissionsAndGetActions(0, 0, 0);
242 permissionHelper.testScenarios(scenario);
245 it('should not get any button if only a drop down should be shown', () => {
247 component.onlyDropDown = 'Drop down label';
248 permissionHelper.setPermissionsAndGetActions(1, 1, 1);
249 permissionHelper.testScenarios(scenario);
253 describe('show drop down', () => {
254 const testShowDropDownActions = (perms, expected) => {
255 permissionHelper.setPermissionsAndGetActions(perms[0], perms[1], perms[2]);
256 expect(`${perms} ${component.showDropDownActions()}`).toBe(`${perms} ${expected}`);
259 it('is shown if multiple items are found depending on the permissions', () => {
260 [[1, 0, 0], [1, 1, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [0, 1, 0]].forEach((perms) => {
261 testShowDropDownActions(perms, true);
265 it('is not shown if only 1 or less items are found depending on the permissions', () => {
266 [[0, 0, 1], [0, 0, 0]].forEach((perms) => {
267 testShowDropDownActions(perms, false);
272 describe('with drop down only', () => {
274 component.onlyDropDown = 'displayMe';
277 it('should not return any button with getCurrentButton', () => {
278 expect(component.getCurrentButton()).toBeFalsy();
282 it('should convert any name to a proper CSS class', () => {
283 expect(component.toClassName('Create')).toBe('create');
284 expect(component.toClassName('Mark x down')).toBe('mark-x-down');
285 expect(component.toClassName('?Su*per!')).toBe('super');
288 describe('useDisableDesc', () => {
289 it('should return a description if disableDesc is set for action', () => {
290 const deleteWithDescAction: CdTableAction = {
291 permission: 'delete',
293 canBePrimary: (selection: CdTableSelection) => selection.hasSelection,
295 return 'Delete action disabled description';
300 expect(component.useDisableDesc(deleteWithDescAction)).toBe(
301 'Delete action disabled description'
305 it('should return no description if disableDesc is not set for action', () => {
306 expect(component.useDisableDesc(deleteAction)).toBeUndefined();
310 describe('useClickAction', () => {
311 const editClickAction: CdTableAction = {
312 permission: 'update',
316 return 'Edit action click';
320 it('should call click action if action is not disabled', () => {
321 editClickAction.disable = () => {
324 expect(component.useClickAction(editClickAction)).toBe('Edit action click');
327 it('should not call click action if action is disabled', () => {
328 editClickAction.disable = () => {
331 expect(component.useClickAction(editClickAction)).toBeFalsy();