Skip to content

Commit

Permalink
fix(build): remove extensionUtility DI to fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding-SE committed Jan 22, 2020
1 parent 2d10daf commit 42cc4b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Subject } from 'rxjs';
import { GroupingAndColspanService } from '../groupingAndColspan.service';
import { GridOption, SlickEventHandler, Column } from '../../models';
import { ResizerService, GridDimension } from '../resizer.service';
import { ExtensionUtility } from '../../extensions/extensionUtility';
import { SharedService } from '../shared.service';

declare var Slick: any;
Expand All @@ -25,10 +24,6 @@ const dataViewStub = {
reSort: jest.fn(),
};

const extensionUtilityStub = {
translateItems: jest.fn(),
};

const gridStub = {
autosizeColumns: jest.fn(),
getColumnIndex: jest.fn(),
Expand Down Expand Up @@ -88,7 +83,6 @@ describe('GroupingAndColspanService', () => {
providers: [
GroupingAndColspanService,
SharedService,
{ provide: ExtensionUtility, useValue: extensionUtilityStub },
{ provide: ResizerService, useValue: resizerServiceStub },
],
imports: [TranslateModule.forRoot()]
Expand All @@ -103,13 +97,15 @@ describe('GroupingAndColspanService', () => {
MALE: 'Male',
OK: 'OK',
OTHER: 'Other',
START: 'Start',
});
translate.setTranslation('fr', {
ALL_SELECTED: 'Tout sélectionnés',
FEMALE: 'Femme',
MALE: 'Mâle',
OK: 'Terminé',
OTHER: 'Autre',
START: 'Début',
});
translate.setDefaultLang('en');
});
Expand Down Expand Up @@ -145,7 +141,7 @@ describe('GroupingAndColspanService', () => {
{ id: 'title', name: 'Title', field: 'title', sortable: true, columnGroup: 'Common Factor' },
{ id: 'duration', name: 'Duration', field: 'duration', width: 100, columnGroup: 'Common Factor' },
{ id: 'category', name: 'Category', field: 'category', columnGroup: 'Common Factor' },
{ id: 'start', name: 'Start', field: 'start' },
{ id: 'start', name: 'Start', nameKey: 'START', field: 'start' },
];
gridStub.getColumns = jest.fn();
jest.spyOn(gridStub, 'getColumns').mockReturnValue(mockColumns);
Expand Down Expand Up @@ -215,7 +211,7 @@ describe('GroupingAndColspanService', () => {
it('should call the "renderPreHeaderRowGroupingTitles" after triggering a translate language change', () => {
gridOptionMock.enableTranslate = true;
const renderSpy = jest.spyOn(service, 'renderPreHeaderRowGroupingTitles');
const translateSpy = jest.spyOn(extensionUtilityStub, 'translateItems');
const translateSpy = jest.spyOn(service, 'translateItems');
const getColSpy = jest.spyOn(gridStub, 'getColumns');
const setColSpy = jest.spyOn(gridStub, 'setColumns');

Expand All @@ -228,6 +224,13 @@ describe('GroupingAndColspanService', () => {
expect(renderSpy).toHaveBeenCalled();
});

it('should translate the items when "translateItems" is called', () => {
translate.use('fr');
service.translateItems(mockColumns, 'nameKey', 'name');

expect(mockColumns[3]).toEqual({ id: 'start', name: 'Début', nameKey: 'START', field: 'start' });
});

it('should render the pre-header row grouping title DOM element', () => {
const spy = jest.spyOn(service, 'renderPreHeaderRowGroupingTitles');
const divHeaderColumns = document.getElementsByClassName('slick-header-columns');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Injectable, Optional } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

import { Column, GridOption, SlickEventHandler } from './../models/index';
import { ExtensionUtility } from '../extensions';
import { ResizerService } from './resizer.service';

// using external non-typed js libraries
Expand All @@ -16,7 +15,7 @@ export class GroupingAndColspanService {
private _eventHandler: SlickEventHandler;
private _grid: any;

constructor(private extensionUtility: ExtensionUtility, private resizerService: ResizerService, @Optional() private translate: TranslateService) {
constructor(private resizerService: ResizerService, @Optional() private translate: TranslateService) {
this._eventHandler = new Slick.EventHandler();
}

Expand Down Expand Up @@ -52,7 +51,7 @@ export class GroupingAndColspanService {
if (this._gridOptions.enableTranslate) {
this.translate.onLangChange.subscribe(() => {
const currentColumnDefinitions = this._grid.getColumns();
this.extensionUtility.translateItems(currentColumnDefinitions, 'columnGroupKey', 'columnGroup');
this.translateItems(currentColumnDefinitions, 'columnGroupKey', 'columnGroup');
this._grid.setColumns(currentColumnDefinitions);
this.renderPreHeaderRowGroupingTitles();
});
Expand Down Expand Up @@ -120,4 +119,15 @@ export class GroupingAndColspanService {
}
}
}

/** Translate the an array of items from an input key and assign to the output key */
translateItems(items: any[], inputKey: string, outputKey: string) {
if (Array.isArray(items)) {
for (const item of items) {
if (item[inputKey]) {
item[outputKey] = this.translate && this.translate && this.translate.instant && this.translate.instant(item[inputKey]);
}
}
}
}
}

0 comments on commit 42cc4b4

Please sign in to comment.