1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { ToastModule } from 'ng2-toastr';
7 import { TabsModule } from 'ngx-bootstrap/tabs';
13 } from '../../../../testing/unit-test-helper';
14 import { ActionLabels } from '../../../shared/constants/app.constants';
15 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
16 import { SharedModule } from '../../../shared/shared.module';
17 import { UserTabsComponent } from '../user-tabs/user-tabs.component';
18 import { UserListComponent } from './user-list.component';
20 describe('UserListComponent', () => {
21 let component: UserListComponent;
22 let fixture: ComponentFixture<UserListComponent>;
27 ToastModule.forRoot(),
30 HttpClientTestingModule
32 declarations: [UserListComponent, UserTabsComponent],
33 providers: i18nProviders
37 fixture = TestBed.createComponent(UserListComponent);
38 component = fixture.componentInstance;
41 it('should create', () => {
42 fixture.detectChanges();
43 expect(component).toBeTruthy();
46 describe('show action buttons and drop down actions depending on permissions', () => {
47 let tableActions: TableActionsComponent;
48 let scenario: { fn; empty; single };
49 let permissionHelper: PermissionHelper;
51 const getTableActionComponent = (): TableActionsComponent => {
52 fixture.detectChanges();
53 return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance;
57 permissionHelper = new PermissionHelper(component.permission, () =>
58 getTableActionComponent()
61 fn: () => tableActions.getCurrentButton().name,
62 single: ActionLabels.EDIT,
63 empty: ActionLabels.CREATE
67 describe('with all', () => {
69 tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1);
72 it(`shows 'Edit' for single selection else 'Add' as main action`, () =>
73 permissionHelper.testScenarios(scenario));
75 it('shows all actions', () => {
76 expect(tableActions.tableActions.length).toBe(3);
77 expect(tableActions.tableActions).toEqual(component.tableActions);
81 describe('with read, create and update', () => {
83 tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0);
86 it(`shows 'Edit' for single selection else 'Add' as main action`, () =>
87 permissionHelper.testScenarios(scenario));
89 it(`shows 'Add' and 'Edit' action`, () => {
90 expect(tableActions.tableActions.length).toBe(2);
91 component.tableActions.pop();
92 expect(tableActions.tableActions).toEqual(component.tableActions);
96 describe('with read, create and delete', () => {
98 tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1);
101 it(`shows 'Delete' for single selection else 'Add' as main action`, () => {
102 scenario.single = 'Delete';
103 permissionHelper.testScenarios(scenario);
106 it(`shows 'Add' and 'Delete' action`, () => {
107 expect(tableActions.tableActions.length).toBe(2);
108 expect(tableActions.tableActions).toEqual([
109 component.tableActions[0],
110 component.tableActions[2]
115 describe('with read, edit and delete', () => {
117 tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1);
120 it(`shows always 'Edit' as main action`, () => {
121 scenario.empty = ActionLabels.EDIT;
122 permissionHelper.testScenarios(scenario);
125 it(`shows 'Edit' and 'Delete' action`, () => {
126 expect(tableActions.tableActions.length).toBe(2);
127 expect(tableActions.tableActions).toEqual([
128 component.tableActions[1],
129 component.tableActions[2]
134 describe('with read and create', () => {
136 tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0);
139 it(`shows always 'Add' as main action`, () => {
140 scenario.single = ActionLabels.CREATE;
141 permissionHelper.testScenarios(scenario);
144 it(`shows only 'Add' action`, () => {
145 expect(tableActions.tableActions.length).toBe(1);
146 expect(tableActions.tableActions).toEqual([component.tableActions[0]]);
150 describe('with read and update', () => {
152 tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0);
155 it(`shows always 'Edit' as main action`, () => {
156 scenario.empty = 'Edit';
157 permissionHelper.testScenarios(scenario);
160 it(`shows only 'Edit' action`, () => {
161 expect(tableActions.tableActions.length).toBe(1);
162 expect(tableActions.tableActions).toEqual([component.tableActions[1]]);
166 describe('with read and delete', () => {
168 tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1);
171 it(`shows always 'Delete' as main action`, () => {
172 scenario.single = 'Delete';
173 scenario.empty = 'Delete';
174 permissionHelper.testScenarios(scenario);
177 it(`shows only 'Delete' action`, () => {
178 expect(tableActions.tableActions.length).toBe(1);
179 expect(tableActions.tableActions).toEqual([component.tableActions[2]]);
183 describe('with only read', () => {
185 tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0);
188 it('shows no main action', () => {
189 permissionHelper.testScenarios({
190 fn: () => tableActions.getCurrentButton(),
196 it('shows no actions', () => {
197 expect(tableActions.tableActions.length).toBe(0);
198 expect(tableActions.tableActions).toEqual([]);