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

Commit ae48ddf

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
feat(locales): add unit tests when using locales with enableTranslate
- move all throw error outside of constructor since it's too early, basically it should throw an error when enableTranslate is used without providing translateService DI
1 parent 413ea71 commit ae48ddf

File tree

7 files changed

+76
-48
lines changed

7 files changed

+76
-48
lines changed

src/app/modules/angular-slickgrid/components/slick-pagination.component.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ export class SlickPaginationComponent implements AfterViewInit, OnDestroy {
5555

5656
/** Constructor */
5757
constructor(private filterService: FilterService, private gridService: GridService, @Optional() private translate: TranslateService) {
58-
if (this._gridPaginationOptions && this._gridPaginationOptions.enableTranslate && !this.translate) {
59-
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
60-
}
6158
// translate all the text using ngx-translate or custom locales
6259
if (translate && translate.onLangChange) {
6360
this.subscriptions.push(this.translate.onLangChange.subscribe(() => this.translateAllUiTexts(this._locales)));
@@ -69,7 +66,10 @@ export class SlickPaginationComponent implements AfterViewInit, OnDestroy {
6966
}
7067

7168
ngAfterViewInit() {
72-
this._gridPaginationOptions = this._gridPaginationOptions;
69+
if (this._gridPaginationOptions && this._gridPaginationOptions.enableTranslate && !this.translate) {
70+
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
71+
}
72+
7373
if (!this._gridPaginationOptions || !this._gridPaginationOptions.pagination || (this._gridPaginationOptions.pagination.totalItems !== this.totalItems)) {
7474
this.refreshPagination();
7575
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -651,13 +651,13 @@ describe('gridMenuExtension', () => {
651651
});
652652

653653
describe('without ngx-translate', () => {
654-
it('should throw an error if "enableTranslate" is set but the Translate Service is null', (done) => {
655-
try {
656-
extension = new GridMenuExtension({} as ExportService, {} as ExtensionUtility, {} as FilterService, { gridOptions: { enableTranslate: true } } as SharedService, {} as SortService, null);
657-
} catch (e) {
658-
expect(e.toString()).toContain('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured');
659-
done();
660-
}
654+
beforeEach(() => {
655+
translate = null;
656+
extension = new GridMenuExtension({} as ExportService, {} as ExtensionUtility, {} as FilterService, { gridOptions: { enableTranslate: true } } as SharedService, {} as SortService, translate);
657+
});
658+
659+
it('should throw an error if "enableTranslate" is set but the I18N Service is null', () => {
660+
expect(() => extension.register()).toThrowError('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured');
661661
});
662662
});
663663
});

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,13 @@ describe('headerMenuExtension', () => {
502502
});
503503

504504
describe('without ngx-translate', () => {
505-
it('should throw an error if "enableTranslate" is set but the Translate Service is null', (done) => {
506-
try {
507-
extension = new HeaderMenuExtension({} as ExtensionUtility, {} as FilterService, { gridOptions: { enableTranslate: true } } as SharedService, {} as SortService, null);
508-
} catch (e) {
509-
expect(e.toString()).toContain('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured');
510-
done();
511-
}
505+
beforeEach(() => {
506+
translate = null;
507+
extension = new HeaderMenuExtension({} as ExtensionUtility, {} as FilterService, { gridOptions: { enableTranslate: true } } as SharedService, {} as SortService, translate);
508+
});
509+
510+
it('should throw an error if "enableTranslate" is set but the Translate Service is null', () => {
511+
expect(() => extension.register()).toThrowError('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured');
512512
});
513513
});
514514
});

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ export class GridMenuExtension implements Extension {
4141
private sortService: SortService,
4242
@Optional() private translate: TranslateService,
4343
) {
44-
if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) {
45-
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
46-
}
4744
this._eventHandler = new Slick.EventHandler();
4845
}
4946

@@ -69,6 +66,10 @@ export class GridMenuExtension implements Extension {
6966

7067
/** Create the Header Menu and expose all the available hooks that user can subscribe (onCommand, onBeforeMenuShow, ...) */
7168
register(): any {
69+
if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) {
70+
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
71+
}
72+
7273
// keep original user grid menu, useful when switching locale to translate
7374
this._userOriginalGridMenu = { ...this.sharedService.gridOptions.gridMenu };
7475

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ export class HeaderMenuExtension implements Extension {
3535
private sortService: SortService,
3636
@Optional() private translate: TranslateService,
3737
) {
38-
if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) {
39-
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
40-
}
4138
this._eventHandler = new Slick.EventHandler();
4239
}
4340

@@ -65,6 +62,10 @@ export class HeaderMenuExtension implements Extension {
6562
* @param columnDefinitions
6663
*/
6764
register(): any {
65+
if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) {
66+
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
67+
}
68+
6869
if (this.sharedService && this.sharedService.grid && this.sharedService.gridOptions) {
6970
// get locales provided by user in forRoot or else use default English locales via the Constants
7071
this._locales = this.sharedService.gridOptions && this.sharedService.gridOptions.locales || Constants.locales;

src/app/modules/angular-slickgrid/services/__tests__/extension.service.spec.ts

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const extensionHeaderMenuStub = {
5959

6060
describe('ExtensionService', () => {
6161
let service: ExtensionService;
62+
let sharedService: SharedService;
6263
let translate: TranslateService;
6364

6465
describe('with ngx-translate', () => {
@@ -87,6 +88,7 @@ describe('ExtensionService', () => {
8788
imports: [TranslateModule.forRoot()]
8889
});
8990
service = TestBed.get(ExtensionService);
91+
sharedService = TestBed.get(SharedService);
9092
translate = TestBed.get(TranslateService);
9193
translate.setTranslation('fr', { HELLO: 'Bonjour', WORLD: 'Monde' });
9294
translate.setTranslation('en', { HELLO: 'Hello', WORLD: 'World' });
@@ -593,26 +595,46 @@ describe('ExtensionService', () => {
593595
});
594596

595597
describe('without ngx-translate', () => {
596-
it('should throw an error if "enableTranslate" is set but the Translate Service is null', (done) => {
598+
beforeEach(() => {
599+
translate = null;
600+
service = new ExtensionService(
601+
// extensions
602+
extensionStub as unknown as AutoTooltipExtension,
603+
extensionStub as unknown as CellExternalCopyManagerExtension,
604+
extensionStub as unknown as CheckboxSelectorExtension,
605+
extensionColumnPickerStub as unknown as ColumnPickerExtension,
606+
extensionStub as unknown as DraggableGroupingExtension,
607+
extensionGridMenuStub as unknown as GridMenuExtension,
608+
extensionGroupItemMetaStub as unknown as GroupItemMetaProviderExtension,
609+
extensionStub as unknown as HeaderButtonExtension,
610+
extensionHeaderMenuStub as unknown as HeaderMenuExtension,
611+
extensionStub as unknown as RowDetailViewExtension,
612+
extensionStub as unknown as RowMoveManagerExtension,
613+
extensionStub as unknown as RowSelectionExtension,
614+
sharedService,
615+
translate,
616+
);
617+
618+
const gridOptionsMock = { enableTranslate: true } as GridOption;
619+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
620+
});
621+
622+
it('should throw an error if "enableTranslate" is set but the Translate Service is null and "translateColumnHeaders" method is called', () => {
623+
expect(() => service.translateColumnHeaders())
624+
.toThrowError('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured');
625+
});
626+
627+
it('should throw an error if "enableTranslate" is set but the Translate Service is null and "translateItems" private method is called', (done) => {
597628
try {
598-
service = new ExtensionService(
599-
{} as AutoTooltipExtension,
600-
{} as CellExternalCopyManagerExtension,
601-
{} as CheckboxSelectorExtension,
602-
{} as ColumnPickerExtension,
603-
{} as DraggableGroupingExtension,
604-
{} as GridMenuExtension,
605-
{} as GroupItemMetaProviderExtension,
606-
{} as HeaderButtonExtension,
607-
{} as HeaderMenuExtension,
608-
{} as RowDetailViewExtension,
609-
{} as RowMoveManagerExtension,
610-
{} as RowSelectionExtension,
611-
{ gridOptions: { enableTranslate: true } } as SharedService,
612-
null
613-
);
629+
const gridOptionsMock = { enableTranslate: true } as GridOption;
630+
const columnBeforeTranslate = { id: 'field1', field: 'field1', name: 'Hello', headerKey: 'HELLO' };
631+
const columnsMock = [columnBeforeTranslate] as Column[];
632+
jest.spyOn(SharedService.prototype, 'allColumns', 'get').mockReturnValue(columnsMock);
633+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
634+
635+
service.bindDifferentExtensions();
614636
} catch (e) {
615-
expect(e.toString()).toContain('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured');
637+
expect(e.message).toContain('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured');
616638
done();
617639
}
618640
});

src/app/modules/angular-slickgrid/services/extension.service.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ export class ExtensionService {
4646
private rowSelectionExtension: RowSelectionExtension,
4747
private sharedService: SharedService,
4848
@Optional() private translate: TranslateService,
49-
) {
50-
if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) {
51-
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
52-
}
53-
}
49+
) { }
5450

5551
/** Dispose of all the controls & plugins */
5652
dispose() {
@@ -122,7 +118,7 @@ export class ExtensionService {
122118
// Auto Tooltip Plugin
123119
if (this.sharedService.gridOptions.enableAutoTooltip) {
124120
if (this.autoTooltipExtension && this.autoTooltipExtension.register) {
125-
const instance = this.autoTooltipExtension.register()
121+
const instance = this.autoTooltipExtension.register();
126122
this._extensionList.push({ name: ExtensionName.autoTooltip, class: this.autoTooltipExtension, addon: instance, instance });
127123
}
128124
}
@@ -312,6 +308,10 @@ export class ExtensionService {
312308
* @param new column definitions (optional)
313309
*/
314310
translateColumnHeaders(locale?: boolean | string, newColumnDefinitions?: Column[]) {
311+
if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) {
312+
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
313+
}
314+
315315
if (locale) {
316316
this.translate.use(locale as string);
317317
}
@@ -356,6 +356,10 @@ export class ExtensionService {
356356

357357
/** Translate an array of items from an input key and assign translated value to the output key */
358358
private translateItems(items: any[], inputKey: string, outputKey: string) {
359+
if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) {
360+
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
361+
}
362+
359363
if (Array.isArray(items)) {
360364
for (const item of items) {
361365
if (item[inputKey]) {

0 commit comments

Comments
 (0)