summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-actions/table-actions.component.spec.ts
blob: 81cc1b9720793b431d442f9b7616ef06f3d95e3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { NgxPipeFunctionModule } from 'ngx-pipe-function';

import { ComponentsModule } from '~/app/shared/components/components.module';
import { CdTableAction } from '~/app/shared/models/cd-table-action';
import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
import { Permission } from '~/app/shared/models/permissions';
import { configureTestBed, PermissionHelper } from '~/testing/unit-test-helper';
import { TableActionsComponent } from './table-actions.component';

describe('TableActionsComponent', () => {
  let component: TableActionsComponent;
  let fixture: ComponentFixture<TableActionsComponent>;
  let addAction: CdTableAction;
  let editAction: CdTableAction;
  let protectAction: CdTableAction;
  let unprotectAction: CdTableAction;
  let deleteAction: CdTableAction;
  let copyAction: CdTableAction;
  let permissionHelper: PermissionHelper;

  configureTestBed({
    declarations: [TableActionsComponent],
    imports: [ComponentsModule, NgxPipeFunctionModule, RouterTestingModule]
  });

  beforeEach(() => {
    addAction = {
      permission: 'create',
      icon: 'fa-plus',
      canBePrimary: (selection: CdTableSelection) => !selection.hasSelection,
      name: 'Add'
    };
    editAction = {
      permission: 'update',
      icon: 'fa-pencil',
      name: 'Edit'
    };
    copyAction = {
      permission: 'create',
      icon: 'fa-copy',
      canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
      disable: (selection: CdTableSelection) =>
        !selection.hasSingleSelection || selection.first().cdExecuting,
      name: 'Copy'
    };
    deleteAction = {
      permission: 'delete',
      icon: 'fa-times',
      canBePrimary: (selection: CdTableSelection) => selection.hasSelection,
      disable: (selection: CdTableSelection) =>
        !selection.hasSelection || selection.first().cdExecuting,
      name: 'Delete'
    };
    protectAction = {
      permission: 'update',
      icon: 'fa-lock',
      canBePrimary: () => false,
      visible: (selection: CdTableSelection) => selection.hasSingleSelection,
      name: 'Protect'
    };
    unprotectAction = {
      permission: 'update',
      icon: 'fa-unlock',
      canBePrimary: () => false,
      visible: (selection: CdTableSelection) => !selection.hasSingleSelection,
      name: 'Unprotect'
    };
    fixture = TestBed.createComponent(TableActionsComponent);
    component = fixture.componentInstance;
    component.selection = new CdTableSelection();
    component.permission = new Permission();
    component.permission.read = true;
    component.tableActions = [
      addAction,
      editAction,
      protectAction,
      unprotectAction,
      copyAction,
      deleteAction
    ];
    permissionHelper = new PermissionHelper(component.permission);
    permissionHelper.setPermissionsAndGetActions(component.tableActions);
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should call ngInit without permissions', () => {
    component.permission = undefined;
    component.ngOnInit();
    expect(component.tableActions).toEqual([]);
    expect(component.dropDownActions).toEqual([]);
  });

  describe('useRouterLink', () => {
    const testLink = '/api/some/link';
    it('should use a link generated from a function', () => {
      addAction.routerLink = () => testLink;
      expect(component.useRouterLink(addAction)).toBe(testLink);
    });

    it('should use the link as it is because it is a string', () => {
      addAction.routerLink = testLink;
      expect(component.useRouterLink(addAction)).toBe(testLink);
    });

    it('should not return anything because no link is defined', () => {
      expect(component.useRouterLink(addAction)).toBe(undefined);
    });

    it('should not return anything because the action is disabled', () => {
      editAction.routerLink = testLink;
      expect(component.useRouterLink(editAction)).toBe(undefined);
    });
  });

  it('should test all TableActions combinations', () => {
    const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
      component.tableActions
    );
    expect(tableActions).toEqual({
      'create,update,delete': {
        actions: ['Add', 'Edit', 'Protect', 'Unprotect', 'Copy', 'Delete'],
        primary: { multiple: 'Delete', executing: 'Edit', single: 'Edit', no: 'Add' }
      },
      'create,update': {
        actions: ['Add', 'Edit', 'Protect', 'Unprotect', 'Copy'],
        primary: { multiple: 'Add', executing: 'Edit', single: 'Edit', no: 'Add' }
      },
      'create,delete': {
        actions: ['Add', 'Copy', 'Delete'],
        primary: { multiple: 'Delete', executing: 'Copy', single: 'Copy', no: 'Add' }
      },
      create: {
        actions: ['Add', 'Copy'],
        primary: { multiple: 'Add', executing: 'Copy', single: 'Copy', no: 'Add' }
      },
      'update,delete': {
        actions: ['Edit', 'Protect', 'Unprotect', 'Delete'],
        primary: { multiple: 'Delete', executing: 'Edit', single: 'Edit', no: 'Edit' }
      },
      update: {
        actions: ['Edit', 'Protect', 'Unprotect'],
        primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
      },
      delete: {
        actions: ['Delete'],
        primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
      },
      'no-permissions': {
        actions: [],
        primary: { multiple: '', executing: '', single: '', no: '' }
      }
    });
  });

  it('should convert any name to a proper CSS class', () => {
    expect(component.toClassName({ name: 'Create' } as CdTableAction)).toBe('create');
    expect(component.toClassName({ name: 'Mark x down' } as CdTableAction)).toBe('mark-x-down');
    expect(component.toClassName({ name: '?Su*per!' } as CdTableAction)).toBe('super');
  });

  describe('useDisableDesc', () => {
    it('should return a description if disable method returns a string', () => {
      const deleteWithDescAction: CdTableAction = {
        permission: 'delete',
        icon: 'fa-times',
        canBePrimary: (selection: CdTableSelection) => selection.hasSelection,
        disable: () => {
          return 'Delete action disabled description';
        },
        name: 'DeleteDesc'
      };

      expect(component.useDisableDesc(deleteWithDescAction)).toBe(
        'Delete action disabled description'
      );
    });

    it('should return no description if disable does not return string', () => {
      expect(component.useDisableDesc(deleteAction)).toBeUndefined();
    });
  });

  describe('useClickAction', () => {
    const editClickAction: CdTableAction = {
      permission: 'update',
      icon: 'fa-pencil',
      name: 'Edit',
      click: () => {
        return 'Edit action click';
      }
    };

    it('should call click action if action is not disabled', () => {
      editClickAction.disable = () => {
        return false;
      };
      expect(component.useClickAction(editClickAction)).toBe('Edit action click');
    });

    it('should not call click action if action is disabled', () => {
      editClickAction.disable = () => {
        return true;
      };
      expect(component.useClickAction(editClickAction)).toBeFalsy();
    });
  });
});