Skip to content

Commit

Permalink
* Added unit tests to the contentmenu if flag set to false
Browse files Browse the repository at this point in the history
  • Loading branch information
sivakumar414ram committed Apr 22, 2020
1 parent ce52e88 commit 7233536
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ describe('Process List: Custom EmptyTemplateComponent', () => {
template: `
<adf-process-instance-list
[appId]="appId"
[showContextMenu]="true"
[showContextMenu]="showContextMenu"
(showRowContextMenu)="onShowRowContextMenu($event)"
#processlistComponentInstance>
<data-columns>
Expand All @@ -549,10 +549,14 @@ describe('Process List: Custom EmptyTemplateComponent', () => {

class ProcessListContextMenuComponent implements OnInit {

appId: number;
@ViewChild('processlistComponentInstance')
processlistComponentInstance: ProcessInstanceListComponent;

@Output()
contextAction = new EventEmitter<any>();
private performAction$ = new Subject<any>();
showContextMenu = true;
appId: number;

ngOnInit() {
this.performContextActions();
Expand Down Expand Up @@ -598,6 +602,8 @@ describe('ProcessListContextMenuComponent', () => {
let customComponent: ProcessListContextMenuComponent;
let processService: ProcessService;
let element: HTMLElement;
let escapeEvent: KeyboardEvent;
let overlayContainer: HTMLElement;

setupTestBed({
imports: [CoreModule.forRoot()],
Expand All @@ -612,30 +618,53 @@ describe('ProcessListContextMenuComponent', () => {
processService = TestBed.get(ProcessService);
customComponent.appId = 12345;
spyOn(processService, 'getProcesses').and.returnValue(of(fakeProcessInstance));
fixture.detectChanges();
});

afterEach(() => {
const event = new KeyboardEvent('keydown', {
bubbles : true, cancelable : true, key : 'Escape'
});
document.querySelector('.cdk-overlay-backdrop').dispatchEvent(event);
escapeEvent = new KeyboardEvent('keydown', { bubbles : true, cancelable : true, key : 'Escape'});
overlayContainer = document.querySelector('.cdk-overlay-backdrop');
overlayContainer.dispatchEvent(escapeEvent);
fixture.detectChanges();
});

it('Should be able to show context menu on process list', async () => {
it('Should be able to show context menu on process list if showContextMenu set to true', async () => {
customComponent.showContextMenu = true;
fixture.detectChanges();
const contextMenu = element.querySelector(`[data-automation-id="text_${fakeProcessInstance.data[0].name}"]`);
const contextActionSpy = spyOn(customComponent.contextAction, 'emit').and.callThrough();
contextMenu.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true }));
fixture.detectChanges();
await fixture.whenStable();

expect(customComponent.showContextMenu).toBe(true);
expect(customComponent.processlistComponentInstance.showContextMenu).toBe(true);

const adfContextMenu = document.querySelector('.adf-context-menu');
const contextActions = document.querySelectorAll('.mat-menu-item');

expect(adfContextMenu).not.toBeNull();
expect(contextActions.length).toBe(2);
expect(contextActions[0]['disabled']).toBe(false, 'View Process Details action not enabled');
expect(contextActions[1]['disabled']).toBe(false, 'Cancel Process action not enabled');
contextActions[0].dispatchEvent(new Event('click'));
fixture.detectChanges();
expect(contextActionSpy).toHaveBeenCalled();
});
});

it('Should not be able to show context menu on process list if showContextMenu set to false', async () => {
customComponent.showContextMenu = false;
fixture.detectChanges();
const contextMenu = element.querySelector(`[data-automation-id="text_${fakeProcessInstance.data[0].name}"]`);
contextMenu.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true }));
fixture.detectChanges();
await fixture.whenStable();

expect(customComponent.showContextMenu).toBe(false);
expect(customComponent.processlistComponentInstance.showContextMenu).toBe(false);

const adfContextMenu = document.querySelector('.adf-context-menu');
const contextActions = document.querySelectorAll('.mat-menu-item');
expect(adfContextMenu).toBeNull();
expect(contextActions.length).toBe(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ describe('Task List: Custom EmptyTemplateComponent', () => {
@Component({
template: `
<adf-tasklist
[showContextMenu]="true"
[showContextMenu]="showContextMenu"
(showRowContextMenu)="onShowRowContextMenu($event)"
#taskList>
<data-columns>
Expand All @@ -828,9 +828,13 @@ describe('Task List: Custom EmptyTemplateComponent', () => {

class TaskListContextMenuComponent implements OnInit {

@ViewChild('taskList')
taskList: TaskListComponent;

@Output()
contextAction = new EventEmitter<any>();
private performAction$ = new Subject<any>();
showContextMenu = true;

ngOnInit() {
this.performContextActions();
Expand Down Expand Up @@ -876,6 +880,8 @@ describe('TaskListContextMenuComponent', () => {
let customComponent: TaskListContextMenuComponent;
let taskListService: TaskListService;
let element: HTMLElement;
let escapeEvent: KeyboardEvent;
let overlayContainer: HTMLElement;

setupTestBed({
imports: [CoreModule.forRoot()],
Expand All @@ -889,30 +895,54 @@ describe('TaskListContextMenuComponent', () => {
element = fixture.nativeElement;
taskListService = TestBed.get(TaskListService);
spyOn(taskListService, 'findTasksByState').and.returnValues(of(fakeGlobalTask));
fixture.detectChanges();
});

afterEach(() => {
const event = new KeyboardEvent('keydown', {
bubbles : true, cancelable : true, key : 'Escape'
});
document.querySelector('.cdk-overlay-backdrop').dispatchEvent(event);
escapeEvent = new KeyboardEvent('keydown', { bubbles : true, cancelable : true, key : 'Escape'});
overlayContainer = document.querySelector('.cdk-overlay-backdrop');
overlayContainer.dispatchEvent(escapeEvent);
fixture.detectChanges();
});

it('Should be able to show context menu on task list', async () => {
it('Should be able to show context menu on task list if showContextMenu set to true', async () => {
customComponent.showContextMenu = true;
fixture.detectChanges();

const contextMenu = element.querySelector(`[data-automation-id="text_${fakeGlobalTask.data[0].name}"]`);
const contextActionSpy = spyOn(customComponent.contextAction, 'emit').and.callThrough();
contextMenu.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true }));

fixture.detectChanges();
await fixture.whenStable();

const adfContextMenu = document.querySelector('.adf-context-menu');
const contextActions = document.querySelectorAll('.mat-menu-item');

expect(customComponent.taskList.showContextMenu).toBe(true);

expect(adfContextMenu).not.toBeNull();
expect(contextActions.length).toBe(2);
expect(contextActions[0]['disabled']).toBe(false, 'View Task Details action not enabled');
expect(contextActions[1]['disabled']).toBe(false, 'Cancel Task action not enabled');
contextActions[0].dispatchEvent(new Event('click'));
fixture.detectChanges();
expect(contextActionSpy).toHaveBeenCalled();
});
});

it('Should not be able to show context menu on task list if showContextMenu set to false', async () => {
customComponent.showContextMenu = false;
fixture.detectChanges();
const contextMenu = element.querySelector(`[data-automation-id="text_${fakeGlobalTask.data[0].name}"]`);
contextMenu.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true }));
fixture.detectChanges();
await fixture.whenStable();

expect(customComponent.showContextMenu).toBe(false);
expect(customComponent.taskList.showContextMenu).toBe(false);

const adfContextMenu = document.querySelector('.adf-context-menu');
const contextActions = document.querySelectorAll('.mat-menu-item');
expect(adfContextMenu).toBeNull();
expect(contextActions.length).toBe(0);
});
});

0 comments on commit 7233536

Please sign in to comment.