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

Commit 8b7e4f1

Browse files
authored
Merge pull request #211 from ghiscoding/feat/test-extensions
feat(tests): add few Extensions unit tests
2 parents 4d1d580 + e94b725 commit 8b7e4f1

31 files changed

+1040
-162
lines changed

src/app/examples/grid-headerbutton.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class GridHeaderButtonComponent implements OnInit, OnDestroy {
4949
enableCellNavigation: true,
5050
headerButton: {
5151
onCommand: (e, args) => {
52+
console.log(args)
5253
const column = args.column;
5354
const button = args.button;
5455
const command = args.command;

src/app/examples/grid-rowdetail.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class GridRowDetailComponent implements OnInit {
8484
selectActiveRow: true
8585
},
8686
rowDetailView: {
87-
// We can load the "process" asynchronously in 3 different ways (aurelia-http-client, aurelia-fetch-client OR even Promise)
87+
// We can load the "process" asynchronously in 2 different ways (httpClient OR even Promise)
8888
process: (item) => this.simulateServerAsyncCall(item),
8989
// process: this.httpFetch.fetch(`api/item/${item.id}`),
9090

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { TranslateService, TranslateModule } from '@ngx-translate/core';
3+
import { GridOption } from '../../models/gridOption.interface';
4+
import { AutoTooltipExtension } from '../autoTooltipExtension';
5+
import { ExtensionUtility } from '../extensionUtility';
6+
import { SharedService } from '../../services/shared.service';
7+
8+
declare var Slick: any;
9+
10+
const gridStub = {
11+
getOptions: jest.fn(),
12+
registerPlugin: jest.fn(),
13+
};
14+
15+
const mockAddon = jest.fn().mockImplementation(() => ({
16+
init: jest.fn(),
17+
destroy: jest.fn()
18+
}));
19+
20+
jest.mock('slickgrid/plugins/slick.autotooltips', () => mockAddon);
21+
Slick.AutoTooltips = mockAddon;
22+
23+
describe('autoTooltipExtension', () => {
24+
let translate: TranslateService;
25+
let extension: AutoTooltipExtension;
26+
const gridOptionsMock = { enableAutoTooltip: true } as GridOption;
27+
28+
beforeEach(() => {
29+
TestBed.configureTestingModule({
30+
providers: [AutoTooltipExtension, ExtensionUtility, SharedService],
31+
imports: [TranslateModule.forRoot()]
32+
});
33+
extension = TestBed.get(AutoTooltipExtension);
34+
translate = TestBed.get(TranslateService);
35+
});
36+
37+
it('should return null when either the grid object or the grid options is missing', () => {
38+
const output = extension.register();
39+
expect(output).toBeNull();
40+
});
41+
42+
describe('registered addon', () => {
43+
beforeEach(() => {
44+
jest.spyOn(SharedService.prototype, 'grid', 'get').mockReturnValue(gridStub);
45+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
46+
});
47+
48+
it('should register the addon', () => {
49+
const pluginSpy = jest.spyOn(SharedService.prototype.grid, 'registerPlugin');
50+
51+
const instance = extension.register();
52+
53+
expect(mockAddon).toHaveBeenCalledWith({});
54+
expect(pluginSpy).toHaveBeenCalledWith(instance);
55+
});
56+
57+
it('should dispose of the addon', () => {
58+
const instance = extension.register();
59+
const destroySpy = jest.spyOn(instance, 'destroy');
60+
61+
extension.dispose();
62+
63+
expect(destroySpy).toHaveBeenCalled();
64+
});
65+
66+
it('should provide addon options and expect them to be called in the addon constructor', () => {
67+
const optionMock = { enableForCells: true, enableForHeaderCells: false, maxToolTipLength: 12 };
68+
const addonOptions = { ...gridOptionsMock, autoTooltipOptions: optionMock };
69+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(addonOptions);
70+
71+
extension.register();
72+
73+
expect(mockAddon).toHaveBeenCalledWith(optionMock);
74+
});
75+
});
76+
});
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { TranslateService, TranslateModule } from '@ngx-translate/core';
3+
import { GridOption } from '../../models/gridOption.interface';
4+
import { CellExternalCopyManagerExtension } from '../cellExternalCopyManagerExtension';
5+
import { ExtensionUtility } from '../extensionUtility';
6+
import { SharedService } from '../../services/shared.service';
7+
import { SelectedRange } from '../../models';
8+
9+
declare var Slick: any;
10+
11+
const gridStub = {
12+
getOptions: jest.fn(),
13+
registerPlugin: jest.fn(),
14+
setSelectionModel: jest.fn(),
15+
};
16+
17+
const addonStub = {
18+
init: jest.fn(),
19+
destroy: jest.fn(),
20+
onCopyCells: new Slick.Event(),
21+
onCopyCancelled: new Slick.Event(),
22+
onPasteCells: new Slick.Event(),
23+
};
24+
const mockAddon = jest.fn().mockImplementation(() => addonStub);
25+
const mockSelectionModel = jest.fn().mockImplementation(() => ({
26+
init: jest.fn(),
27+
destroy: jest.fn()
28+
}));
29+
30+
jest.mock('slickgrid/plugins/slick.cellexternalcopymanager', () => mockAddon);
31+
Slick.CellExternalCopyManager = mockAddon;
32+
33+
jest.mock('slickgrid/plugins/slick.cellselectionmodel', () => mockSelectionModel);
34+
Slick.CellSelectionModel = mockSelectionModel;
35+
36+
describe('cellExternalCopyManagerExtension', () => {
37+
let translate: TranslateService;
38+
const mockEventCallback = (e, args: { ranges: SelectedRange[] }) => { };
39+
const mockSelectRange = [{ fromCell: 1, fromRow: 1, toCell: 1, toRow: 1 }] as SelectedRange[];
40+
const mockSelectRangeEvent = { ranges: mockSelectRange };
41+
42+
let extension: CellExternalCopyManagerExtension;
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+
TestBed.configureTestingModule({
55+
providers: [CellExternalCopyManagerExtension, ExtensionUtility, SharedService],
56+
imports: [TranslateModule.forRoot()]
57+
});
58+
extension = TestBed.get(CellExternalCopyManagerExtension);
59+
translate = TestBed.get(TranslateService);
60+
});
61+
62+
afterEach(() => {
63+
jest.clearAllMocks();
64+
});
65+
66+
it('should return null when either the grid object or the grid options is missing', () => {
67+
const output = extension.register();
68+
expect(output).toBeNull();
69+
});
70+
71+
describe('registered addon', () => {
72+
beforeEach(() => {
73+
jest.spyOn(SharedService.prototype, 'grid', 'get').mockReturnValue(gridStub);
74+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
75+
});
76+
77+
it('should register the addon', () => {
78+
const pluginSpy = jest.spyOn(SharedService.prototype.grid, 'registerPlugin');
79+
const optionSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onExtensionRegistered');
80+
81+
const instance = extension.register();
82+
83+
expect(optionSpy).toHaveBeenCalledWith(instance);
84+
expect(pluginSpy).toHaveBeenCalledWith(instance);
85+
expect(mockSelectionModel).toHaveBeenCalled();
86+
expect(mockAddon).toHaveBeenCalledWith({
87+
clipboardCommandHandler: expect.anything(),
88+
dataItemColumnValueExtractor: expect.anything(),
89+
newRowCreator: expect.anything(),
90+
includeHeaderWhenCopying: false,
91+
readOnlyMode: false,
92+
onCopyCancelled: expect.anything(),
93+
onCopyCells: expect.anything(),
94+
onExtensionRegistered: expect.anything(),
95+
onPasteCells: expect.anything(),
96+
});
97+
});
98+
99+
it('should call internal event handler subscribe and expect the "onCopyCells" option to be called when addon notify is called', () => {
100+
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
101+
const onCopySpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCells');
102+
const onCancelSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCancelled');
103+
const onPasteSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onPasteCells');
104+
105+
const instance = extension.register();
106+
instance.onCopyCells.notify(mockSelectRangeEvent, new Slick.EventData(), gridStub);
107+
108+
expect(handlerSpy).toHaveBeenCalledTimes(3);
109+
expect(handlerSpy).toHaveBeenCalledWith(
110+
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
111+
expect.anything()
112+
);
113+
expect(onCopySpy).toHaveBeenCalledWith(expect.anything(), mockSelectRangeEvent);
114+
expect(onCancelSpy).not.toHaveBeenCalled();
115+
expect(onPasteSpy).not.toHaveBeenCalled();
116+
});
117+
118+
it('should call internal event handler subscribe and expect the "onCopyCancelled" option to be called when addon notify is called', () => {
119+
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
120+
const onCopySpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCells');
121+
const onCancelSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCancelled');
122+
const onPasteSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onPasteCells');
123+
124+
const instance = extension.register();
125+
instance.onCopyCancelled.notify(mockSelectRangeEvent, new Slick.EventData(), gridStub);
126+
127+
expect(handlerSpy).toHaveBeenCalledTimes(3);
128+
expect(handlerSpy).toHaveBeenCalledWith(
129+
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
130+
expect.anything()
131+
);
132+
expect(onCopySpy).not.toHaveBeenCalled();
133+
expect(onCancelSpy).toHaveBeenCalledWith(expect.anything(), mockSelectRangeEvent);
134+
expect(onPasteSpy).not.toHaveBeenCalled();
135+
});
136+
137+
it('should call internal event handler subscribe and expect the "onPasteCells" option to be called when addon notify is called', () => {
138+
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
139+
const onCopySpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCells');
140+
const onCancelSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCancelled');
141+
const onPasteSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onPasteCells');
142+
143+
const instance = extension.register();
144+
instance.onPasteCells.notify(mockSelectRangeEvent, new Slick.EventData(), gridStub);
145+
146+
expect(handlerSpy).toHaveBeenCalledTimes(3);
147+
expect(handlerSpy).toHaveBeenCalledWith(
148+
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
149+
expect.anything()
150+
);
151+
expect(onCopySpy).not.toHaveBeenCalled();
152+
expect(onCancelSpy).not.toHaveBeenCalled();
153+
expect(onPasteSpy).toHaveBeenCalledWith(expect.anything(), mockSelectRangeEvent);
154+
});
155+
156+
it('should dispose of the addon', () => {
157+
const instance = extension.register();
158+
const destroySpy = jest.spyOn(instance, 'destroy');
159+
160+
extension.dispose();
161+
162+
expect(destroySpy).toHaveBeenCalled();
163+
});
164+
});
165+
});

0 commit comments

Comments
 (0)