Skip to content

Commit

Permalink
ACS-8706 add context menu service, effects and directive unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
g-jaskowski committed Oct 28, 2024
1 parent 685f48d commit c14a411
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,25 @@ import { ContextMenuService } from './context-menu.service';
import { TranslateModule } from '@ngx-translate/core';
import { ContextMenuComponent } from './context-menu.component';
import { ContextmenuOverlayConfig } from './interfaces';
import { ContentActionRef, ContentActionType } from '@alfresco/adf-extensions';

describe('ContextMenuService', () => {
let contextMenuService: ContextMenuService;
let overlay: Overlay;
let injector: Injector;
let userPreferencesService: UserPreferencesService;

const customActionMock: ContentActionRef[] = [
{
type: ContentActionType.default,
id: 'action',
title: 'action',
actions: {
click: 'event'
}
}
];

const overlayConfig: ContextmenuOverlayConfig = {
hasBackdrop: false,
backdropClass: '',
Expand Down Expand Up @@ -93,4 +105,20 @@ describe('ContextMenuService', () => {

expect(document.body.querySelector('div[dir="rtl"]')).not.toBe(null);
});

it('should render custom context menu component', () => {
contextMenuService = new ContextMenuService(injector, overlay, userPreferencesService);

contextMenuService.open(overlayConfig, customActionMock);

expect(document.querySelector('aca-custom-context-menu')).not.toBe(null);
});

it('should not render custom context menu when no custom actions are provided', () => {
contextMenuService = new ContextMenuService(injector, overlay, userPreferencesService);

contextMenuService.open(overlayConfig, []);

expect(document.querySelector('aca-custom-context-menu')).toBe(null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,22 @@ import { AppTestingModule } from '../../testing/app-testing.module';
import { ContextMenuEffects } from './contextmenu.effects';
import { EffectsModule } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { ContextMenu } from '@alfresco/aca-shared/store';
import { ContextMenu, CustomContextMenu } from '@alfresco/aca-shared/store';
import { ContextMenuService } from '../../components/context-menu/context-menu.service';
import { OverlayModule, OverlayRef } from '@angular/cdk/overlay';
import { ContextMenuOverlayRef } from '../../components/context-menu/context-menu-overlay';
import { ContentActionRef, ContentActionType } from '@alfresco/adf-extensions';

const actionPayloadMock: ContentActionRef[] = [
{
type: ContentActionType.default,
id: 'action',
title: 'action',
actions: {
click: 'event'
}
}
];

describe('ContextMenuEffects', () => {
let store: Store<any>;
Expand Down Expand Up @@ -62,4 +74,22 @@ describe('ContextMenuEffects', () => {
store.dispatch(new ContextMenu(new MouseEvent('click')));
expect(overlayRefMock.close).toHaveBeenCalled();
});

it('should open custom context menu on customContextMenu$ action', () => {
store.dispatch(new CustomContextMenu(new MouseEvent('click'), actionPayloadMock));
expect(contextMenuService.open).toHaveBeenCalled();
});

it('should not open custom context menu on customContextMenu$ action if no action provided', () => {
store.dispatch(new CustomContextMenu(new MouseEvent('click'), []));
expect(contextMenuService.open).not.toHaveBeenCalled();
});

it('should close custom context menu if a new one is opened', () => {
store.dispatch(new CustomContextMenu(new MouseEvent('click'), actionPayloadMock));
expect(contextMenuService.open).toHaveBeenCalled();

store.dispatch(new CustomContextMenu(new MouseEvent('click'), actionPayloadMock));
expect(overlayRefMock.close).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@
*/

import { ContextActionsDirective } from './contextmenu.directive';
import { ContextMenu } from '@alfresco/aca-shared/store';
import { ContextMenu, CustomContextMenu } from '@alfresco/aca-shared/store';
import { ContentActionRef, ContentActionType } from '@alfresco/adf-extensions';
import { fakeAsync, tick } from '@angular/core/testing';

const customActionsMock: ContentActionRef[] = [
{
type: ContentActionType.default,
id: 'action',
title: 'action',
actions: {
click: 'event'
}
}
];

describe('ContextActionsDirective', () => {
let directive: ContextActionsDirective;

Expand All @@ -45,24 +57,6 @@ describe('ContextActionsDirective', () => {
expect(directive.execute).not.toHaveBeenCalled();
});

it('should call service to render context menu', fakeAsync(() => {
const el = document.createElement('div');
el.className = 'adf-datatable-cell adf-datatable-cell--text adf-datatable-row';

const fragment = document.createDocumentFragment();
fragment.appendChild(el);
const target = fragment.querySelector('div');
const mouseEventMock: any = { preventDefault: () => {}, target };

directive.ngOnInit();

directive.onContextMenuEvent(mouseEventMock);

tick(500);

expect(storeMock.dispatch).toHaveBeenCalledWith(new ContextMenu(mouseEventMock));
}));

it('should not call service to render context menu if the datatable is empty', fakeAsync(() => {
storeMock.dispatch.calls.reset();
const el = document.createElement('div');
Expand All @@ -82,4 +76,34 @@ describe('ContextActionsDirective', () => {

expect(storeMock.dispatch).not.toHaveBeenCalled();
}));

describe('Context Menu rendering', () => {
let mouseEventMock: any;
beforeEach(() => {
const el = document.createElement('div');
el.className = 'adf-datatable-cell adf-datatable-cell--text adf-datatable-row';

const fragment = document.createDocumentFragment();
fragment.appendChild(el);
const target = fragment.querySelector('div');
mouseEventMock = { preventDefault: () => {}, target };
});

it('should call service to render context menu', fakeAsync(() => {
directive.ngOnInit();
directive.onContextMenuEvent(mouseEventMock);
tick(500);

expect(storeMock.dispatch).toHaveBeenCalledWith(new ContextMenu(mouseEventMock));
}));

it('should call service to render custom context menu if custom actions are provided', fakeAsync(() => {
directive.customActions = customActionsMock;
directive.ngOnInit();
directive.onContextMenuEvent(mouseEventMock);
tick(500);

expect(storeMock.dispatch).toHaveBeenCalledWith(new CustomContextMenu(mouseEventMock, customActionsMock));
}));
});
});

0 comments on commit c14a411

Please sign in to comment.