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;
20 let permissionHelper: PermissionHelper;
23 declarations: [TableActionsComponent],
24 imports: [ComponentsModule, RouterTestingModule]
31 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection,
42 canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
43 disable: (selection: CdTableSelection) =>
44 !selection.hasSingleSelection || selection.first().cdExecuting,
50 canBePrimary: (selection: CdTableSelection) => selection.hasSelection,
51 disable: (selection: CdTableSelection) =>
52 !selection.hasSelection || selection.first().cdExecuting,
58 canBePrimary: () => false,
59 visible: (selection: CdTableSelection) => selection.hasSingleSelection,
65 canBePrimary: () => false,
66 visible: (selection: CdTableSelection) => !selection.hasSingleSelection,
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 = [
82 permissionHelper = new PermissionHelper(component.permission);
83 permissionHelper.setPermissionsAndGetActions(component.tableActions);
86 it('should create', () => {
87 expect(component).toBeTruthy();
90 it('should call ngInit without permissions', () => {
91 component.permission = undefined;
93 expect(component.tableActions).toEqual([]);
94 expect(component.dropDownActions).toEqual([]);
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);
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);
109 it('should not return anything because no link is defined', () => {
110 expect(component.useRouterLink(addAction)).toBe(undefined);
113 it('should not return anything because the action is disabled', () => {
114 editAction.routerLink = testLink;
115 expect(component.useRouterLink(editAction)).toBe(undefined);
119 it('should test all TableActions combinations', () => {
120 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
121 component.tableActions
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' }
129 actions: ['Add', 'Edit', 'Protect', 'Unprotect', 'Copy'],
130 primary: { multiple: 'Add', executing: 'Edit', single: 'Edit', no: 'Add' }
133 actions: ['Add', 'Copy', 'Delete'],
134 primary: { multiple: 'Delete', executing: 'Copy', single: 'Copy', no: 'Add' }
137 actions: ['Add', 'Copy'],
138 primary: { multiple: 'Add', executing: 'Copy', single: 'Copy', no: 'Add' }
141 actions: ['Edit', 'Protect', 'Unprotect', 'Delete'],
142 primary: { multiple: 'Delete', executing: 'Edit', single: 'Edit', no: 'Edit' }
145 actions: ['Edit', 'Protect', 'Unprotect'],
146 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
150 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
154 primary: { multiple: '', executing: '', single: '', no: '' }
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');
165 describe('useDisableDesc', () => {
166 it('should return a description if disable method returns a string', () => {
167 const deleteWithDescAction: CdTableAction = {
168 permission: 'delete',
170 canBePrimary: (selection: CdTableSelection) => selection.hasSelection,
172 return 'Delete action disabled description';
177 expect(component.useDisableDesc(deleteWithDescAction)).toBe(
178 'Delete action disabled description'
182 it('should return no description if disable does not return string', () => {
183 expect(component.useDisableDesc(deleteAction)).toBeUndefined();
187 describe('useClickAction', () => {
188 const editClickAction: CdTableAction = {
189 permission: 'update',
193 return 'Edit action click';
197 it('should call click action if action is not disabled', () => {
198 editClickAction.disable = () => {
201 expect(component.useClickAction(editClickAction)).toBe('Edit action click');
204 it('should not call click action if action is disabled', () => {
205 editClickAction.disable = () => {
208 expect(component.useClickAction(editClickAction)).toBeFalsy();