Skip to content

Commit

Permalink
SHIBUI-814 Added translate component
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Sep 13, 2018
1 parent 61865fd commit 10f75e4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
4 changes: 4 additions & 0 deletions ui/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
"input": "src",
"output": "/"
}
],
"codeCoverageExclude": [
"src/polyfills.ts",
"**/*.stub.ts"
]
}
},
Expand Down
8 changes: 4 additions & 4 deletions ui/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AppComponent } from './app.component';
import * as fromRoot from './core/reducer';
import { NotificationModule } from './notification/notification.module';
import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap';
import { MockTranslatePipe, MockI18nService } from '../testing/i18n.stub';
import { MockTranslatePipe, MockI18nService, MockI18nModule } from '../testing/i18n.stub';
import { I18nService } from './i18n/service/i18n.service';

@Component({
Expand Down Expand Up @@ -38,12 +38,12 @@ describe('AppComponent', () => {
StoreModule.forRoot({
core: combineReducers(fromRoot.reducers)
}),
NotificationModule
NotificationModule,
MockI18nModule
],
declarations: [
AppComponent,
TestHostComponent,
MockTranslatePipe
TestHostComponent
],
}).compileComponents();

Expand Down
Empty file.
10 changes: 2 additions & 8 deletions ui/src/app/i18n/pipe/i18n.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as fromI18n from '../reducer';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { Component } from '@angular/core';
import { MessagesLoadSuccessAction } from '../action/message.action';
import { MockI18nService, MockI18nModule } from '../../../testing/i18n.stub';

@Component({
template: `
Expand All @@ -33,14 +34,7 @@ describe('Pipe: I18n translation', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: I18nService, useValue: {
interpolate: (value: string, interpolated: { [prop: string]: string } = {}): string => {
return Object.entries(interpolated).reduce((current, interpolate) => {
let reg = new RegExp(`{\\s*${interpolate[0]}\\s*}`, 'gm');
return current.replace(reg, interpolate[1]);
}, value);
}
} }
{ provide: I18nService, useClass: MockI18nService }
],
imports: [
CommonModule,
Expand Down
42 changes: 41 additions & 1 deletion ui/src/testing/i18n.stub.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* istanbul ignore */

import { PipeTransform, Pipe } from '@angular/core';
import { PipeTransform, Pipe, NgModule, Directive, Component } from '@angular/core';

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { CommonModule } from '@angular/common';

/*tslint:disable:component-selector */

@Pipe({
name: 'i18n',
Expand All @@ -18,6 +21,18 @@ export class MockTranslatePipe implements PipeTransform {
}
}

@Directive({
selector: '[translate]'
})
export class MockTranslateDirective {}


@Component({
selector: 'translate',
template: '<ng-content></ng-content>'
})
export class MockTranslateComponent { }

@Injectable()
export class MockI18nService {

Expand All @@ -42,7 +57,32 @@ export class MockI18nService {
return 'en-US';
}

translate (value: string, interpolated: any, messages): string {
return messages.hasOwnProperty(value) ? messages[value] : '';
}

interpolate(value: string, interpolated: { [prop: string]: string } = {}): string {
return value;
}
}


@NgModule({
imports: [
CommonModule
],
declarations: [
MockTranslatePipe,
MockTranslateDirective,
MockTranslateComponent
],
exports: [
MockTranslateComponent,
MockTranslateDirective,
MockTranslatePipe
],
providers: [
MockI18nService
]
})
export class MockI18nModule {}

0 comments on commit 10f75e4

Please sign in to comment.