diff --git a/src/app/modules/angular-slickgrid/components/slick-pagination.component.ts b/src/app/modules/angular-slickgrid/components/slick-pagination.component.ts index c69d5886e..22f9b9183 100644 --- a/src/app/modules/angular-slickgrid/components/slick-pagination.component.ts +++ b/src/app/modules/angular-slickgrid/components/slick-pagination.component.ts @@ -55,9 +55,6 @@ export class SlickPaginationComponent implements AfterViewInit, OnDestroy { /** Constructor */ constructor(private filterService: FilterService, private gridService: GridService, @Optional() private translate: TranslateService) { - if (this._gridPaginationOptions && this._gridPaginationOptions.enableTranslate && !this.translate) { - throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.'); - } // translate all the text using ngx-translate or custom locales if (translate && translate.onLangChange) { this.subscriptions.push(this.translate.onLangChange.subscribe(() => this.translateAllUiTexts(this._locales))); @@ -69,7 +66,10 @@ export class SlickPaginationComponent implements AfterViewInit, OnDestroy { } ngAfterViewInit() { - this._gridPaginationOptions = this._gridPaginationOptions; + if (this._gridPaginationOptions && this._gridPaginationOptions.enableTranslate && !this.translate) { + throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.'); + } + if (!this._gridPaginationOptions || !this._gridPaginationOptions.pagination || (this._gridPaginationOptions.pagination.totalItems !== this.totalItems)) { this.refreshPagination(); } diff --git a/src/app/modules/angular-slickgrid/extensions/__tests__/gridMenuExtension.spec.ts b/src/app/modules/angular-slickgrid/extensions/__tests__/gridMenuExtension.spec.ts index 4cb5fb39c..cf77737e4 100644 --- a/src/app/modules/angular-slickgrid/extensions/__tests__/gridMenuExtension.spec.ts +++ b/src/app/modules/angular-slickgrid/extensions/__tests__/gridMenuExtension.spec.ts @@ -651,13 +651,13 @@ describe('gridMenuExtension', () => { }); describe('without ngx-translate', () => { - it('should throw an error if "enableTranslate" is set but the Translate Service is null', (done) => { - try { - extension = new GridMenuExtension({} as ExportService, {} as ExtensionUtility, {} as FilterService, { gridOptions: { enableTranslate: true } } as SharedService, {} as SortService, null); - } catch (e) { - expect(e.toString()).toContain('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured'); - done(); - } + beforeEach(() => { + translate = null; + extension = new GridMenuExtension({} as ExportService, {} as ExtensionUtility, {} as FilterService, { gridOptions: { enableTranslate: true } } as SharedService, {} as SortService, translate); + }); + + it('should throw an error if "enableTranslate" is set but the I18N Service is null', () => { + expect(() => extension.register()).toThrowError('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured'); }); }); }); diff --git a/src/app/modules/angular-slickgrid/extensions/__tests__/headerMenuExtension.spec.ts b/src/app/modules/angular-slickgrid/extensions/__tests__/headerMenuExtension.spec.ts index 1e875666b..273a9fa0e 100644 --- a/src/app/modules/angular-slickgrid/extensions/__tests__/headerMenuExtension.spec.ts +++ b/src/app/modules/angular-slickgrid/extensions/__tests__/headerMenuExtension.spec.ts @@ -502,13 +502,13 @@ describe('headerMenuExtension', () => { }); describe('without ngx-translate', () => { - it('should throw an error if "enableTranslate" is set but the Translate Service is null', (done) => { - try { - extension = new HeaderMenuExtension({} as ExtensionUtility, {} as FilterService, { gridOptions: { enableTranslate: true } } as SharedService, {} as SortService, null); - } catch (e) { - expect(e.toString()).toContain('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured'); - done(); - } + beforeEach(() => { + translate = null; + extension = new HeaderMenuExtension({} as ExtensionUtility, {} as FilterService, { gridOptions: { enableTranslate: true } } as SharedService, {} as SortService, translate); + }); + + it('should throw an error if "enableTranslate" is set but the Translate Service is null', () => { + expect(() => extension.register()).toThrowError('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured'); }); }); }); diff --git a/src/app/modules/angular-slickgrid/extensions/gridMenuExtension.ts b/src/app/modules/angular-slickgrid/extensions/gridMenuExtension.ts index 13f793bbb..0501f5102 100644 --- a/src/app/modules/angular-slickgrid/extensions/gridMenuExtension.ts +++ b/src/app/modules/angular-slickgrid/extensions/gridMenuExtension.ts @@ -41,9 +41,6 @@ export class GridMenuExtension implements Extension { private sortService: SortService, @Optional() private translate: TranslateService, ) { - if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) { - throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.'); - } this._eventHandler = new Slick.EventHandler(); } @@ -69,6 +66,10 @@ export class GridMenuExtension implements Extension { /** Create the Header Menu and expose all the available hooks that user can subscribe (onCommand, onBeforeMenuShow, ...) */ register(): any { + if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) { + throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.'); + } + // keep original user grid menu, useful when switching locale to translate this._userOriginalGridMenu = { ...this.sharedService.gridOptions.gridMenu }; diff --git a/src/app/modules/angular-slickgrid/extensions/headerMenuExtension.ts b/src/app/modules/angular-slickgrid/extensions/headerMenuExtension.ts index 7761ef885..cebf2c4ad 100644 --- a/src/app/modules/angular-slickgrid/extensions/headerMenuExtension.ts +++ b/src/app/modules/angular-slickgrid/extensions/headerMenuExtension.ts @@ -35,9 +35,6 @@ export class HeaderMenuExtension implements Extension { private sortService: SortService, @Optional() private translate: TranslateService, ) { - if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) { - throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.'); - } this._eventHandler = new Slick.EventHandler(); } @@ -65,6 +62,10 @@ export class HeaderMenuExtension implements Extension { * @param columnDefinitions */ register(): any { + if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) { + throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.'); + } + if (this.sharedService && this.sharedService.grid && this.sharedService.gridOptions) { // get locales provided by user in forRoot or else use default English locales via the Constants this._locales = this.sharedService.gridOptions && this.sharedService.gridOptions.locales || Constants.locales; diff --git a/src/app/modules/angular-slickgrid/services/__tests__/extension.service.spec.ts b/src/app/modules/angular-slickgrid/services/__tests__/extension.service.spec.ts index 3a96aca95..c64df58bc 100644 --- a/src/app/modules/angular-slickgrid/services/__tests__/extension.service.spec.ts +++ b/src/app/modules/angular-slickgrid/services/__tests__/extension.service.spec.ts @@ -59,6 +59,7 @@ const extensionHeaderMenuStub = { describe('ExtensionService', () => { let service: ExtensionService; + let sharedService: SharedService; let translate: TranslateService; describe('with ngx-translate', () => { @@ -87,6 +88,7 @@ describe('ExtensionService', () => { imports: [TranslateModule.forRoot()] }); service = TestBed.get(ExtensionService); + sharedService = TestBed.get(SharedService); translate = TestBed.get(TranslateService); translate.setTranslation('fr', { HELLO: 'Bonjour', WORLD: 'Monde' }); translate.setTranslation('en', { HELLO: 'Hello', WORLD: 'World' }); @@ -593,26 +595,46 @@ describe('ExtensionService', () => { }); describe('without ngx-translate', () => { - it('should throw an error if "enableTranslate" is set but the Translate Service is null', (done) => { + beforeEach(() => { + translate = null; + service = new ExtensionService( + // extensions + extensionStub as unknown as AutoTooltipExtension, + extensionStub as unknown as CellExternalCopyManagerExtension, + extensionStub as unknown as CheckboxSelectorExtension, + extensionColumnPickerStub as unknown as ColumnPickerExtension, + extensionStub as unknown as DraggableGroupingExtension, + extensionGridMenuStub as unknown as GridMenuExtension, + extensionGroupItemMetaStub as unknown as GroupItemMetaProviderExtension, + extensionStub as unknown as HeaderButtonExtension, + extensionHeaderMenuStub as unknown as HeaderMenuExtension, + extensionStub as unknown as RowDetailViewExtension, + extensionStub as unknown as RowMoveManagerExtension, + extensionStub as unknown as RowSelectionExtension, + sharedService, + translate, + ); + + const gridOptionsMock = { enableTranslate: true } as GridOption; + jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock); + }); + + it('should throw an error if "enableTranslate" is set but the Translate Service is null and "translateColumnHeaders" method is called', () => { + expect(() => service.translateColumnHeaders()) + .toThrowError('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured'); + }); + + it('should throw an error if "enableTranslate" is set but the Translate Service is null and "translateItems" private method is called', (done) => { try { - service = new ExtensionService( - {} as AutoTooltipExtension, - {} as CellExternalCopyManagerExtension, - {} as CheckboxSelectorExtension, - {} as ColumnPickerExtension, - {} as DraggableGroupingExtension, - {} as GridMenuExtension, - {} as GroupItemMetaProviderExtension, - {} as HeaderButtonExtension, - {} as HeaderMenuExtension, - {} as RowDetailViewExtension, - {} as RowMoveManagerExtension, - {} as RowSelectionExtension, - { gridOptions: { enableTranslate: true } } as SharedService, - null - ); + const gridOptionsMock = { enableTranslate: true } as GridOption; + const columnBeforeTranslate = { id: 'field1', field: 'field1', name: 'Hello', headerKey: 'HELLO' }; + const columnsMock = [columnBeforeTranslate] as Column[]; + jest.spyOn(SharedService.prototype, 'allColumns', 'get').mockReturnValue(columnsMock); + jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock); + + service.bindDifferentExtensions(); } catch (e) { - expect(e.toString()).toContain('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured'); + expect(e.message).toContain('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured'); done(); } }); diff --git a/src/app/modules/angular-slickgrid/services/extension.service.ts b/src/app/modules/angular-slickgrid/services/extension.service.ts index 0ef19bed1..fe529c065 100644 --- a/src/app/modules/angular-slickgrid/services/extension.service.ts +++ b/src/app/modules/angular-slickgrid/services/extension.service.ts @@ -46,11 +46,7 @@ export class ExtensionService { private rowSelectionExtension: RowSelectionExtension, private sharedService: SharedService, @Optional() private translate: TranslateService, - ) { - if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) { - throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.'); - } - } + ) { } /** Dispose of all the controls & plugins */ dispose() { @@ -122,7 +118,7 @@ export class ExtensionService { // Auto Tooltip Plugin if (this.sharedService.gridOptions.enableAutoTooltip) { if (this.autoTooltipExtension && this.autoTooltipExtension.register) { - const instance = this.autoTooltipExtension.register() + const instance = this.autoTooltipExtension.register(); this._extensionList.push({ name: ExtensionName.autoTooltip, class: this.autoTooltipExtension, addon: instance, instance }); } } @@ -312,6 +308,10 @@ export class ExtensionService { * @param new column definitions (optional) */ translateColumnHeaders(locale?: boolean | string, newColumnDefinitions?: Column[]) { + if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) { + throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.'); + } + if (locale) { this.translate.use(locale as string); } @@ -356,6 +356,10 @@ export class ExtensionService { /** Translate an array of items from an input key and assign translated value to the output key */ private translateItems(items: any[], inputKey: string, outputKey: string) { + if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) { + throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.'); + } + if (Array.isArray(items)) { for (const item of items) { if (item[inputKey]) {