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';
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';
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: '' }
+ }
});
});
});