From: Tiago Melo Date: Tue, 28 May 2019 17:06:25 +0000 (+0000) Subject: mgr/dashboard: Reduce code duplication through TableActionComponent Unit Tests X-Git-Tag: v15.1.0~2196^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F28633%2Fhead;p=ceph.git mgr/dashboard: Reduce code duplication through TableActionComponent Unit Tests Fixes: http://tracker.ceph.com/issues/40399 Signed-off-by: Patrick Nawracay --- diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.html b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.html index 985bd476e3f4f..e5e7677d2b46b 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.html +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.html @@ -25,7 +25,7 @@ (updateSelection)="updateSelection($event)">
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.spec.ts index cbf88d290e5ca..be3867acdecab 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.spec.ts @@ -1,6 +1,5 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { TreeModule } from 'ng2-tree'; @@ -173,158 +172,45 @@ describe('IscsiTargetListComponent', () => { }); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permissions.iscsi, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: 'Edit', - empty: 'Create' - }; - }); - - describe('with all', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - it(`shows 'Edit' for single selection else 'Create' as main action`, () => { - permissionHelper.testScenarios(scenario); - }); - - it('shows all actions', () => { - expect(tableActions.tableActions.length).toBe(3); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0); - scenario.single = 'Edit'; - }); - - it(`should always show 'Edit'`, () => { - permissionHelper.testScenarios(scenario); - }); - - it(`shows all actions except for 'Delete'`, () => { - expect(tableActions.tableActions.length).toBe(2); - component.tableActions.pop(); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1); - }); - - it(`shows 'Delete' for single selection else 'Create' as main action`, () => { - scenario.single = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Create' and 'Delete' actions`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[2] - ]); - }); - }); - - describe('with read, edit and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = 'Edit'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Edit' and 'Delete' actions`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[1], - component.tableActions[2] - ]); - }); - }); - - describe('with read and create', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0); - }); - - it(`shows 'Create' for single selection and 'Create' as main action`, () => { - scenario.single = 'Create'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Create' actions`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[0]]); - }); - }); - - describe('with read and edit', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it(`shows no actions`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[1]]); - }); - }); - - describe('with read and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1); - }); - - it(`shows always 'Delete' as main action`, () => { - scenario.single = 'Delete'; - scenario.empty = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Delete' actions`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[2]]); - }); - }); - - describe('with only read', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); - }); - - it('shows no main action', () => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - - it('shows no actions', () => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: ['Create', 'Edit', 'Delete'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,update': { + actions: ['Create', 'Edit'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,delete': { + actions: ['Create', 'Delete'], + primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' } + }, + create: { + actions: ['Create'], + primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' } + }, + 'update,delete': { + actions: ['Edit', 'Delete'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + update: { + actions: ['Edit'], + 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: '' } + } }); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.ts index 20cbfc5b9cb7c..b3b72067f2337 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-list/iscsi-target-list.component.ts @@ -14,7 +14,7 @@ import { CdTableAction } from '../../../shared/models/cd-table-action'; import { CdTableColumn } from '../../../shared/models/cd-table-column'; import { CdTableSelection } from '../../../shared/models/cd-table-selection'; import { FinishedTask } from '../../../shared/models/finished-task'; -import { Permissions } from '../../../shared/models/permissions'; +import { Permission } from '../../../shared/models/permissions'; import { CephReleaseNamePipe } from '../../../shared/pipes/ceph-release-name.pipe'; import { AuthStorageService } from '../../../shared/services/auth-storage.service'; import { SummaryService } from '../../../shared/services/summary.service'; @@ -36,7 +36,7 @@ export class IscsiTargetListComponent implements OnInit, OnDestroy { columns: CdTableColumn[]; docsUrl: string; modalRef: BsModalRef; - permissions: Permissions; + permission: Permission; selection = new CdTableSelection(); settings: any; status: string; @@ -64,7 +64,7 @@ export class IscsiTargetListComponent implements OnInit, OnDestroy { private taskWrapper: TaskWrapperService, public actionLabels: ActionLabelsI18n ) { - this.permissions = this.authStorageService.getPermissions(); + this.permission = this.authStorageService.getPermissions().iscsi; this.tableActions = [ { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-list/rbd-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-list/rbd-list.component.spec.ts index 1a418fe83ec4c..1350372d070f7 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-list/rbd-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-list/rbd-list.component.spec.ts @@ -1,6 +1,5 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { AlertModule } from 'ngx-bootstrap/alert'; @@ -17,7 +16,6 @@ import { PermissionHelper } from '../../../../testing/unit-test-helper'; import { RbdService } from '../../../shared/api/rbd.service'; -import { ActionLabels } from '../../../shared/constants/app.constants'; import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component'; import { ViewCacheStatus } from '../../../shared/enum/view-cache-status.enum'; import { ExecutingTask } from '../../../shared/models/executing-task'; @@ -197,174 +195,45 @@ describe('RbdListComponent', () => { }); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permission, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: ActionLabels.EDIT, - empty: ActionLabels.CREATE - }; - }); - - describe('with all', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - it(`shows 'Edit' for single selection else 'Add' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it('shows all actions', () => { - expect(tableActions.tableActions.length).toBe(6); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0); - }); - - it(`shows 'Edit' for single selection else 'Add' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it(`shows all actions except for 'Delete' and 'Move'`, () => { - expect(tableActions.tableActions.length).toBe(4); - component.tableActions.pop(); - component.tableActions.pop(); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1); - }); - - it(`shows 'Copy' for single selection else 'Add' as main action`, () => { - scenario.single = 'Copy'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Add', 'Copy', 'Delete' and 'Move' action`, () => { - expect(tableActions.tableActions.length).toBe(4); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[2], - component.tableActions[4], - component.tableActions[5] - ]); - }); - }); - - describe('with read, edit and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = 'Edit'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Edit', 'Flatten', 'Delete' and 'Move' action`, () => { - expect(tableActions.tableActions.length).toBe(4); - expect(tableActions.tableActions).toEqual([ - component.tableActions[1], - component.tableActions[3], - component.tableActions[4], - component.tableActions[5] - ]); - }); - }); - - describe('with read and create', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0); - }); - - it(`shows 'Copy' for single selection else 'Add' as main action`, () => { - scenario.single = 'Copy'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Copy' and 'Add' actions`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[2] - ]); - }); - }); - - describe('with read and edit', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = 'Edit'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Edit' and 'Flatten' actions`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[1], - component.tableActions[3] - ]); - }); - }); - - describe('with read and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1); - }); - - it(`shows always 'Delete' as main action`, () => { - scenario.single = 'Delete'; - scenario.empty = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Delete' and 'Move' actions`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[4], - component.tableActions[5] - ]); - }); - }); - - describe('with only read', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); - }); - - it('shows no main action', () => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - - it('shows no actions', () => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: ['Create', 'Edit', 'Copy', 'Flatten', 'Delete', 'Move to Trash'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,update': { + actions: ['Create', 'Edit', 'Copy', 'Flatten'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,delete': { + actions: ['Create', 'Copy', 'Delete', 'Move to Trash'], + primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' } + }, + create: { + actions: ['Create', 'Copy'], + primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' } + }, + 'update,delete': { + actions: ['Edit', 'Flatten', 'Delete', 'Move to Trash'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + update: { + actions: ['Edit', 'Flatten'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + delete: { + actions: ['Delete', 'Move to Trash'], + primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' } + }, + 'no-permissions': { + actions: [], + primary: { multiple: '', executing: '', single: '', no: '' } + } }); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-snapshot-list/rbd-snapshot-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-snapshot-list/rbd-snapshot-list.component.spec.ts index 8790f5f39cab3..5c9b960ea4737 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-snapshot-list/rbd-snapshot-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-snapshot-list/rbd-snapshot-list.component.spec.ts @@ -1,6 +1,5 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { I18n } from '@ngx-translate/i18n-polyfill'; @@ -212,209 +211,54 @@ describe('RbdSnapshotListComponent', () => { }); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permission, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: 'Rename', - empty: 'Create' - }; - }); - - describe('with all', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - it(`shows 'Rename' for single selection else 'Create' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it('shows all actions', () => { - expect(tableActions.tableActions.length).toBe(8); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0); - }); - - it(`shows 'Rename' for single selection else 'Create' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it(`shows all actions except for 'Delete'`, () => { - expect(tableActions.tableActions.length).toBe(7); - component.tableActions.pop(); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1); - }); - - it(`shows 'Clone' for single selection else 'Create' as main action`, () => { - scenario.single = 'Clone'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Create', 'Clone', 'Copy' and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(4); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[4], - component.tableActions[5], - component.tableActions[7] - ]); - }); - }); - - describe('with read, edit and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1); - }); - - it(`shows always 'Rename' as main action`, () => { - scenario.empty = 'Rename'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Rename', 'Protect', 'Unprotect', 'Rollback' and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(5); - expect(tableActions.tableActions).toEqual([ - component.tableActions[1], - component.tableActions[2], - component.tableActions[3], - component.tableActions[6], - component.tableActions[7] - ]); - }); - }); - - describe('with read and create', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0); - }); - - it(`shows 'Clone' for single selection else 'Create' as main action`, () => { - scenario.single = 'Clone'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Create', 'Clone' and 'Copy' actions`, () => { - expect(tableActions.tableActions.length).toBe(3); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[4], - component.tableActions[5] - ]); - }); - }); - - describe('with read and edit', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it(`shows always 'Rename' as main action`, () => { - scenario.empty = 'Rename'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Rename', 'Protect', 'Unprotect' and 'Rollback' actions`, () => { - expect(tableActions.tableActions.length).toBe(4); - expect(tableActions.tableActions).toEqual([ - component.tableActions[1], - component.tableActions[2], - component.tableActions[3], - component.tableActions[6] - ]); - }); - }); - - describe('with read and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1); - }); - - it(`shows always 'Delete' as main action`, () => { - scenario.single = 'Delete'; - scenario.empty = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[7]]); - }); - }); - - describe('with only read', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); - }); - - it('shows no main action', () => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - - it('shows no actions', () => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); - }); - - describe('test unprotected and protected action cases', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it(`shows none of them if nothing is selected`, () => { - permissionHelper.setSelection([]); - fixture.detectChanges(); - expect(tableActions.dropDownActions).toEqual([ - component.tableActions[1], - component.tableActions[6] - ]); - }); - - it(`shows 'Protect' of them if nothing is selected`, () => { - permissionHelper.setSelection([{ is_protected: false }]); - fixture.detectChanges(); - expect(tableActions.dropDownActions).toEqual([ - component.tableActions[1], - component.tableActions[2], - component.tableActions[6] - ]); - }); - - it(`shows 'Unprotect' of them if nothing is selected`, () => { - permissionHelper.setSelection([{ is_protected: true }]); - fixture.detectChanges(); - expect(tableActions.dropDownActions).toEqual([ - component.tableActions[1], - component.tableActions[3], - component.tableActions[6] - ]); - }); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: [ + 'Create', + 'Rename', + 'Protect', + 'Unprotect', + 'Clone', + 'Copy', + 'Rollback', + 'Delete' + ], + primary: { multiple: 'Create', executing: 'Rename', single: 'Rename', no: 'Create' } + }, + 'create,update': { + actions: ['Create', 'Rename', 'Protect', 'Unprotect', 'Clone', 'Copy', 'Rollback'], + primary: { multiple: 'Create', executing: 'Rename', single: 'Rename', no: 'Create' } + }, + 'create,delete': { + actions: ['Create', 'Clone', 'Copy', 'Delete'], + primary: { multiple: 'Create', executing: 'Clone', single: 'Clone', no: 'Create' } + }, + create: { + actions: ['Create', 'Clone', 'Copy'], + primary: { multiple: 'Create', executing: 'Clone', single: 'Clone', no: 'Create' } + }, + 'update,delete': { + actions: ['Rename', 'Protect', 'Unprotect', 'Rollback', 'Delete'], + primary: { multiple: 'Rename', executing: 'Rename', single: 'Rename', no: 'Rename' } + }, + update: { + actions: ['Rename', 'Protect', 'Unprotect', 'Rollback'], + primary: { multiple: 'Rename', executing: 'Rename', single: 'Rename', no: 'Rename' } + }, + delete: { + actions: ['Delete'], + primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' } + }, + 'no-permissions': { + actions: [], + primary: { multiple: '', executing: '', single: '', no: '' } + } }); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/mgr-modules/mgr-module-list/mgr-module-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/mgr-modules/mgr-module-list/mgr-module-list.component.spec.ts index 252bd67e2085a..7c10a1700ed88 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/mgr-modules/mgr-module-list/mgr-module-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/mgr-modules/mgr-module-list/mgr-module-list.component.spec.ts @@ -1,6 +1,5 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { TabsModule } from 'ngx-bootstrap/tabs'; @@ -50,57 +49,39 @@ describe('MgrModuleListComponent', () => { expect(component).toBeTruthy(); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permission, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: 'Edit', - empty: 'Edit' - }; - }); - - describe('with read and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it('shows action button', () => permissionHelper.testScenarios(scenario)); - - it('shows all actions', () => { - expect(tableActions.tableActions.length).toBe(3); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with only read', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); - }); - - it('shows no main action', () => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - - it('shows no actions', () => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: ['Edit', 'Enable', 'Disable'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + 'create,update': { + actions: ['Edit', 'Enable', 'Disable'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + 'create,delete': { + actions: [], + primary: { multiple: '', executing: '', single: '', no: '' } + }, + create: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } }, + 'update,delete': { + actions: ['Edit', 'Enable', 'Disable'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + update: { + actions: ['Edit', 'Enable', 'Disable'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + delete: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } }, + 'no-permissions': { + actions: [], + primary: { multiple: '', executing: '', single: '', no: '' } + } }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-list/osd-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-list/osd-list.component.spec.ts index fdd50c66e6154..f44158e9e2264 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-list/osd-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-list/osd-list.component.spec.ts @@ -216,33 +216,72 @@ describe('OsdListComponent', () => { }); }); - describe('show table actions as defined', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - - const getTableActionComponent = () => { - fixture.detectChanges(); - return fixture.debugElement.query(By.css('#osd-actions')).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permissions.osd, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: 'Scrub', - empty: 'Scrub' - }; - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - it('shows action button', () => permissionHelper.testScenarios(scenario)); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permissions.osd); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); - it('shows all actions', () => { - expect(tableActions.tableActions.length).toBe(9); - expect(tableActions.tableActions).toEqual(component.tableActions); + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: [ + 'Scrub', + 'Deep Scrub', + 'Reweight', + 'Mark Out', + 'Mark In', + 'Mark Down', + 'Mark Lost', + 'Purge', + 'Destroy' + ], + primary: { multiple: 'Scrub', executing: 'Scrub', single: 'Scrub', no: 'Scrub' } + }, + 'create,update': { + actions: ['Scrub', 'Deep Scrub', 'Reweight', 'Mark Out', 'Mark In', 'Mark Down'], + primary: { multiple: 'Scrub', executing: 'Scrub', single: 'Scrub', no: 'Scrub' } + }, + 'create,delete': { + actions: ['Mark Lost', 'Purge', 'Destroy'], + primary: { + multiple: 'Mark Lost', + executing: 'Mark Lost', + single: 'Mark Lost', + no: 'Mark Lost' + } + }, + create: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } }, + 'update,delete': { + actions: [ + 'Scrub', + 'Deep Scrub', + 'Reweight', + 'Mark Out', + 'Mark In', + 'Mark Down', + 'Mark Lost', + 'Purge', + 'Destroy' + ], + primary: { multiple: 'Scrub', executing: 'Scrub', single: 'Scrub', no: 'Scrub' } + }, + update: { + actions: ['Scrub', 'Deep Scrub', 'Reweight', 'Mark Out', 'Mark In', 'Mark Down'], + primary: { multiple: 'Scrub', executing: 'Scrub', single: 'Scrub', no: 'Scrub' } + }, + delete: { + actions: ['Mark Lost', 'Purge', 'Destroy'], + primary: { + multiple: 'Mark Lost', + executing: 'Mark Lost', + single: 'Mark Lost', + no: 'Mark Lost' + } + }, + 'no-permissions': { + actions: [], + primary: { multiple: '', executing: '', single: '', no: '' } + } }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/alert-list/alert-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/alert-list/alert-list.component.spec.ts index 1b53db811a9c2..e0ec0568da1fb 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/alert-list/alert-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/alert-list/alert-list.component.spec.ts @@ -1,6 +1,5 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { TabsModule } from 'ngx-bootstrap/tabs'; @@ -16,7 +15,7 @@ import { SharedModule } from '../../../../shared/shared.module'; import { PrometheusTabsComponent } from '../prometheus-tabs/prometheus-tabs.component'; import { AlertListComponent } from './alert-list.component'; -describe('PrometheusListComponent', () => { +describe('AlertListComponent', () => { let component: AlertListComponent; let fixture: ComponentFixture; @@ -42,84 +41,60 @@ describe('PrometheusListComponent', () => { expect(component).toBeTruthy(); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - let combinations: number[][]; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permission, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: 'Create silence', - empty: 'Create silence' - }; - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - const permissionSwitch = (combination) => { - tableActions = permissionHelper.setPermissionsAndGetActions( - combination[0], - combination[1], - combination[2] - ); - tableActions.tableActions = component.tableActions; - tableActions.ngOnInit(); - }; - - const testCombinations = (test: Function) => { - combinations.forEach((combination) => { - permissionSwitch(combination); - test(); - }); - }; - - describe('with every permission combination that includes create', () => { - beforeEach(() => { - combinations = [[1, 1, 1], [1, 1, 0], [1, 0, 1], [1, 0, 0]]; - }); - - it(`always shows 'Create silence' as main action`, () => { - testCombinations(() => permissionHelper.testScenarios(scenario)); - }); - - it('shows all actions', () => { - testCombinations(() => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - }); - - describe('with every permission combination that does not include create', () => { - beforeEach(() => { - combinations = [[0, 1, 1], [0, 1, 0], [0, 0, 1], [0, 0, 0]]; - }); - - it(`won't show any action`, () => { - testCombinations(() => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - }); - - it('shows no actions', () => { - testCombinations(() => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); - }); + it('should test all TableActions combinations', () => { + component.ngOnInit(); + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: ['Create silence'], + primary: { + multiple: 'Create silence', + executing: 'Create silence', + single: 'Create silence', + no: 'Create silence' + } + }, + 'create,update': { + actions: ['Create silence'], + primary: { + multiple: 'Create silence', + executing: 'Create silence', + single: 'Create silence', + no: 'Create silence' + } + }, + 'create,delete': { + actions: ['Create silence'], + primary: { + multiple: 'Create silence', + executing: 'Create silence', + single: 'Create silence', + no: 'Create silence' + } + }, + create: { + actions: ['Create silence'], + primary: { + multiple: 'Create silence', + executing: 'Create silence', + single: 'Create silence', + no: 'Create silence' + } + }, + 'update,delete': { + actions: [], + primary: { multiple: '', executing: '', single: '', no: '' } + }, + update: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } }, + delete: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } }, + 'no-permissions': { + actions: [], + primary: { multiple: '', executing: '', single: '', no: '' } + } }); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.spec.ts index 10eeb061dfb70..be6127cb9c1ef 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.spec.ts @@ -1,6 +1,5 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { BsDropdownModule } from 'ngx-bootstrap/dropdown'; @@ -53,216 +52,45 @@ describe('SilenceListComponent', () => { expect(component).toBeTruthy(); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - let silenceState: string; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - const setSilenceState = (state) => { - silenceState = state; - }; - - const testNonExpiredSilenceScenario = () => { - setSilenceState('active'); - permissionHelper.testScenarios(scenario); - setSilenceState('pending'); - permissionHelper.testScenarios(scenario); - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permission, () => - getTableActionComponent() - ); - permissionHelper.createSelection = () => ({ status: { state: silenceState } }); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: 'Edit', - empty: 'Create' - }; - }); - - describe('with all', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - it(`shows 'Edit' for single non expired silence else 'Create' as main action`, () => { - scenario.single = 'Edit'; - testNonExpiredSilenceScenario(); - }); - - it(`shows 'Recreate' for single expired silence else 'Create' as main action`, () => { - scenario.single = 'Recreate'; - setSilenceState('expired'); - permissionHelper.testScenarios(scenario); - }); - - it('can use all actions', () => { - expect(tableActions.tableActions.length).toBe(4); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0); - }); - - it(`shows 'Edit' for single non expired silence else 'Create' as main action`, () => { - scenario.single = 'Edit'; - testNonExpiredSilenceScenario(); - }); - - it(`shows 'Recreate' for single expired silence else 'Create' as main action`, () => { - scenario.single = 'Recreate'; - setSilenceState('expired'); - permissionHelper.testScenarios(scenario); - }); - - it(`can use all actions except for 'Expire'`, () => { - expect(tableActions.tableActions.length).toBe(3); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[1], - component.tableActions[2] - ]); - }); - }); - - describe('with read, create and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1); - }); - - it(`shows 'Expire' for single non expired silence else 'Create' as main action`, () => { - scenario.single = 'Expire'; - testNonExpiredSilenceScenario(); - }); - - it(`shows 'Recreate' for single expired silence else 'Create' as main action`, () => { - scenario.single = 'Recreate'; - setSilenceState('expired'); - permissionHelper.testScenarios(scenario); - }); - - it(`can use 'Create' and 'Expire' action`, () => { - expect(tableActions.tableActions.length).toBe(3); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[1], - component.tableActions[3] - ]); - }); - }); - - describe('with read, edit and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1); - }); - - it(`shows always 'Edit' as main action for any state`, () => { - scenario.single = 'Edit'; - scenario.empty = 'Edit'; - testNonExpiredSilenceScenario(); - setSilenceState('expired'); - permissionHelper.testScenarios(scenario); - }); - - it(`can use 'Edit' and 'Expire' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[2], - component.tableActions[3] - ]); - }); - }); - - describe('with read and create', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0); - }); - - it(`shows always 'Create' as main action for single non expired silences`, () => { - scenario.single = 'Create'; - testNonExpiredSilenceScenario(); - }); - - it(`shows 'Recreate' for single expired silence else 'Create' as main action`, () => { - scenario.single = 'Recreate'; - setSilenceState('expired'); - permissionHelper.testScenarios(scenario); - }); - - it(`can use 'Create' and 'Recreate' actions`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[1] - ]); - }); - }); - - describe('with read and edit', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it(`shows always 'Edit' as main action for any state`, () => { - scenario.single = 'Edit'; - scenario.empty = 'Edit'; - testNonExpiredSilenceScenario(); - setSilenceState('expired'); - permissionHelper.testScenarios(scenario); - }); - - it(`can use 'Edit' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[2]]); - }); - }); - - describe('with read and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1); - }); - - it(`shows always 'Expire' as main action for any state`, () => { - scenario.single = 'Expire'; - scenario.empty = 'Expire'; - testNonExpiredSilenceScenario(); - setSilenceState('expired'); - permissionHelper.testScenarios(scenario); - }); - - it(`can use 'Expire' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[3]]); - }); - }); - - describe('with only read', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); - }); - - it('shows no main action', () => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - - it('can use no actions', () => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: ['Create', 'Recreate', 'Edit', 'Expire'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,update': { + actions: ['Create', 'Recreate', 'Edit'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,delete': { + actions: ['Create', 'Recreate', 'Expire'], + primary: { multiple: 'Create', executing: 'Expire', single: 'Expire', no: 'Create' } + }, + create: { + actions: ['Create', 'Recreate'], + primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' } + }, + 'update,delete': { + actions: ['Edit', 'Expire'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + update: { + actions: ['Edit'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + delete: { + actions: ['Expire'], + primary: { multiple: 'Expire', executing: 'Expire', single: 'Expire', no: 'Expire' } + }, + 'no-permissions': { + actions: [], + primary: { multiple: '', executing: '', single: '', no: '' } + } }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.ts index d2ea0ee438244..2671a1d46c58c 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component } from '@angular/core'; import { I18n } from '@ngx-translate/i18n-polyfill'; import { SortDirection, SortPropDir } from '@swimlane/ngx-datatable'; @@ -32,7 +32,7 @@ const BASE_URL = 'silence'; templateUrl: './silence-list.component.html', styleUrls: ['./silence-list.component.scss'] }) -export class SilenceListComponent implements OnInit { +export class SilenceListComponent { silences: AlertmanagerSilence[] = []; columns: CdTableColumn[]; tableActions: CdTableAction[]; @@ -58,11 +58,8 @@ export class SilenceListComponent implements OnInit { private succeededLabels: SucceededActionLabelsI18n ) { this.permission = this.authStorageService.getPermissions().prometheus; - } - - ngOnInit() { const selectionExpired = (selection: CdTableSelection) => - selection.first() && selection.first().status.state === 'expired'; + selection.first() && selection.first().status && selection.first().status.state === 'expired'; this.tableActions = [ { permission: 'create', diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts index 2167fbd4ff1cb..9ebc6943bfde8 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts @@ -1,6 +1,5 @@ import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { TabsModule } from 'ngx-bootstrap/tabs'; @@ -169,160 +168,45 @@ describe('NfsListComponent', () => { }); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permission, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: 'Edit', - empty: 'Create' - }; - }); - - describe('with all', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - it(`shows 'Edit' for single selection else 'Create' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it('shows all actions', () => { - expect(tableActions.tableActions.length).toBe(3); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0); - }); - - it(`shows 'Edit' for single selection else 'Create' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it(`shows all actions except for 'Delete'`, () => { - expect(tableActions.tableActions.length).toBe(2); - component.tableActions.pop(); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1); - }); - - it(`shows 'Delete' for single selection else 'Create' as main action`, () => { - scenario.single = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Create', and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[2] - ]); - }); - }); - - describe('with read, edit and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = 'Edit'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Edit' and 'Delete' actions`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[1], - component.tableActions[2] - ]); - }); - }); - - describe('with read and create', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0); - }); - - it(`always shows 'Create' as main action`, () => { - scenario.single = 'Create'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Create' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[0]]); - }); - }); - - describe('with read and edit', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = 'Edit'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Edit' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[1]]); - }); - }); - - describe('with read and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1); - }); - - it(`shows always 'Delete' as main action`, () => { - scenario.single = 'Delete'; - scenario.empty = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[2]]); - }); - }); - - describe('with only read', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); - }); - - it('shows no main action', () => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - - it('shows no actions', () => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: ['Create', 'Edit', 'Delete'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,update': { + actions: ['Create', 'Edit'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,delete': { + actions: ['Create', 'Delete'], + primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' } + }, + create: { + actions: ['Create'], + primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' } + }, + 'update,delete': { + actions: ['Edit', 'Delete'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + update: { + actions: ['Edit'], + 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: '' } + } }); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-bucket-list/rgw-bucket-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-bucket-list/rgw-bucket-list.component.spec.ts index 2d4736dd71ab7..bb8fcf71918f8 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-bucket-list/rgw-bucket-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-bucket-list/rgw-bucket-list.component.spec.ts @@ -1,6 +1,5 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { ModalModule } from 'ngx-bootstrap/modal'; @@ -11,7 +10,6 @@ import { i18nProviders, PermissionHelper } from '../../../../testing/unit-test-helper'; -import { ActionLabels } from '../../../shared/constants/app.constants'; import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component'; import { SharedModule } from '../../../shared/shared.module'; import { RgwBucketDetailsComponent } from '../rgw-bucket-details/rgw-bucket-details.component'; @@ -43,160 +41,45 @@ describe('RgwBucketListComponent', () => { expect(component).toBeTruthy(); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permission, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: ActionLabels.EDIT, - empty: ActionLabels.CREATE - }; - }); - - describe('with all', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - it(`shows 'Edit' for single selection else 'Add' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it('shows all actions', () => { - expect(tableActions.tableActions.length).toBe(3); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0); - }); - - it(`shows 'Edit' for single selection else 'Add' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it(`shows 'Add' and 'Edit' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - component.tableActions.pop(); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1); - }); - - it(`shows 'Delete' for single selection else 'Add' as main action`, () => { - scenario.single = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Add' and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[2] - ]); - }); - }); - - describe('with read, edit and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = ActionLabels.EDIT; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Edit' and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[1], - component.tableActions[2] - ]); - }); - }); - - describe('with read and create', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0); - }); - - it(`shows always 'Add' as main action`, () => { - scenario.single = ActionLabels.CREATE; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Add' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[0]]); - }); - }); - - describe('with read and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = ActionLabels.EDIT; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Edit' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[1]]); - }); - }); - - describe('with read and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1); - }); - - it(`shows always 'Delete' as main action`, () => { - scenario.single = 'Delete'; - scenario.empty = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[2]]); - }); - }); - - describe('with only read', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); - }); - - it('shows no main action', () => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - - it('shows no actions', () => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: ['Create', 'Edit', 'Delete'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,update': { + actions: ['Create', 'Edit'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,delete': { + actions: ['Create', 'Delete'], + primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' } + }, + create: { + actions: ['Create'], + primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' } + }, + 'update,delete': { + actions: ['Edit', 'Delete'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + update: { + actions: ['Edit'], + 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: '' } + } }); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-list/rgw-user-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-list/rgw-user-list.component.spec.ts index 46c3ee99f8bec..431bd4696782d 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-list/rgw-user-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-list/rgw-user-list.component.spec.ts @@ -1,7 +1,6 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { NO_ERRORS_SCHEMA } from '@angular/core'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { ModalModule } from 'ngx-bootstrap/modal'; @@ -11,7 +10,6 @@ import { i18nProviders, PermissionHelper } from '../../../../testing/unit-test-helper'; -import { ActionLabels } from '../../../shared/constants/app.constants'; import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component'; import { SharedModule } from '../../../shared/shared.module'; import { RgwUserListComponent } from './rgw-user-list.component'; @@ -37,160 +35,45 @@ describe('RgwUserListComponent', () => { expect(component).toBeTruthy(); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permission, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: ActionLabels.EDIT, - empty: ActionLabels.CREATE - }; - }); - - describe('with all', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - it(`shows 'Edit' for single selection else 'Add' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it('shows all actions', () => { - expect(tableActions.tableActions.length).toBe(3); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0); - }); - - it(`shows 'Edit' for single selection else 'Add' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it(`shows 'Add' and 'Edit' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - component.tableActions.pop(); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1); - }); - - it(`shows 'Delete' for single selection else 'Add' as main action`, () => { - scenario.single = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Add' and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[2] - ]); - }); - }); - - describe('with read, edit and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = ActionLabels.EDIT; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Edit' and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[1], - component.tableActions[2] - ]); - }); - }); - - describe('with read and create', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0); - }); - - it(`shows always 'Add' as main action`, () => { - scenario.single = ActionLabels.CREATE; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Add' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[0]]); - }); - }); - - describe('with read and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = ActionLabels.EDIT; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Edit' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[1]]); - }); - }); - - describe('with read and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1); - }); - - it(`shows always 'Delete' as main action`, () => { - scenario.single = 'Delete'; - scenario.empty = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[2]]); - }); - }); - - describe('with only read', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); - }); - - it('shows no main action', () => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - - it('shows no actions', () => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: ['Create', 'Edit', 'Delete'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,update': { + actions: ['Create', 'Edit'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,delete': { + actions: ['Create', 'Delete'], + primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' } + }, + create: { + actions: ['Create'], + primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' } + }, + 'update,delete': { + actions: ['Edit', 'Delete'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + update: { + actions: ['Edit'], + 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: '' } + } }); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-list/role-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-list/role-list.component.spec.ts index 7274125f04dc1..799cf25039760 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-list/role-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-list/role-list.component.spec.ts @@ -1,6 +1,5 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { TabsModule } from 'ngx-bootstrap/tabs'; @@ -11,7 +10,6 @@ import { i18nProviders, PermissionHelper } from '../../../../testing/unit-test-helper'; -import { ActionLabels } from '../../../shared/constants/app.constants'; import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component'; import { SharedModule } from '../../../shared/shared.module'; import { RoleDetailsComponent } from '../role-details/role-details.component'; @@ -44,160 +42,45 @@ describe('RoleListComponent', () => { expect(component).toBeTruthy(); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permission, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: ActionLabels.EDIT, - empty: ActionLabels.CREATE - }; - }); - - describe('with all', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - it(`shows 'Edit' for single selection else 'Add' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it('shows all actions', () => { - expect(tableActions.tableActions.length).toBe(3); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0); - }); - - it(`shows 'Edit' for single selection else 'Add' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it(`shows 'Add' and 'Edit' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - component.tableActions.pop(); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1); - }); - - it(`shows 'Delete' for single selection else 'Add' as main action`, () => { - scenario.single = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Add' and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[2] - ]); - }); - }); - - describe('with read, edit and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = ActionLabels.EDIT; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Edit' and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[1], - component.tableActions[2] - ]); - }); - }); - - describe('with read and create', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0); - }); - - it(`shows always 'Add' as main action`, () => { - scenario.single = ActionLabels.CREATE; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Add' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[0]]); - }); - }); - - describe('with read and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = ActionLabels.EDIT; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Edit' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[1]]); - }); - }); - - describe('with read and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1); - }); - - it(`shows always 'Delete' as main action`, () => { - scenario.single = 'Delete'; - scenario.empty = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[2]]); - }); - }); - - describe('with only read', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); - }); - - it('shows no main action', () => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - - it('shows no actions', () => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: ['Create', 'Edit', 'Delete'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,update': { + actions: ['Create', 'Edit'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,delete': { + actions: ['Create', 'Delete'], + primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' } + }, + create: { + actions: ['Create'], + primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' } + }, + 'update,delete': { + actions: ['Edit', 'Delete'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + update: { + actions: ['Edit'], + 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: '' } + } }); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-list/user-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-list/user-list.component.spec.ts index 1a34a2f6b7d66..253fc486a3776 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-list/user-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-list/user-list.component.spec.ts @@ -1,6 +1,5 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { TabsModule } from 'ngx-bootstrap/tabs'; @@ -11,7 +10,6 @@ import { i18nProviders, PermissionHelper } from '../../../../testing/unit-test-helper'; -import { ActionLabels } from '../../../shared/constants/app.constants'; import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component'; import { SharedModule } from '../../../shared/shared.module'; import { UserTabsComponent } from '../user-tabs/user-tabs.component'; @@ -43,160 +41,45 @@ describe('UserListComponent', () => { expect(component).toBeTruthy(); }); - describe('show action buttons and drop down actions depending on permissions', () => { - let tableActions: TableActionsComponent; - let scenario: { fn; empty; single }; - let permissionHelper: PermissionHelper; - - const getTableActionComponent = (): TableActionsComponent => { - fixture.detectChanges(); - return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; - }; - - beforeEach(() => { - permissionHelper = new PermissionHelper(component.permission, () => - getTableActionComponent() - ); - scenario = { - fn: () => tableActions.getCurrentButton().name, - single: ActionLabels.EDIT, - empty: ActionLabels.CREATE - }; - }); - - describe('with all', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1); - }); - - it(`shows 'Edit' for single selection else 'Add' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it('shows all actions', () => { - expect(tableActions.tableActions.length).toBe(3); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0); - }); - - it(`shows 'Edit' for single selection else 'Add' as main action`, () => - permissionHelper.testScenarios(scenario)); - - it(`shows 'Add' and 'Edit' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - component.tableActions.pop(); - expect(tableActions.tableActions).toEqual(component.tableActions); - }); - }); - - describe('with read, create and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1); - }); - - it(`shows 'Delete' for single selection else 'Add' as main action`, () => { - scenario.single = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Add' and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[0], - component.tableActions[2] - ]); - }); - }); - - describe('with read, edit and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = ActionLabels.EDIT; - permissionHelper.testScenarios(scenario); - }); - - it(`shows 'Edit' and 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(2); - expect(tableActions.tableActions).toEqual([ - component.tableActions[1], - component.tableActions[2] - ]); - }); - }); - - describe('with read and create', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0); - }); - - it(`shows always 'Add' as main action`, () => { - scenario.single = ActionLabels.CREATE; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Add' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[0]]); - }); - }); - - describe('with read and update', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); - }); - - it(`shows always 'Edit' as main action`, () => { - scenario.empty = 'Edit'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Edit' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[1]]); - }); - }); - - describe('with read and delete', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1); - }); - - it(`shows always 'Delete' as main action`, () => { - scenario.single = 'Delete'; - scenario.empty = 'Delete'; - permissionHelper.testScenarios(scenario); - }); - - it(`shows only 'Delete' action`, () => { - expect(tableActions.tableActions.length).toBe(1); - expect(tableActions.tableActions).toEqual([component.tableActions[2]]); - }); - }); - - describe('with only read', () => { - beforeEach(() => { - tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); - }); - - it('shows no main action', () => { - permissionHelper.testScenarios({ - fn: () => tableActions.getCurrentButton(), - single: undefined, - empty: undefined - }); - }); - - it('shows no actions', () => { - expect(tableActions.tableActions.length).toBe(0); - expect(tableActions.tableActions).toEqual([]); - }); + it('should test all TableActions combinations', () => { + const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); + const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( + component.tableActions + ); + + expect(tableActions).toEqual({ + 'create,update,delete': { + actions: ['Create', 'Edit', 'Delete'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,update': { + actions: ['Create', 'Edit'], + primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' } + }, + 'create,delete': { + actions: ['Create', 'Delete'], + primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' } + }, + create: { + actions: ['Create'], + primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' } + }, + 'update,delete': { + actions: ['Edit', 'Delete'], + primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } + }, + update: { + actions: ['Edit'], + 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: '' } + } }); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-actions/table-actions.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-actions/table-actions.component.spec.ts index c8d47c3115457..2593a59b2bea7 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-actions/table-actions.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-actions/table-actions.component.spec.ts @@ -17,22 +17,8 @@ describe('TableActionsComponent', () => { let unprotectAction: CdTableAction; let deleteAction: CdTableAction; let copyAction: CdTableAction; - let scenario; let permissionHelper: PermissionHelper; - const getTableActionComponent = (): TableActionsComponent => { - component.tableActions = [ - addAction, - editAction, - protectAction, - unprotectAction, - copyAction, - deleteAction - ]; - component.ngOnInit(); - return component; - }; - configureTestBed({ declarations: [TableActionsComponent], imports: [ComponentsModule, RouterTestingModule] @@ -85,15 +71,23 @@ describe('TableActionsComponent', () => { component.selection = new CdTableSelection(); component.permission = new Permission(); component.permission.read = true; - permissionHelper = new PermissionHelper(component.permission, () => getTableActionComponent()); - permissionHelper.setPermissionsAndGetActions(1, 1, 1); + 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 ngInit should be called with no permissions', () => { + it('should call ngInit without permissions', () => { component.permission = undefined; component.ngOnInit(); expect(component.tableActions).toEqual([]); @@ -122,199 +116,43 @@ describe('TableActionsComponent', () => { }); }); - describe('disableSelectionAction', () => { - beforeEach(() => { - scenario = { - fn: () => null, - multiple: false, - singleExecuting: false, - single: false, - empty: false - }; - }); - - it('tests disabling addAction', () => { - scenario.fn = () => component.disableSelectionAction(addAction); - permissionHelper.testScenarios(scenario); - }); - - it('tests disabling editAction', () => { - scenario.fn = () => component.disableSelectionAction(editAction); - scenario.multiple = true; - scenario.empty = true; - scenario.singleExecuting = true; - permissionHelper.testScenarios(scenario); - }); - - it('tests disabling deleteAction', () => { - scenario.fn = () => component.disableSelectionAction(deleteAction); - scenario.multiple = false; - scenario.empty = true; - scenario.singleExecuting = true; - permissionHelper.testScenarios(scenario); - }); - - it('tests disabling copyAction', () => { - scenario.fn = () => component.disableSelectionAction(copyAction); - scenario.multiple = true; - scenario.empty = true; - scenario.singleExecuting = true; - permissionHelper.testScenarios(scenario); - }); - }); - - describe('get current button', () => { - const hiddenScenario = () => { - scenario.multiple = undefined; - scenario.empty = undefined; - scenario.singleExecuting = undefined; - scenario.single = undefined; - }; - - const setScenario = (defaultAction, selectionAction) => { - scenario.single = selectionAction; - scenario.singleExecuting = selectionAction; - scenario.multiple = defaultAction; - scenario.empty = defaultAction; - }; - - beforeEach(() => { - scenario = { - fn: () => component.getCurrentButton(), - singleExecuting: copyAction, - single: copyAction, - empty: addAction - }; - }); - - it('gets add for no, edit for single and delete for multiple selections', () => { - setScenario(addAction, editAction); - scenario.multiple = deleteAction; - permissionHelper.setPermissionsAndGetActions(1, 1, 1); - permissionHelper.testScenarios(scenario); - }); - - it('gets add action except for selections where it shows edit action', () => { - setScenario(addAction, editAction); - permissionHelper.setPermissionsAndGetActions(1, 1, 0); - permissionHelper.testScenarios(scenario); - }); - - it('gets add for no, copy for single and delete for multiple selections', () => { - setScenario(addAction, copyAction); - scenario.multiple = deleteAction; - permissionHelper.setPermissionsAndGetActions(1, 0, 1); - permissionHelper.testScenarios(scenario); - }); - - it('gets add action except for selections where it shows copy action', () => { - setScenario(addAction, copyAction); - permissionHelper.setPermissionsAndGetActions(1, 0, 0); - permissionHelper.testScenarios(scenario); - }); - - it('should always get edit action except delete for multiple items', () => { - setScenario(editAction, editAction); - scenario.multiple = deleteAction; - permissionHelper.setPermissionsAndGetActions(0, 1, 1); - permissionHelper.testScenarios(scenario); - }); - - it('should always get edit action', () => { - setScenario(editAction, editAction); - permissionHelper.setPermissionsAndGetActions(0, 1, 0); - permissionHelper.testScenarios(scenario); - }); - - it('should always get delete action', () => { - setScenario(deleteAction, deleteAction); - permissionHelper.setPermissionsAndGetActions(0, 0, 1); - permissionHelper.testScenarios(scenario); - }); - - it('should not get any button with no permissions, except the true action', () => { - hiddenScenario(); - permissionHelper.setPermissionsAndGetActions(0, 0, 0); - permissionHelper.testScenarios(scenario); - }); - - it('should not get any button if only a drop down should be shown', () => { - hiddenScenario(); - component.dropDownOnly = 'Drop down label that is shown'; - permissionHelper.setPermissionsAndGetActions(1, 1, 1); - permissionHelper.testScenarios(scenario); - }); - }); - - describe('show drop down', () => { - const testShowDropDownActions = (perms, expected) => { - permissionHelper.setPermissionsAndGetActions(perms[0], perms[1], perms[2]); - expect(`${perms} ${component.showDropDownActions()}`).toBe(`${perms} ${expected}`); - }; - - it('is shown if multiple items are found depending on the permissions', () => { - [[1, 0, 0], [1, 1, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [0, 1, 0]].forEach((perms) => { - testShowDropDownActions(perms, true); - }); - }); - - it('is not shown if only 1 or less items are found depending on the permissions', () => { - [[0, 0, 1], [0, 0, 0]].forEach((perms) => { - testShowDropDownActions(perms, false); - }); - }); - }); - - describe('all visible actions with all different permissions', () => { - it('with create, update and delete', () => { - permissionHelper.setPermissionsAndGetActions(1, 1, 1); - expect(component.dropDownActions).toEqual([ - addAction, - editAction, - unprotectAction, - copyAction, - deleteAction - ]); - }); - - it('with create and delete', () => { - permissionHelper.setPermissionsAndGetActions(1, 0, 1); - expect(component.dropDownActions).toEqual([addAction, copyAction, deleteAction]); - }); - - it('with create and update', () => { - permissionHelper.setPermissionsAndGetActions(1, 1, 0); - expect(component.dropDownActions).toEqual([ - addAction, - editAction, - unprotectAction, - copyAction - ]); - }); - - it('with create', () => { - permissionHelper.setPermissionsAndGetActions(1, 0, 0); - expect(component.dropDownActions).toEqual([addAction, copyAction]); - }); - - it('with update and delete', () => { - permissionHelper.setPermissionsAndGetActions(0, 1, 1); - expect(component.dropDownActions).toEqual([editAction, unprotectAction, deleteAction]); - }); - - it('with update', () => { - permissionHelper.setPermissionsAndGetActions(0, 1, 0); - expect(component.dropDownActions).toEqual([editAction, unprotectAction]); - }); - - it('with delete', () => { - permissionHelper.setPermissionsAndGetActions(0, 0, 1); - expect(component.dropDownActions).toEqual([deleteAction]); - }); - - it('without any', () => { - permissionHelper.setPermissionsAndGetActions(0, 0, 0); - expect(component.dropDownActions).toEqual([]); + 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: '' } + } }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts b/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts index 7f432d5d3c05b..767ee5e76e2d8 100644 --- a/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts +++ b/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts @@ -4,11 +4,12 @@ import { AbstractControl } from '@angular/forms'; import { By } from '@angular/platform-browser'; import { I18n } from '@ngx-translate/i18n-polyfill'; -import * as _ from 'lodash'; import { TableActionsComponent } from '../app/shared/datatable/table-actions/table-actions.component'; import { Icons } from '../app/shared/enum/icons.enum'; import { CdFormGroup } from '../app/shared/forms/cd-form-group'; +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 { AlertmanagerAlert, @@ -42,71 +43,74 @@ export function configureTestBed(configuration, useOldMethod?) { } export class PermissionHelper { - tableActions: TableActionsComponent; + tac: TableActionsComponent; permission: Permission; - getTableActionComponent: () => TableActionsComponent; - constructor(permission: Permission, getTableActionComponent: () => TableActionsComponent) { + constructor(permission: Permission) { this.permission = permission; - this.getTableActionComponent = getTableActionComponent; } - setPermissionsAndGetActions( - createPerm: number | boolean, - updatePerm: number | boolean, - deletePerm: number | boolean - ): TableActionsComponent { - this.permission.create = Boolean(createPerm); - this.permission.update = Boolean(updatePerm); - this.permission.delete = Boolean(deletePerm); - this.tableActions = this.getTableActionComponent(); - return this.tableActions; - } + setPermissionsAndGetActions(tableActions: CdTableAction[]): any { + const result = {}; + [true, false].forEach((create) => { + [true, false].forEach((update) => { + [true, false].forEach((deleteP) => { + this.permission.create = create; + this.permission.update = update; + this.permission.delete = deleteP; + + this.tac = new TableActionsComponent(); + this.tac.selection = new CdTableSelection(); + this.tac.tableActions = [...tableActions]; + this.tac.permission = this.permission; + this.tac.ngOnInit(); + + const perms = []; + if (create) { + perms.push('create'); + } + if (update) { + perms.push('update'); + } + if (deleteP) { + perms.push('delete'); + } + const permissionText = perms.join(','); + + result[permissionText !== '' ? permissionText : 'no-permissions'] = { + actions: this.tac.tableActions.map((action) => action.name), + primary: this.testScenarios() + }; + }); + }); + }); - // Overwrite if needed - createSelection(): object { - return {}; + return result; } - testScenarios({ - fn, - empty, - single, - singleExecuting, - multiple - }: { - fn: () => any; - empty: any; - single: any; - singleExecuting?: any; // uses 'single' if not defined - multiple?: any; // uses 'empty' if not defined - }) { - this.testScenario( - // 'multiple selections' - [this.createSelection(), this.createSelection()], - fn, - _.isUndefined(multiple) ? empty : multiple - ); - const executing = this.createSelection(); - executing['cdExecuting'] = 'someAction'; - this.testScenario( - // 'select executing item' - [executing], - fn, - _.isUndefined(singleExecuting) ? single : singleExecuting - ); - this.testScenario([this.createSelection()], fn, single); // 'select non-executing item' - this.testScenario([], fn, empty); // 'no selection' + testScenarios() { + const result: any = {}; + // 'multiple selections' + result.multiple = this.testScenario([{}, {}]); + // 'select executing item' + result.executing = this.testScenario([{ cdExecuting: 'someAction' }]); + // 'select non-executing item' + result.single = this.testScenario([{}]); + // 'no selection' + result.no = this.testScenario([]); + + return result; } - private testScenario(selection: object[], fn: () => any, expected: any) { + private testScenario(selection: object[]) { this.setSelection(selection); - expect(fn()).toBe(expected); + const btn = this.tac.getCurrentButton(); + return btn ? btn.name : ''; } setSelection(selection: object[]) { - this.tableActions.selection.selected = selection; - this.tableActions.selection.update(); + this.tac.selection.selected = selection; + this.tac.selection.update(); } }