Skip to content

Commit

Permalink
feat(menus): add "onAfterMenuShow" event to all possible menu plugins (
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Jan 31, 2020
1 parent a50d489 commit 141d01a
Show file tree
Hide file tree
Showing 16 changed files with 252 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const gridStub = {
const mockAddon = jest.fn().mockImplementation(() => ({
init: jest.fn(),
destroy: jest.fn(),
onAfterMenuShow: new Slick.Event(),
onBeforeMenuClose: new Slick.Event(),
onBeforeMenuShow: new Slick.Event(),
onColumnsChanged: new Slick.Event(),
Expand Down Expand Up @@ -68,10 +69,11 @@ describe('CellMenuExtension', () => {
maxHeight: 'none',
width: 'auto',
onExtensionRegistered: jest.fn(),
onCommand: (e, args: MenuCommandItemCallbackArgs) => { },
onBeforeMenuShow: (e, args: { cell: number; row: number; grid: any; }) => { },
onBeforeMenuClose: (e, args: { cell: number; row: number; grid: any; menu: any; }) => { },
onOptionSelected: (e, args: MenuOptionItemCallbackArgs) => { },
onCommand: () => { },
onAfterMenuShow: () => { },
onBeforeMenuShow: () => { },
onBeforeMenuClose: () => { },
onOptionSelected: () => { },
},
multiColumnSort: true,
pagination: {
Expand Down Expand Up @@ -160,6 +162,7 @@ describe('CellMenuExtension', () => {
onOptionSelected: expect.anything(),
onBeforeMenuClose: expect.anything(),
onBeforeMenuShow: expect.anything(),
onAfterMenuShow: expect.anything(),
onExtensionRegistered: expect.anything(),
});
expect(onRegisteredSpy).toHaveBeenCalledWith(instance);
Expand All @@ -170,13 +173,14 @@ describe('CellMenuExtension', () => {
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
const onb4CloseSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onBeforeMenuClose');
const onb4ShowSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onBeforeMenuShow');
const onAfterShowSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onAfterMenuShow');
const onCommandSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onCommand');
const onOptionSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onOptionSelected');

const instance = extension.register();
instance.onBeforeMenuShow.notify({ cell: 0, row: 0, grid: gridStub }, new Slick.EventData(), gridStub);

expect(handlerSpy).toHaveBeenCalledTimes(4);
expect(handlerSpy).toHaveBeenCalledTimes(5);
expect(handlerSpy).toHaveBeenCalledWith(
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
Expand All @@ -185,25 +189,51 @@ describe('CellMenuExtension', () => {
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onCommandSpy).not.toHaveBeenCalled();
expect(onOptionSpy).not.toHaveBeenCalled();
expect(onAfterShowSpy).not.toHaveBeenCalled();
});

it('should call internal event handler subscribe and expect the "onBeforeMenuClose" option to be called when addon notify is called', () => {
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
const onb4CloseSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onBeforeMenuClose');
const onb4ShowSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onBeforeMenuShow');
const onAfterShowSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onAfterMenuShow');
const onCommandSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onCommand');
const onOptionSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onOptionSelected');

const menuElm = document.createElement('div');
const instance = extension.register();
instance.onBeforeMenuClose.notify({ cell: 0, row: 0, grid: gridStub, menu: menuElm }, new Slick.EventData(), gridStub);

expect(handlerSpy).toHaveBeenCalledTimes(4);
expect(handlerSpy).toHaveBeenCalledTimes(5);
expect(handlerSpy).toHaveBeenCalledWith(
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
);
expect(onb4CloseSpy).toHaveBeenCalledWith(expect.anything(), { cell: 0, row: 0, grid: gridStub, menu: menuElm });
expect(onAfterShowSpy).not.toHaveBeenCalled();
expect(onb4ShowSpy).not.toHaveBeenCalled();
expect(onCommandSpy).not.toHaveBeenCalled();
expect(onOptionSpy).not.toHaveBeenCalled();
});

it('should call internal event handler subscribe and expect the "onAfterMenuShow" option to be called when addon notify is called', () => {
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
const onb4CloseSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onBeforeMenuClose');
const onb4ShowSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onBeforeMenuShow');
const onAfterShowSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onAfterMenuShow');
const onCommandSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onCommand');
const onOptionSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onOptionSelected');

const instance = extension.register();
instance.onAfterMenuShow.notify({ cell: 0, row: 0, grid: gridStub }, new Slick.EventData(), gridStub);

expect(handlerSpy).toHaveBeenCalledTimes(5);
expect(handlerSpy).toHaveBeenCalledWith(
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
);
expect(onAfterShowSpy).toHaveBeenCalledWith(expect.anything(), { cell: 0, row: 0, grid: gridStub });
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onb4ShowSpy).not.toHaveBeenCalled();
expect(onCommandSpy).not.toHaveBeenCalled();
expect(onOptionSpy).not.toHaveBeenCalled();
Expand All @@ -213,13 +243,14 @@ describe('CellMenuExtension', () => {
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
const onb4CloseSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onBeforeMenuClose');
const onb4ShowSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onBeforeMenuShow');
const onAfterShowSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onAfterMenuShow');
const onCommandSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onCommand');
const onOptionSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onOptionSelected');

const instance = extension.register();
instance.onCommand.notify({ grid: gridStub, command: 'help' }, new Slick.EventData(), gridStub);

expect(handlerSpy).toHaveBeenCalledTimes(4);
expect(handlerSpy).toHaveBeenCalledTimes(5);
expect(handlerSpy).toHaveBeenCalledWith(
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
Expand All @@ -228,19 +259,21 @@ describe('CellMenuExtension', () => {
expect(onOptionSpy).not.toHaveBeenCalled();
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onb4ShowSpy).not.toHaveBeenCalled();
expect(onAfterShowSpy).not.toHaveBeenCalled();
});

it('should call internal event handler subscribe and expect the "onOptionSelected" option to be called when addon notify is called', () => {
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
const onb4CloseSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onBeforeMenuClose');
const onb4ShowSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onBeforeMenuShow');
const onAfterShowSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onAfterMenuShow');
const onCommandSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onCommand');
const onOptionSpy = jest.spyOn(SharedService.prototype.gridOptions.cellMenu, 'onOptionSelected');

const instance = extension.register();
instance.onOptionSelected.notify({ grid: gridStub, command: 'help' }, new Slick.EventData(), gridStub);

expect(handlerSpy).toHaveBeenCalledTimes(4);
expect(handlerSpy).toHaveBeenCalledTimes(5);
expect(handlerSpy).toHaveBeenCalledWith(
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
Expand All @@ -249,6 +282,7 @@ describe('CellMenuExtension', () => {
expect(onCommandSpy).not.toHaveBeenCalled();
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onb4ShowSpy).not.toHaveBeenCalled();
expect(onAfterShowSpy).not.toHaveBeenCalled();
});

it('should dispose of the addon', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const mockAddon = jest.fn().mockImplementation(() => ({
setOptions: jest.fn(),
onBeforeMenuClose: new Slick.Event(),
onBeforeMenuShow: new Slick.Event(),
onAfterMenuShow: new Slick.Event(),
onColumnsChanged: new Slick.Event(),
onCommand: new Slick.Event(),
onOptionSelected: new Slick.Event(),
Expand Down Expand Up @@ -92,10 +93,11 @@ describe('contextMenuExtension', () => {
hideMenuOnScroll: true,
hideOptionSection: false,
onExtensionRegistered: jest.fn(),
onCommand: (e, args: MenuCommandItemCallbackArgs) => { },
onBeforeMenuShow: (e, args: { cell: number; row: number; grid: any; }) => { },
onBeforeMenuClose: (e, args: { cell: number; row: number; grid: any; menu: any; }) => { },
onOptionSelected: (e, args: MenuOptionItemCallbackArgs) => { },
onCommand: () => { },
onBeforeMenuShow: () => { },
onBeforeMenuClose: () => { },
onAfterMenuShow: () => { },
onOptionSelected: () => { },
},
pagination: {
totalItems: 0
Expand Down Expand Up @@ -215,6 +217,7 @@ describe('contextMenuExtension', () => {
onOptionSelected: expect.anything(),
onBeforeMenuClose: expect.anything(),
onBeforeMenuShow: expect.anything(),
onAfterMenuShow: expect.anything(),
onExtensionRegistered: expect.anything(),
});
});
Expand All @@ -223,13 +226,14 @@ describe('contextMenuExtension', () => {
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
const onb4CloseSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onBeforeMenuClose');
const onb4ShowSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onBeforeMenuShow');
const onAfterShowSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onAfterMenuShow');
const onCommandSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onCommand');
const onOptionSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onOptionSelected');

const instance = extension.register();
instance.onBeforeMenuShow.notify({ cell: 0, row: 0, grid: gridStub }, new Slick.EventData(), gridStub);

expect(handlerSpy).toHaveBeenCalledTimes(4);
expect(handlerSpy).toHaveBeenCalledTimes(5);
expect(handlerSpy).toHaveBeenCalledWith(
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
Expand All @@ -238,20 +242,22 @@ describe('contextMenuExtension', () => {
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onCommandSpy).not.toHaveBeenCalled();
expect(onOptionSpy).not.toHaveBeenCalled();
expect(onAfterShowSpy).not.toHaveBeenCalled();
});

it('should call internal event handler subscribe and expect the "onBeforeMenuClose" option to be called when addon notify is called', () => {
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
const onb4CloseSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onBeforeMenuClose');
const onb4ShowSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onBeforeMenuShow');
const onAfterShowSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onAfterMenuShow');
const onCommandSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onCommand');
const onOptionSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onOptionSelected');

const menuElm = document.createElement('div');
const instance = extension.register();
instance.onBeforeMenuClose.notify({ cell: 0, row: 0, grid: gridStub, menu: menuElm }, new Slick.EventData(), gridStub);

expect(handlerSpy).toHaveBeenCalledTimes(4);
expect(handlerSpy).toHaveBeenCalledTimes(5);
expect(handlerSpy).toHaveBeenCalledWith(
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
Expand All @@ -260,19 +266,44 @@ describe('contextMenuExtension', () => {
expect(onb4ShowSpy).not.toHaveBeenCalled();
expect(onCommandSpy).not.toHaveBeenCalled();
expect(onOptionSpy).not.toHaveBeenCalled();
expect(onAfterShowSpy).not.toHaveBeenCalled();
});

it('should call internal event handler subscribe and expect the "onAfterMenuShow" option to be called when addon notify is called', () => {
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
const onb4CloseSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onBeforeMenuClose');
const onb4ShowSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onBeforeMenuShow');
const onAfterShowSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onAfterMenuShow');
const onCommandSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onCommand');
const onOptionSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onOptionSelected');

const instance = extension.register();
instance.onAfterMenuShow.notify({ cell: 0, row: 0, grid: gridStub }, new Slick.EventData(), gridStub);

expect(handlerSpy).toHaveBeenCalledTimes(5);
expect(handlerSpy).toHaveBeenCalledWith(
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
);
expect(onAfterShowSpy).toHaveBeenCalledWith(expect.anything(), { cell: 0, row: 0, grid: gridStub });
expect(onb4ShowSpy).not.toHaveBeenCalled();
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onCommandSpy).not.toHaveBeenCalled();
expect(onOptionSpy).not.toHaveBeenCalled();
});

it('should call internal event handler subscribe and expect the "onCommand" option to be called when addon notify is called', () => {
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
const onb4CloseSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onBeforeMenuClose');
const onb4ShowSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onBeforeMenuShow');
const onAfterShowSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onAfterMenuShow');
const onCommandSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onCommand');
const onOptionSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onOptionSelected');

const instance = extension.register();
instance.onCommand.notify({ grid: gridStub, command: 'help' }, new Slick.EventData(), gridStub);

expect(handlerSpy).toHaveBeenCalledTimes(4);
expect(handlerSpy).toHaveBeenCalledTimes(5);
expect(handlerSpy).toHaveBeenCalledWith(
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
Expand All @@ -281,19 +312,21 @@ describe('contextMenuExtension', () => {
expect(onOptionSpy).not.toHaveBeenCalled();
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onb4ShowSpy).not.toHaveBeenCalled();
expect(onAfterShowSpy).not.toHaveBeenCalled();
});

it('should call internal event handler subscribe and expect the "onOptionSelected" option to be called when addon notify is called', () => {
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
const onb4CloseSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onBeforeMenuClose');
const onb4ShowSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onBeforeMenuShow');
const onAfterShowSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onAfterMenuShow');
const onCommandSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onCommand');
const onOptionSpy = jest.spyOn(SharedService.prototype.gridOptions.contextMenu, 'onOptionSelected');

const instance = extension.register();
instance.onOptionSelected.notify({ grid: gridStub, command: 'help' }, new Slick.EventData(), gridStub);

expect(handlerSpy).toHaveBeenCalledTimes(4);
expect(handlerSpy).toHaveBeenCalledTimes(5);
expect(handlerSpy).toHaveBeenCalledWith(
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
Expand All @@ -302,6 +335,7 @@ describe('contextMenuExtension', () => {
expect(onCommandSpy).not.toHaveBeenCalled();
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onb4ShowSpy).not.toHaveBeenCalled();
expect(onAfterShowSpy).not.toHaveBeenCalled();
});

it('should dispose of the addon', () => {
Expand Down

0 comments on commit 141d01a

Please sign in to comment.