Skip to content

Commit

Permalink
feat(locales): add unit tests when using locales with enableTranslate
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Ghislain Beaulac authored and Ghislain Beaulac committed Jul 29, 2019
1 parent 413ea71 commit ae48ddf
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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 };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const extensionHeaderMenuStub = {

describe('ExtensionService', () => {
let service: ExtensionService;
let sharedService: SharedService;
let translate: TranslateService;

describe('with ngx-translate', () => {
Expand Down Expand Up @@ -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' });
Expand Down Expand Up @@ -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();
}
});
Expand Down
16 changes: 10 additions & 6 deletions src/app/modules/angular-slickgrid/services/extension.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 });
}
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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]) {
Expand Down

0 comments on commit ae48ddf

Please sign in to comment.