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

Commit e94b725

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
refactor(tests): test all with TestBed & add HeaderButton test
1 parent 7f6c0c2 commit e94b725

11 files changed

+163
-40
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

src/app/modules/angular-slickgrid/extensions/__tests__/autoTooltipExtension.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { I18N } from 'aurelia-i18n';
1+
import { TestBed } from '@angular/core/testing';
2+
import { TranslateService, TranslateModule } from '@ngx-translate/core';
23
import { GridOption } from '../../models/gridOption.interface';
34
import { AutoTooltipExtension } from '../autoTooltipExtension';
45
import { ExtensionUtility } from '../extensionUtility';
@@ -20,15 +21,17 @@ jest.mock('slickgrid/plugins/slick.autotooltips', () => mockAddon);
2021
Slick.AutoTooltips = mockAddon;
2122

2223
describe('autoTooltipExtension', () => {
24+
let translate: TranslateService;
2325
let extension: AutoTooltipExtension;
24-
let extensionUtility: ExtensionUtility;
25-
let sharedService: SharedService;
2626
const gridOptionsMock = { enableAutoTooltip: true } as GridOption;
2727

2828
beforeEach(() => {
29-
extensionUtility = new ExtensionUtility({} as I18N, sharedService);
30-
sharedService = new SharedService();
31-
extension = new AutoTooltipExtension(extensionUtility, sharedService);
29+
TestBed.configureTestingModule({
30+
providers: [AutoTooltipExtension, ExtensionUtility, SharedService],
31+
imports: [TranslateModule.forRoot()]
32+
});
33+
extension = TestBed.get(AutoTooltipExtension);
34+
translate = TestBed.get(TranslateService);
3235
});
3336

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

src/app/modules/angular-slickgrid/extensions/__tests__/cellExternalCopyManagerExtension.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { I18N } from 'aurelia-i18n';
1+
import { TestBed } from '@angular/core/testing';
2+
import { TranslateService, TranslateModule } from '@ngx-translate/core';
23
import { GridOption } from '../../models/gridOption.interface';
34
import { CellExternalCopyManagerExtension } from '../cellExternalCopyManagerExtension';
45
import { ExtensionUtility } from '../extensionUtility';
@@ -33,13 +34,12 @@ jest.mock('slickgrid/plugins/slick.cellselectionmodel', () => mockSelectionModel
3334
Slick.CellSelectionModel = mockSelectionModel;
3435

3536
describe('cellExternalCopyManagerExtension', () => {
37+
let translate: TranslateService;
3638
const mockEventCallback = (e, args: { ranges: SelectedRange[] }) => { };
3739
const mockSelectRange = [{ fromCell: 1, fromRow: 1, toCell: 1, toRow: 1 }] as SelectedRange[];
3840
const mockSelectRangeEvent = { ranges: mockSelectRange };
3941

4042
let extension: CellExternalCopyManagerExtension;
41-
let extensionUtility: ExtensionUtility;
42-
let sharedService: SharedService;
4343
const gridOptionsMock = {
4444
enableCheckboxSelector: true,
4545
excelCopyBufferOptions: {
@@ -51,9 +51,12 @@ describe('cellExternalCopyManagerExtension', () => {
5151
} as GridOption;
5252

5353
beforeEach(() => {
54-
extensionUtility = new ExtensionUtility({} as I18N, sharedService);
55-
sharedService = new SharedService();
56-
extension = new CellExternalCopyManagerExtension(extensionUtility, sharedService);
54+
TestBed.configureTestingModule({
55+
providers: [CellExternalCopyManagerExtension, ExtensionUtility, SharedService],
56+
imports: [TranslateModule.forRoot()]
57+
});
58+
extension = TestBed.get(CellExternalCopyManagerExtension);
59+
translate = TestBed.get(TranslateService);
5760
});
5861

5962
afterEach(() => {

src/app/modules/angular-slickgrid/extensions/__tests__/checkboxSelectorExtension.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { I18N } from 'aurelia-i18n';
1+
import { TestBed } from '@angular/core/testing';
2+
import { TranslateService, TranslateModule } from '@ngx-translate/core';
23
import { GridOption } from '../../models/gridOption.interface';
34
import { CheckboxSelectorExtension } from '../checkboxSelectorExtension';
45
import { ExtensionUtility } from '../extensionUtility';
@@ -32,15 +33,17 @@ jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockSelectionModel)
3233
Slick.RowSelectionModel = mockSelectionModel;
3334

3435
describe('checkboxSelectorExtension', () => {
36+
let translate: TranslateService;
3537
let extension: CheckboxSelectorExtension;
36-
let extensionUtility: ExtensionUtility;
37-
let sharedService: SharedService;
3838
const gridOptionsMock = { enableCheckboxSelector: true } as GridOption;
3939

4040
beforeEach(() => {
41-
extensionUtility = new ExtensionUtility({} as I18N, sharedService);
42-
sharedService = new SharedService();
43-
extension = new CheckboxSelectorExtension(extensionUtility, sharedService);
41+
TestBed.configureTestingModule({
42+
providers: [CheckboxSelectorExtension, ExtensionUtility, SharedService],
43+
imports: [TranslateModule.forRoot()]
44+
});
45+
extension = TestBed.get(CheckboxSelectorExtension);
46+
translate = TestBed.get(TranslateService);
4447
});
4548

4649
it('should return null after calling "create" method when either the column definitions or the grid options is missing', () => {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { TranslateService, TranslateModule } from '@ngx-translate/core';
3+
import { GridOption } from '../../models/gridOption.interface';
4+
import { HeaderButtonExtension } from '../headerButtonExtension';
5+
import { ExtensionUtility } from '../extensionUtility';
6+
import { SharedService } from '../../services/shared.service';
7+
import { HeaderButtonOnCommandArgs } from '../../models';
8+
9+
declare var Slick: any;
10+
11+
const gridStub = {
12+
getOptions: jest.fn(),
13+
registerPlugin: jest.fn(),
14+
};
15+
16+
const mockAddon = jest.fn().mockImplementation(() => ({
17+
onCommand: new Slick.Event(),
18+
init: jest.fn(),
19+
destroy: jest.fn()
20+
}));
21+
22+
jest.mock('slickgrid/plugins/slick.headerbuttons', () => mockAddon);
23+
Slick.Plugins = {
24+
HeaderButtons: mockAddon
25+
};
26+
27+
describe('headerButtonExtension', () => {
28+
let translate: TranslateService;
29+
let extension: HeaderButtonExtension;
30+
const mockOnCommandArgs = {
31+
button: {
32+
command: 'toggle-highlight',
33+
cssClass: 'fa fa-circle red',
34+
tooltip: 'Remove highlight.',
35+
},
36+
column: { id: 'field1', field: 'field1' },
37+
command: 'toggle-highlight',
38+
grid: gridStub
39+
} as HeaderButtonOnCommandArgs;
40+
const mockEventCallback = (e, args: HeaderButtonOnCommandArgs) => { };
41+
const gridOptionsMock = {
42+
enableHeaderButton: true,
43+
headerButton: {
44+
onExtensionRegistered: jest.fn(),
45+
onCommand: mockEventCallback
46+
}
47+
} as GridOption;
48+
49+
beforeEach(() => {
50+
TestBed.configureTestingModule({
51+
providers: [HeaderButtonExtension, ExtensionUtility, SharedService],
52+
imports: [TranslateModule.forRoot()]
53+
});
54+
extension = TestBed.get(HeaderButtonExtension);
55+
translate = TestBed.get(TranslateService);
56+
});
57+
58+
it('should return null when either the grid object or the grid options is missing', () => {
59+
const output = extension.register();
60+
expect(output).toBeNull();
61+
});
62+
63+
describe('registered addon', () => {
64+
beforeEach(() => {
65+
jest.spyOn(SharedService.prototype, 'grid', 'get').mockReturnValue(gridStub);
66+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
67+
});
68+
69+
it('should register the addon', () => {
70+
const pluginSpy = jest.spyOn(SharedService.prototype.grid, 'registerPlugin');
71+
72+
const instance = extension.register();
73+
74+
expect(mockAddon).toHaveBeenCalledWith({
75+
onCommand: expect.anything(),
76+
onExtensionRegistered: expect.anything(),
77+
});
78+
expect(pluginSpy).toHaveBeenCalledWith(instance);
79+
});
80+
81+
it('should call internal event handler subscribe and expect the "onCommand" option to be called when addon notify is called', () => {
82+
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
83+
const onCopySpy = jest.spyOn(SharedService.prototype.gridOptions.headerButton, 'onCommand');
84+
const instance = extension.register();
85+
instance.onCommand.notify(mockOnCommandArgs, new Slick.EventData(), gridStub);
86+
87+
expect(handlerSpy).toHaveBeenCalledWith(
88+
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
89+
expect.anything()
90+
);
91+
expect(onCopySpy).toHaveBeenCalledWith(expect.anything(), mockOnCommandArgs);
92+
});
93+
94+
it('should dispose of the addon', () => {
95+
const instance = extension.register();
96+
const destroySpy = jest.spyOn(instance, 'destroy');
97+
98+
extension.dispose();
99+
100+
expect(destroySpy).toHaveBeenCalled();
101+
});
102+
});
103+
});

src/app/modules/angular-slickgrid/extensions/__tests__/rowSelectionExtension.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { I18N } from 'aurelia-i18n';
1+
import { TestBed } from '@angular/core/testing';
2+
import { TranslateService, TranslateModule } from '@ngx-translate/core';
23
import { GridOption } from '../../models/gridOption.interface';
34
import { RowSelectionExtension } from '../rowSelectionExtension';
45
import { ExtensionUtility } from '../extensionUtility';
@@ -22,15 +23,17 @@ jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockAddon);
2223
Slick.RowSelectionModel = mockAddon;
2324

2425
describe('rowSelectionExtension', () => {
26+
let translate: TranslateService;
2527
let extension: RowSelectionExtension;
26-
let extensionUtility: ExtensionUtility;
27-
let sharedService: SharedService;
2828
const gridOptionsMock = { enableRowSelection: true } as GridOption;
2929

3030
beforeEach(() => {
31-
extensionUtility = new ExtensionUtility({} as I18N, sharedService);
32-
sharedService = new SharedService();
33-
extension = new RowSelectionExtension(extensionUtility, sharedService);
31+
TestBed.configureTestingModule({
32+
providers: [RowSelectionExtension, ExtensionUtility, SharedService],
33+
imports: [TranslateModule.forRoot()]
34+
});
35+
extension = TestBed.get(RowSelectionExtension);
36+
translate = TestBed.get(TranslateService);
3437
});
3538

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

src/app/modules/angular-slickgrid/extensions/headerButtonExtension.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { Extension, ExtensionName, HeaderButtonOnCommandArgs } from '../models/index';
2+
import { Extension, ExtensionName, HeaderButtonOnCommandArgs, SlickEventHandler } from '../models/index';
33
import { ExtensionUtility } from './extensionUtility';
44
import { SharedService } from '../services/shared.service';
55

@@ -8,17 +8,23 @@ declare var Slick: any;
88

99
@Injectable()
1010
export class HeaderButtonExtension implements Extension {
11-
private _eventHandler: any = new Slick.EventHandler();
12-
private _extension: any;
11+
private _eventHandler: SlickEventHandler;
12+
private _addon: any;
1313

14-
constructor(private extensionUtility: ExtensionUtility, private sharedService: SharedService) { }
14+
constructor(private extensionUtility: ExtensionUtility, private sharedService: SharedService) {
15+
this._eventHandler = new Slick.EventHandler();
16+
}
17+
18+
get eventHandler(): SlickEventHandler {
19+
return this._eventHandler;
20+
}
1521

1622
dispose() {
1723
// unsubscribe all SlickGrid events
1824
this._eventHandler.unsubscribeAll();
1925

20-
if (this._extension && this._extension.destroy) {
21-
this._extension.destroy();
26+
if (this._addon && this._addon.destroy) {
27+
this._addon.destroy();
2228
}
2329
}
2430

@@ -27,21 +33,21 @@ export class HeaderButtonExtension implements Extension {
2733
if (this.sharedService && this.sharedService.grid && this.sharedService.gridOptions) {
2834
// dynamically import the SlickGrid plugin (addon) with RequireJS
2935
this.extensionUtility.loadExtensionDynamically(ExtensionName.headerButton);
30-
this._extension = new Slick.Plugins.HeaderButtons(this.sharedService.gridOptions.headerButton || {});
31-
this.sharedService.grid.registerPlugin(this._extension);
36+
this._addon = new Slick.Plugins.HeaderButtons(this.sharedService.gridOptions.headerButton || {});
37+
this.sharedService.grid.registerPlugin(this._addon);
3238

3339
// hook all events
3440
if (this.sharedService.grid && this.sharedService.gridOptions.headerButton) {
3541
if (this.sharedService.gridOptions.headerButton.onExtensionRegistered) {
36-
this.sharedService.gridOptions.headerButton.onExtensionRegistered(this._extension);
42+
this.sharedService.gridOptions.headerButton.onExtensionRegistered(this._addon);
3743
}
38-
this._eventHandler.subscribe(this._extension.onCommand, (e: any, args: HeaderButtonOnCommandArgs) => {
44+
this._eventHandler.subscribe(this._addon.onCommand, (e: any, args: HeaderButtonOnCommandArgs) => {
3945
if (this.sharedService.gridOptions.headerButton && typeof this.sharedService.gridOptions.headerButton.onCommand === 'function') {
4046
this.sharedService.gridOptions.headerButton.onCommand(e, args);
4147
}
4248
});
4349
}
44-
return this._extension;
50+
return this._addon;
4551
}
4652
return null;
4753
}

src/app/modules/angular-slickgrid/filters/inputMaskFilter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class InputMaskFilter extends InputFilter {
1818
*/
1919
init(args: FilterArguments) {
2020
if (!args) {
21-
throw new Error('[Aurelia-SlickGrid] A filter must always have an "init()" with valid arguments.');
21+
throw new Error('[Angular-SlickGrid] A filter must always have an "init()" with valid arguments.');
2222
}
2323
this.grid = args.grid;
2424
this.callback = args.callback;

src/app/modules/angular-slickgrid/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export * from './searchTerm.type';
8989
export * from './selectedRange.interface';
9090
export * from './selectOption.interface';
9191
export * from './slickEvent.interface';
92+
export * from './slickEventHandler.interface';
9293
export * from './sortChangedArgs.interface';
9394
export * from './sortDirection.enum';
9495
export * from './sortDirectionNumber.enum';

0 commit comments

Comments
 (0)