1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2 import { RouterTestingModule } from '@angular/router/testing';
4 import { NgxPipeFunctionModule } from 'ngx-pipe-function';
6 import { ComponentsModule } from '~/app/shared/components/components.module';
7 import { CdTableAction } from '~/app/shared/models/cd-table-action';
8 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
9 import { Permission } from '~/app/shared/models/permissions';
10 import { configureTestBed, PermissionHelper } from '~/testing/unit-test-helper';
11 import { TableActionsComponent } from './table-actions.component';
13 describe('TableActionsComponent', () => {
14 let component: TableActionsComponent;
15 let fixture: ComponentFixture<TableActionsComponent>;
16 let addAction: CdTableAction;
17 let editAction: CdTableAction;
18 let protectAction: CdTableAction;
19 let unprotectAction: CdTableAction;
20 let deleteAction: CdTableAction;
21 let copyAction: CdTableAction;
22 let permissionHelper: PermissionHelper;
25 declarations: [TableActionsComponent],
26 imports: [ComponentsModule, NgxPipeFunctionModule, RouterTestingModule]
33 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection,
44 canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
45 disable: (selection: CdTableSelection) =>
46 !selection.hasSingleSelection || selection.first().cdExecuting,
52 canBePrimary: (selection: CdTableSelection) => selection.hasSelection,
53 disable: (selection: CdTableSelection) =>
54 !selection.hasSelection || selection.first().cdExecuting,
60 canBePrimary: () => false,
61 visible: (selection: CdTableSelection) => selection.hasSingleSelection,
67 canBePrimary: () => false,
68 visible: (selection: CdTableSelection) => !selection.hasSingleSelection,
71 fixture = TestBed.createComponent(TableActionsComponent);
72 component = fixture.componentInstance;
73 component.selection = new CdTableSelection();
74 component.permission = new Permission();
75 component.permission.read = true;
76 component.tableActions = [
84 permissionHelper = new PermissionHelper(component.permission);
85 permissionHelper.setPermissionsAndGetActions(component.tableActions);
88 it('should create', () => {
89 expect(component).toBeTruthy();
92 it('should call ngInit without permissions', () => {
93 component.permission = undefined;
95 expect(component.tableActions).toEqual([]);
96 expect(component.dropDownActions).toEqual([]);
99 describe('useRouterLink', () => {
100 const testLink = '/api/some/link';
101 it('should use a link generated from a function', () => {
102 addAction.routerLink = () => testLink;
103 expect(component.useRouterLink(addAction)).toBe(testLink);
106 it('should use the link as it is because it is a string', () => {
107 addAction.routerLink = testLink;
108 expect(component.useRouterLink(addAction)).toBe(testLink);
111 it('should not return anything because no link is defined', () => {
112 expect(component.useRouterLink(addAction)).toBe(undefined);
115 it('should not return anything because the action is disabled', () => {
116 editAction.routerLink = testLink;
117 expect(component.useRouterLink(editAction)).toBe(undefined);
121 it('should test all TableActions combinations', () => {
122 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
123 component.tableActions
125 expect(tableActions).toEqual({
126 'create,update,delete': {
127 actions: ['Add', 'Edit', 'Protect', 'Unprotect', 'Copy', 'Delete'],
128 primary: { multiple: 'Delete', executing: 'Edit', single: 'Edit', no: 'Add' }
131 actions: ['Add', 'Edit', 'Protect', 'Unprotect', 'Copy'],
132 primary: { multiple: 'Add', executing: 'Edit', single: 'Edit', no: 'Add' }
135 actions: ['Add', 'Copy', 'Delete'],
136 primary: { multiple: 'Delete', executing: 'Copy', single: 'Copy', no: 'Add' }
139 actions: ['Add', 'Copy'],
140 primary: { multiple: 'Add', executing: 'Copy', single: 'Copy', no: 'Add' }
143 actions: ['Edit', 'Protect', 'Unprotect', 'Delete'],
144 primary: { multiple: 'Delete', executing: 'Edit', single: 'Edit', no: 'Edit' }
147 actions: ['Edit', 'Protect', 'Unprotect'],
148 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
152 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
156 primary: { multiple: '', executing: '', single: '', no: '' }
161 it('should convert any name to a proper CSS class', () => {
162 expect(component.toClassName({ name: 'Create' } as CdTableAction)).toBe('create');
163 expect(component.toClassName({ name: 'Mark x down' } as CdTableAction)).toBe('mark-x-down');
164 expect(component.toClassName({ name: '?Su*per!' } as CdTableAction)).toBe('super');
167 describe('useDisableDesc', () => {
168 it('should return a description if disable method returns a string', () => {
169 const deleteWithDescAction: CdTableAction = {
170 permission: 'delete',
172 canBePrimary: (selection: CdTableSelection) => selection.hasSelection,
174 return 'Delete action disabled description';
179 expect(component.useDisableDesc(deleteWithDescAction)).toBe(
180 'Delete action disabled description'
184 it('should return no description if disable does not return string', () => {
185 expect(component.useDisableDesc(deleteAction)).toBeUndefined();
189 describe('useClickAction', () => {
190 const editClickAction: CdTableAction = {
191 permission: 'update',
195 return 'Edit action click';
199 it('should call click action if action is not disabled', () => {
200 editClickAction.disable = () => {
203 expect(component.useClickAction(editClickAction)).toBe('Edit action click');
206 it('should not call click action if action is disabled', () => {
207 editClickAction.disable = () => {
210 expect(component.useClickAction(editClickAction)).toBeFalsy();