Skip to content

Commit

Permalink
fix(core): use regular imports instead of require to load plugins (#209)
Browse files Browse the repository at this point in the history
* fix(core): use regular imports instead of require to load plugins
  • Loading branch information
ghiscoding authored Dec 22, 2020
1 parent 3b3b463 commit 6816696
Show file tree
Hide file tree
Showing 28 changed files with 88 additions and 288 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { GridOption, SlickAutoTooltips, SlickGrid, SlickNamespace } from '../../interfaces/index';
import { AutoTooltipExtension } from '../autoTooltipExtension';
import { ExtensionUtility } from '../extensionUtility';
import { SharedService } from '../../services/shared.service';
import { TranslateServiceStub } from '../../../../../test/translateServiceStub';

declare const Slick: SlickNamespace;

Expand All @@ -16,21 +14,17 @@ const mockAddon = jest.fn().mockImplementation(() => ({
destroy: jest.fn()
}));

jest.mock('slickgrid/plugins/slick.autotooltips', () => mockAddon);
Slick.AutoTooltips = mockAddon;

describe('autoTooltipExtension', () => {
jest.mock('slickgrid/plugins/slick.autotooltips', () => mockAddon);
Slick.AutoTooltips = mockAddon;

let extension: AutoTooltipExtension;
let extensionUtility: ExtensionUtility;
let sharedService: SharedService;
let translateService: TranslateServiceStub;
const gridOptionsMock = { enableAutoTooltip: true } as GridOption;

beforeEach(() => {
sharedService = new SharedService();
translateService = new TranslateServiceStub();
extensionUtility = new ExtensionUtility(sharedService, translateService);
extension = new AutoTooltipExtension(extensionUtility, sharedService);
extension = new AutoTooltipExtension(sharedService);
});

it('should return null when either the grid object or the grid options is missing', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ const mockAddon = jest.fn().mockImplementation(() => ({
onOptionSelected: new Slick.Event(),
}));

jest.mock('slickgrid/plugins/slick.cellmenu', () => mockAddon);
Slick.Plugins = {
CellMenu: mockAddon
} as any;

describe('CellMenuExtension', () => {
jest.mock('slickgrid/plugins/slick.cellmenu', () => mockAddon);
Slick.Plugins = {
CellMenu: mockAddon
} as any;

const columnsMock: Column[] = [{ id: 'field1', field: 'field1', width: 100, nameKey: 'TITLE' }, { id: 'field2', field: 'field2', width: 75 }];
let extensionUtility: ExtensionUtility;
let translateService: TranslateServiceStub;
Expand Down Expand Up @@ -226,7 +226,7 @@ describe('CellMenuExtension', () => {
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
);
expect(onCommandSpy).toHaveBeenCalledWith(expect.anything(), { item: { command: 'help' }, column: {}, grid: gridStub, command: 'help' });
expect(onCommandSpy).toHaveBeenCalledWith(expect.anything(), { item: { command: 'help' }, column: {} as Column, grid: gridStub, command: 'help' });
expect(onOptionSpy).not.toHaveBeenCalled();
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onb4ShowSpy).not.toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ const mockSelectionModel = jest.fn().mockImplementation(() => ({
onSelectedRangesChanged: new Slick.Event(),
} as SlickRowSelectionModel));

jest.mock('slickgrid/plugins/slick.checkboxselectcolumn', () => mockAddon);
Slick.CheckboxSelectColumn = mockAddon;

jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockSelectionModel);
Slick.RowSelectionModel = mockSelectionModel;

describe('checkboxSelectorExtension', () => {
jest.mock('slickgrid/plugins/slick.checkboxselectcolumn', () => mockAddon);
Slick.CheckboxSelectColumn = mockAddon;

jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockSelectionModel);
Slick.RowSelectionModel = mockSelectionModel;

let extension: CheckboxSelectorExtension;
let extensionUtility: ExtensionUtility;
let sharedService: SharedService;
Expand All @@ -51,7 +52,7 @@ describe('checkboxSelectorExtension', () => {
sharedService = new SharedService();
translateService = new TranslateServiceStub();
extensionUtility = new ExtensionUtility(sharedService, translateService);
extension = new CheckboxSelectorExtension(extensionUtility, sharedService);
extension = new CheckboxSelectorExtension(sharedService);
});

it('should return null after calling "create" method when either the column definitions or the grid options is missing', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ const mockAddon = jest.fn().mockImplementation(() => ({
onColumnsChanged: new Slick.Event(),
}));

jest.mock('slickgrid/controls/slick.columnpicker', () => mockAddon);
Slick.Controls = {
ColumnPicker: mockAddon
} as any;

describe('columnPickerExtension', () => {
jest.mock('slickgrid/controls/slick.columnpicker', () => mockAddon);
Slick.Controls = {
ColumnPicker: mockAddon
} as any;

const columnsMock: Column[] = [{ id: 'field1', field: 'field1', width: 100, nameKey: 'TITLE' }, { id: 'field2', field: 'field2', width: 75 }];
let extensionUtility: ExtensionUtility;
let extension: ColumnPickerExtension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ const mockAddon = jest.fn().mockImplementation(() => ({
onOptionSelected: new Slick.Event(),
}));

jest.mock('slickgrid/plugins/slick.contextmenu', () => mockAddon);
Slick.Plugins = {
ContextMenu: mockAddon
} as any;

describe('contextMenuExtension', () => {
jest.mock('slickgrid/plugins/slick.contextmenu', () => mockAddon);
Slick.Plugins = {
ContextMenu: mockAddon
} as any;

const columnsMock: Column[] = [{ id: 'field1', field: 'field1', width: 100, nameKey: 'TITLE' }, { id: 'field2', field: 'field2', width: 75 }];
let extensionUtility: ExtensionUtility;
let translateService: TranslateServiceStub;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const mockAddon = jest.fn().mockImplementation(() => ({
onGroupChanged: new Slick.Event(),
}));

jest.mock('slickgrid/plugins/slick.draggablegrouping', () => mockAddon);
Slick.DraggableGrouping = mockAddon;

describe('draggableGroupingExtension', () => {
jest.mock('slickgrid/plugins/slick.draggablegrouping', () => mockAddon);
Slick.DraggableGrouping = mockAddon;

let extensionUtility: ExtensionUtility;
let sharedService: SharedService;
let extension: DraggableGroupingExtension;
Expand Down
93 changes: 0 additions & 93 deletions packages/common/src/extensions/__tests__/extensionUtility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,99 +65,6 @@ describe('extensionUtility', () => {
await translateService.use('fr');
});

describe('loadExtensionDynamically method', () => {
it('should check that autoTooltip gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.autoTooltip);
const autoTooltip = new Slick.AutoTooltips();
expect(mockAddon).toHaveBeenCalled();
expect(autoTooltip).not.toBeNull();
});

it('should check that cellExternalCopyManager gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.cellExternalCopyManager);
const autoTooltip = new Slick.Plugins.CellExternalCopyManager();
expect(mockAddon).toHaveBeenCalled();
expect(autoTooltip).not.toBeNull();
});

it('should check that cellMenu gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.cellMenu);
const cellMenu = new Slick.Plugins.CellMenu();
expect(mockAddon).toHaveBeenCalled();
expect(cellMenu).not.toBeNull();
});

it('should check that columnPicker gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.columnPicker);
const columnPicker = new Slick.Controls.ColumnPicker();
expect(mockAddon).toHaveBeenCalled();
expect(columnPicker).not.toBeNull();
});

it('should check that contextMenu gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.contextMenu);
const contextMenu = new Slick.Plugins.ContextMenu();
expect(mockAddon).toHaveBeenCalled();
expect(contextMenu).not.toBeNull();
});

it('should check that draggableGrouping gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.draggableGrouping);
const draggableGrouping = new Slick.DraggableGrouping();
expect(mockAddon).toHaveBeenCalled();
expect(draggableGrouping).not.toBeNull();
});

it('should check that gridMenu gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.gridMenu);
const gridMenu = new Slick.Controls.GridMenu();
expect(mockAddon).toHaveBeenCalled();
expect(gridMenu).not.toBeNull();
});

it('should check that groupItemMetaProvider gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.groupItemMetaProvider);
const groupItemMetadataProvider = new Slick.Data.GroupItemMetadataProvider();
expect(mockAddon).toHaveBeenCalled();
expect(groupItemMetadataProvider).not.toBeNull();
});

it('should check that headerButton gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.headerButton);
const headerButton = new Slick.Plugins.HeaderButtons();
expect(mockAddon).toHaveBeenCalled();
expect(headerButton).not.toBeNull();
});

it('should check that headerMenu gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.headerMenu);
const headerMenu = new Slick.Plugins.HeaderMenu();
expect(mockAddon).toHaveBeenCalled();
expect(headerMenu).not.toBeNull();
});

it('should check that rowSelection gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.rowSelection);
const rowSelection = new Slick.RowSelectionModel();
expect(mockAddon).toHaveBeenCalled();
expect(rowSelection).not.toBeNull();
});

it('should check that rowDetail gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.rowDetailView);
const rowDetailView = new Slick.Plugins.RowDetailView();
expect(mockAddon).toHaveBeenCalled();
expect(rowDetailView).not.toBeNull();
});

it('should check that rowMoveManager gets loaded', () => {
utility.loadExtensionDynamically(ExtensionName.rowMoveManager);
const rowMoveManager = new Slick.RowMoveManager();
expect(mockAddon).toHaveBeenCalled();
expect(rowMoveManager).not.toBeNull();
});
});

describe('getPickerTitleOutputString method', () => {
it('should translate titleKey when there is one', () => {
const gridOptionsMock = { enableTranslate: true, enableGridMenu: true, gridMenu: { hideForceFitButton: false, hideSyncResizeButton: true, columnTitleKey: 'TITLE' } } as GridOption;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ const mockGridMenuAddon = {
};
const mockAddon = jest.fn().mockImplementation(() => mockGridMenuAddon);

jest.mock('slickgrid/controls/slick.gridmenu', () => mockAddon);
Slick.Controls = {
GridMenu: mockAddon
} as any;

// define a <div> container to simulate the grid container
const template =
`<div id="${containerId}" style="height: 800px; width: 600px;">
Expand All @@ -76,6 +71,9 @@ const template =
</div>`;

describe('gridMenuExtension', () => {
jest.mock('slickgrid/controls/slick.gridmenu', () => mockAddon);
Slick.Controls = { GridMenu: mockAddon } as any;

const columnsMock: Column[] = [{ id: 'field1', field: 'field1', width: 100, nameKey: 'TITLE' }, { id: 'field2', field: 'field2', width: 75 }];
let divElement: HTMLDivElement;
let extensionUtility: ExtensionUtility;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ const mockAddon = jest.fn().mockImplementation(() => ({
destroy: jest.fn()
}));

jest.mock('slickgrid/plugins/slick.headerbuttons', () => mockAddon);
Slick.Plugins = {
HeaderButtons: mockAddon
} as any;

describe('headerButtonExtension', () => {
jest.mock('slickgrid/plugins/slick.headerbuttons', () => mockAddon);
Slick.Plugins = { HeaderButtons: mockAddon } as any;

let extension: HeaderButtonExtension;
let extensionUtility: ExtensionUtility;
let sharedService: SharedService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ const mockAddon = jest.fn().mockImplementation(() => ({
onCommand: new Slick.Event(),
}));

jest.mock('slickgrid/plugins/slick.headermenu', () => mockAddon);
Slick.Plugins = {
HeaderMenu: mockAddon
} as any;

describe('headerMenuExtension', () => {
jest.mock('slickgrid/plugins/slick.headermenu', () => mockAddon);
Slick.Plugins = { HeaderMenu: mockAddon } as any;

const columnsMock: Column[] = [{ id: 'field1', field: 'field1', width: 100, nameKey: 'TITLE' }, { id: 'field2', field: 'field2', width: 75 }];
let extensionUtility: ExtensionUtility;
let extension: HeaderMenuExtension;
Expand Down Expand Up @@ -170,7 +168,7 @@ describe('headerMenuExtension', () => {
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
);
expect(onBeforeSpy).toHaveBeenCalledWith(expect.anything(), { grid: gridStub, column: {}, menu: divElement });
expect(onBeforeSpy).toHaveBeenCalledWith(expect.anything(), { grid: gridStub, column: {} as Column, menu: divElement });
expect(onCommandSpy).not.toHaveBeenCalled();
expect(onAfterSpy).not.toHaveBeenCalled();
});
Expand All @@ -189,7 +187,7 @@ describe('headerMenuExtension', () => {
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
);
expect(onAfterSpy).toHaveBeenCalledWith(expect.anything(), { grid: gridStub, column: {}, menu: divElement });
expect(onAfterSpy).toHaveBeenCalledWith(expect.anything(), { grid: gridStub, column: {} as Column, menu: divElement });
expect(onBeforeSpy).not.toHaveBeenCalled();
expect(onCommandSpy).not.toHaveBeenCalled();
});
Expand All @@ -208,7 +206,7 @@ describe('headerMenuExtension', () => {
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
);
expect(onCommandSpy).toHaveBeenCalledWith(expect.anything(), { grid: gridStub, command: 'help', item: { command: 'help' }, column: {} });
expect(onCommandSpy).toHaveBeenCalledWith(expect.anything(), { grid: gridStub, command: 'help', item: { command: 'help' }, column: {} as Column });
expect(onBeforeSpy).not.toHaveBeenCalled();
expect(onAfterSpy).not.toHaveBeenCalled();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ const mockSelectionModel = jest.fn().mockImplementation(() => ({
destroy: jest.fn()
}));

jest.mock('slickgrid/plugins/slick.rowmovemanager', () => mockAddon);
Slick.RowMoveManager = mockAddon;
describe('rowMoveManagerExtension', () => {
jest.mock('slickgrid/plugins/slick.rowmovemanager', () => mockAddon);
Slick.RowMoveManager = mockAddon;

jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockSelectionModel);
Slick.RowSelectionModel = mockSelectionModel;
jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockSelectionModel);
Slick.RowSelectionModel = mockSelectionModel;

describe('rowMoveManagerExtension', () => {
let extensionUtility: ExtensionUtility;
let sharedService: SharedService;
let translateService: TranslateServiceStub;
Expand All @@ -53,7 +53,7 @@ describe('rowMoveManagerExtension', () => {
sharedService = new SharedService();
translateService = new TranslateServiceStub();
extensionUtility = new ExtensionUtility(sharedService, translateService);
extension = new RowMoveManagerExtension(extensionUtility, sharedService);
extension = new RowMoveManagerExtension(sharedService);
});

it('should return null after calling "create" method when either the column definitions or the grid options is missing', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const mockAddon = jest.fn().mockImplementation(() => ({
destroy: jest.fn()
}));

jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockAddon);
Slick.RowSelectionModel = mockAddon;

describe('rowSelectionExtension', () => {
jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockAddon);
Slick.RowSelectionModel = mockAddon;

let extension: RowSelectionExtension;
let extensionUtility: ExtensionUtility;
let sharedService: SharedService;
Expand All @@ -32,7 +32,7 @@ describe('rowSelectionExtension', () => {
sharedService = new SharedService();
translateService = new TranslateServiceStub();
extensionUtility = new ExtensionUtility(sharedService, translateService);
extension = new RowSelectionExtension(extensionUtility, sharedService);
extension = new RowSelectionExtension(sharedService);
});

it('should return null when either the grid object or the grid options is missing', () => {
Expand Down
Loading

0 comments on commit 6816696

Please sign in to comment.