Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 7f6c0c2

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
feat(tests): add few Extensions unit tests
1 parent 4d1d580 commit 7f6c0c2

25 files changed

+901
-146
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { I18N } from 'aurelia-i18n';
2+
import { GridOption } from '../../models/gridOption.interface';
3+
import { AutoTooltipExtension } from '../autoTooltipExtension';
4+
import { ExtensionUtility } from '../extensionUtility';
5+
import { SharedService } from '../../services/shared.service';
6+
7+
declare var Slick: any;
8+
9+
const gridStub = {
10+
getOptions: jest.fn(),
11+
registerPlugin: jest.fn(),
12+
};
13+
14+
const mockAddon = jest.fn().mockImplementation(() => ({
15+
init: jest.fn(),
16+
destroy: jest.fn()
17+
}));
18+
19+
jest.mock('slickgrid/plugins/slick.autotooltips', () => mockAddon);
20+
Slick.AutoTooltips = mockAddon;
21+
22+
describe('autoTooltipExtension', () => {
23+
let extension: AutoTooltipExtension;
24+
let extensionUtility: ExtensionUtility;
25+
let sharedService: SharedService;
26+
const gridOptionsMock = { enableAutoTooltip: true } as GridOption;
27+
28+
beforeEach(() => {
29+
extensionUtility = new ExtensionUtility({} as I18N, sharedService);
30+
sharedService = new SharedService();
31+
extension = new AutoTooltipExtension(extensionUtility, sharedService);
32+
});
33+
34+
it('should return null when either the grid object or the grid options is missing', () => {
35+
const output = extension.register();
36+
expect(output).toBeNull();
37+
});
38+
39+
describe('registered addon', () => {
40+
beforeEach(() => {
41+
jest.spyOn(SharedService.prototype, 'grid', 'get').mockReturnValue(gridStub);
42+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
43+
});
44+
45+
it('should register the addon', () => {
46+
const pluginSpy = jest.spyOn(SharedService.prototype.grid, 'registerPlugin');
47+
48+
const instance = extension.register();
49+
50+
expect(mockAddon).toHaveBeenCalledWith({});
51+
expect(pluginSpy).toHaveBeenCalledWith(instance);
52+
});
53+
54+
it('should dispose of the addon', () => {
55+
const instance = extension.register();
56+
const destroySpy = jest.spyOn(instance, 'destroy');
57+
58+
extension.dispose();
59+
60+
expect(destroySpy).toHaveBeenCalled();
61+
});
62+
63+
it('should provide addon options and expect them to be called in the addon constructor', () => {
64+
const optionMock = { enableForCells: true, enableForHeaderCells: false, maxToolTipLength: 12 };
65+
const addonOptions = { ...gridOptionsMock, autoTooltipOptions: optionMock };
66+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(addonOptions);
67+
68+
extension.register();
69+
70+
expect(mockAddon).toHaveBeenCalledWith(optionMock);
71+
});
72+
});
73+
});
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import { I18N } from 'aurelia-i18n';
2+
import { GridOption } from '../../models/gridOption.interface';
3+
import { CellExternalCopyManagerExtension } from '../cellExternalCopyManagerExtension';
4+
import { ExtensionUtility } from '../extensionUtility';
5+
import { SharedService } from '../../services/shared.service';
6+
import { SelectedRange } from '../../models';
7+
8+
declare var Slick: any;
9+
10+
const gridStub = {
11+
getOptions: jest.fn(),
12+
registerPlugin: jest.fn(),
13+
setSelectionModel: jest.fn(),
14+
};
15+
16+
const addonStub = {
17+
init: jest.fn(),
18+
destroy: jest.fn(),
19+
onCopyCells: new Slick.Event(),
20+
onCopyCancelled: new Slick.Event(),
21+
onPasteCells: new Slick.Event(),
22+
};
23+
const mockAddon = jest.fn().mockImplementation(() => addonStub);
24+
const mockSelectionModel = jest.fn().mockImplementation(() => ({
25+
init: jest.fn(),
26+
destroy: jest.fn()
27+
}));
28+
29+
jest.mock('slickgrid/plugins/slick.cellexternalcopymanager', () => mockAddon);
30+
Slick.CellExternalCopyManager = mockAddon;
31+
32+
jest.mock('slickgrid/plugins/slick.cellselectionmodel', () => mockSelectionModel);
33+
Slick.CellSelectionModel = mockSelectionModel;
34+
35+
describe('cellExternalCopyManagerExtension', () => {
36+
const mockEventCallback = (e, args: { ranges: SelectedRange[] }) => { };
37+
const mockSelectRange = [{ fromCell: 1, fromRow: 1, toCell: 1, toRow: 1 }] as SelectedRange[];
38+
const mockSelectRangeEvent = { ranges: mockSelectRange };
39+
40+
let extension: CellExternalCopyManagerExtension;
41+
let extensionUtility: ExtensionUtility;
42+
let sharedService: SharedService;
43+
const gridOptionsMock = {
44+
enableCheckboxSelector: true,
45+
excelCopyBufferOptions: {
46+
onExtensionRegistered: jest.fn(),
47+
onCopyCells: mockEventCallback,
48+
onCopyCancelled: mockEventCallback,
49+
onPasteCells: mockEventCallback,
50+
}
51+
} as GridOption;
52+
53+
beforeEach(() => {
54+
extensionUtility = new ExtensionUtility({} as I18N, sharedService);
55+
sharedService = new SharedService();
56+
extension = new CellExternalCopyManagerExtension(extensionUtility, sharedService);
57+
});
58+
59+
afterEach(() => {
60+
jest.clearAllMocks();
61+
});
62+
63+
it('should return null when either the grid object or the grid options is missing', () => {
64+
const output = extension.register();
65+
expect(output).toBeNull();
66+
});
67+
68+
describe('registered addon', () => {
69+
beforeEach(() => {
70+
jest.spyOn(SharedService.prototype, 'grid', 'get').mockReturnValue(gridStub);
71+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
72+
});
73+
74+
it('should register the addon', () => {
75+
const pluginSpy = jest.spyOn(SharedService.prototype.grid, 'registerPlugin');
76+
const optionSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onExtensionRegistered');
77+
78+
const instance = extension.register();
79+
80+
expect(optionSpy).toHaveBeenCalledWith(instance);
81+
expect(pluginSpy).toHaveBeenCalledWith(instance);
82+
expect(mockSelectionModel).toHaveBeenCalled();
83+
expect(mockAddon).toHaveBeenCalledWith({
84+
clipboardCommandHandler: expect.anything(),
85+
dataItemColumnValueExtractor: expect.anything(),
86+
newRowCreator: expect.anything(),
87+
includeHeaderWhenCopying: false,
88+
readOnlyMode: false,
89+
onCopyCancelled: expect.anything(),
90+
onCopyCells: expect.anything(),
91+
onExtensionRegistered: expect.anything(),
92+
onPasteCells: expect.anything(),
93+
});
94+
});
95+
96+
it('should call internal event handler subscribe and expect the "onCopyCells" option to be called when addon notify is called', () => {
97+
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
98+
const onCopySpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCells');
99+
const onCancelSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCancelled');
100+
const onPasteSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onPasteCells');
101+
102+
const instance = extension.register();
103+
instance.onCopyCells.notify(mockSelectRangeEvent, new Slick.EventData(), gridStub);
104+
105+
expect(handlerSpy).toHaveBeenCalledTimes(3);
106+
expect(handlerSpy).toHaveBeenCalledWith(
107+
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
108+
expect.anything()
109+
);
110+
expect(onCopySpy).toHaveBeenCalledWith(expect.anything(), mockSelectRangeEvent);
111+
expect(onCancelSpy).not.toHaveBeenCalled();
112+
expect(onPasteSpy).not.toHaveBeenCalled();
113+
});
114+
115+
it('should call internal event handler subscribe and expect the "onCopyCancelled" option to be called when addon notify is called', () => {
116+
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
117+
const onCopySpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCells');
118+
const onCancelSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCancelled');
119+
const onPasteSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onPasteCells');
120+
121+
const instance = extension.register();
122+
instance.onCopyCancelled.notify(mockSelectRangeEvent, new Slick.EventData(), gridStub);
123+
124+
expect(handlerSpy).toHaveBeenCalledTimes(3);
125+
expect(handlerSpy).toHaveBeenCalledWith(
126+
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
127+
expect.anything()
128+
);
129+
expect(onCopySpy).not.toHaveBeenCalled();
130+
expect(onCancelSpy).toHaveBeenCalledWith(expect.anything(), mockSelectRangeEvent);
131+
expect(onPasteSpy).not.toHaveBeenCalled();
132+
});
133+
134+
it('should call internal event handler subscribe and expect the "onPasteCells" option to be called when addon notify is called', () => {
135+
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
136+
const onCopySpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCells');
137+
const onCancelSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCancelled');
138+
const onPasteSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onPasteCells');
139+
140+
const instance = extension.register();
141+
instance.onPasteCells.notify(mockSelectRangeEvent, new Slick.EventData(), gridStub);
142+
143+
expect(handlerSpy).toHaveBeenCalledTimes(3);
144+
expect(handlerSpy).toHaveBeenCalledWith(
145+
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
146+
expect.anything()
147+
);
148+
expect(onCopySpy).not.toHaveBeenCalled();
149+
expect(onCancelSpy).not.toHaveBeenCalled();
150+
expect(onPasteSpy).toHaveBeenCalledWith(expect.anything(), mockSelectRangeEvent);
151+
});
152+
153+
it('should dispose of the addon', () => {
154+
const instance = extension.register();
155+
const destroySpy = jest.spyOn(instance, 'destroy');
156+
157+
extension.dispose();
158+
159+
expect(destroySpy).toHaveBeenCalled();
160+
});
161+
});
162+
});
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { I18N } from 'aurelia-i18n';
2+
import { GridOption } from '../../models/gridOption.interface';
3+
import { CheckboxSelectorExtension } from '../checkboxSelectorExtension';
4+
import { ExtensionUtility } from '../extensionUtility';
5+
import { SharedService } from '../../services/shared.service';
6+
import { Column } from '../../models';
7+
8+
declare var Slick: any;
9+
10+
const gridStub = {
11+
getOptions: jest.fn(),
12+
getSelectionModel: jest.fn(),
13+
registerPlugin: jest.fn(),
14+
setSelectionModel: jest.fn(),
15+
};
16+
17+
const mockAddon = jest.fn().mockImplementation(() => ({
18+
init: jest.fn(),
19+
destroy: jest.fn(),
20+
getColumnDefinition: jest.fn(),
21+
selectRows: jest.fn(),
22+
}));
23+
const mockSelectionModel = jest.fn().mockImplementation(() => ({
24+
init: jest.fn(),
25+
destroy: jest.fn()
26+
}));
27+
28+
jest.mock('slickgrid/plugins/slick.checkboxselectcolumn', () => mockAddon);
29+
Slick.CheckboxSelectColumn = mockAddon;
30+
31+
jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockSelectionModel);
32+
Slick.RowSelectionModel = mockSelectionModel;
33+
34+
describe('checkboxSelectorExtension', () => {
35+
let extension: CheckboxSelectorExtension;
36+
let extensionUtility: ExtensionUtility;
37+
let sharedService: SharedService;
38+
const gridOptionsMock = { enableCheckboxSelector: true } as GridOption;
39+
40+
beforeEach(() => {
41+
extensionUtility = new ExtensionUtility({} as I18N, sharedService);
42+
sharedService = new SharedService();
43+
extension = new CheckboxSelectorExtension(extensionUtility, sharedService);
44+
});
45+
46+
it('should return null after calling "create" method when either the column definitions or the grid options is missing', () => {
47+
const output = extension.create([] as Column[], null);
48+
expect(output).toBeNull();
49+
});
50+
51+
it('should return null after calling "register" method when either the grid object or the grid options is missing', () => {
52+
const output = extension.register();
53+
expect(output).toBeNull();
54+
});
55+
56+
describe('registered addon', () => {
57+
let columnSelectionMock: Column;
58+
let columnsMock: Column[];
59+
60+
beforeEach(() => {
61+
columnsMock = [{ id: 'field1', field: 'field1', width: 100, cssClass: 'red' }];
62+
columnSelectionMock = { id: '_checkbox_selector', field: 'sel' };
63+
jest.spyOn(SharedService.prototype, 'grid', 'get').mockReturnValue(gridStub);
64+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
65+
});
66+
67+
it('should register the addon', () => {
68+
const pluginSpy = jest.spyOn(SharedService.prototype.grid, 'registerPlugin');
69+
70+
const instance = extension.create(columnsMock, gridOptionsMock);
71+
const selectionModel = extension.register();
72+
73+
expect(selectionModel).not.toBeNull();
74+
expect(mockAddon).toHaveBeenCalledWith({});
75+
expect(mockSelectionModel).toHaveBeenCalledWith({});
76+
expect(pluginSpy).toHaveBeenCalledWith(instance);
77+
});
78+
79+
it('should register the addon with the registered plugin provided as argument', () => {
80+
const pluginSpy = jest.spyOn(SharedService.prototype.grid, 'registerPlugin');
81+
const selectionSpy = jest.spyOn(SharedService.prototype.grid, 'getSelectionModel');
82+
83+
const instance = extension.create(columnsMock, gridOptionsMock);
84+
const selectionModel = extension.register();
85+
const selectionModel2 = extension.register(selectionModel);
86+
87+
expect(selectionModel).not.toBeNull();
88+
expect(selectionModel2).not.toBeNull();
89+
expect(mockAddon).toHaveBeenCalledWith({});
90+
expect(selectionSpy).toHaveBeenCalled();
91+
expect(mockSelectionModel).toHaveBeenCalledWith({});
92+
expect(pluginSpy).toHaveBeenCalledWith(instance);
93+
});
94+
95+
it('should dispose of the addon', () => {
96+
const instance = extension.create(columnsMock, gridOptionsMock);
97+
const destroySpy = jest.spyOn(instance, 'destroy');
98+
99+
extension.dispose();
100+
101+
expect(destroySpy).toHaveBeenCalled();
102+
});
103+
104+
it('should provide addon options and expect them to be called in the addon constructor', () => {
105+
const optionMock = { selectActiveRow: true };
106+
const selectionModelOptions = { ...gridOptionsMock, rowSelectionOptions: optionMock };
107+
const selectionColumn = { ...columnSelectionMock, excludeFromExport: true, excludeFromColumnPicker: true, excludeFromGridMenu: true, excludeFromQuery: true, excludeFromHeaderMenu: true };
108+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(selectionModelOptions);
109+
110+
// we can only spy after 1st "create" call, we'll only get a valid selectionColumn on 2nd "create" call
111+
const instance = extension.create(columnsMock, gridOptionsMock);
112+
jest.spyOn(instance, 'getColumnDefinition').mockReturnValue(columnSelectionMock);
113+
expect(columnsMock[0]).not.toEqual(selectionColumn);
114+
115+
// do our expect here after 2nd "create" call, the selectionColumn flags will change only after this 2nd call
116+
extension.create(columnsMock, gridOptionsMock);
117+
extension.register();
118+
119+
expect(mockSelectionModel).toHaveBeenCalledWith(optionMock);
120+
expect(columnsMock[0]).toEqual(selectionColumn);
121+
});
122+
123+
it('should be able to pre-select rows', (done) => {
124+
const selectionModelOptions = { ...gridOptionsMock, preselectedRows: [0], rowSelectionOptions: { selectActiveRow: true } };
125+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(selectionModelOptions);
126+
const pluginSpy = jest.spyOn(SharedService.prototype.grid, 'registerPlugin');
127+
const selectionSpy = jest.spyOn(SharedService.prototype.grid, 'getSelectionModel').mockReturnValue(true);
128+
129+
const instance = extension.create(columnsMock, gridOptionsMock);
130+
const rowSpy = jest.spyOn(instance, 'selectRows');
131+
const selectionModel = extension.register();
132+
133+
expect(selectionModel).not.toBeNull();
134+
expect(mockAddon).toHaveBeenCalledWith({});
135+
expect(selectionSpy).toHaveBeenCalled();
136+
expect(mockSelectionModel).toHaveBeenCalledWith({});
137+
expect(pluginSpy).toHaveBeenCalledWith(instance);
138+
setTimeout(() => {
139+
expect(rowSpy).toHaveBeenCalledWith(selectionModelOptions.preselectedRows);
140+
done();
141+
});
142+
});
143+
});
144+
});

0 commit comments

Comments
 (0)