2016-12-08 16:26:38 +03:00
|
|
|
import { expect } from 'chai';
|
|
|
|
|
2016-12-08 16:28:59 +03:00
|
|
|
import { ServiceParser } from '../../src/parsers/service.parser';
|
2016-12-08 16:26:38 +03:00
|
|
|
|
|
|
|
class TestServiceParser extends ServiceParser {
|
|
|
|
|
|
|
|
public extractTranslateServiceVar(contents: string): string {
|
|
|
|
return this._extractTranslateServiceVar(contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('ServiceParser', () => {
|
|
|
|
|
|
|
|
const componentFilename: string = 'test.component.ts';
|
|
|
|
|
|
|
|
let parser: TestServiceParser;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
parser = new TestServiceParser();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract variable used for TranslateService', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(
|
|
|
|
_serviceA: ServiceA,
|
|
|
|
public _serviceB: ServiceB,
|
|
|
|
protected _translateService: TranslateService
|
|
|
|
) { }
|
|
|
|
`;
|
2016-12-09 07:18:04 +03:00
|
|
|
const name = parser.extractTranslateServiceVar(contents);
|
|
|
|
expect(name).to.equal('_translateService');
|
2016-12-08 16:26:38 +03:00
|
|
|
});
|
|
|
|
|
2016-12-08 17:24:23 +03:00
|
|
|
it('should extract strings in TranslateService\'s get() method', () => {
|
2016-12-08 16:26:38 +03:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.get('Hello World');
|
|
|
|
}
|
|
|
|
`;
|
2016-12-09 07:18:04 +03:00
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
2016-12-08 16:26:38 +03:00
|
|
|
});
|
|
|
|
|
2016-12-08 17:24:23 +03:00
|
|
|
it('should extract strings in TranslateService\'s instant() method', () => {
|
2016-12-08 16:26:38 +03:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.instant('Hello World');
|
|
|
|
}
|
|
|
|
`;
|
2016-12-09 07:18:04 +03:00
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
2016-12-08 16:26:38 +03:00
|
|
|
});
|
|
|
|
|
2016-12-08 17:24:23 +03:00
|
|
|
it('should extract array of strings in TranslateService\'s get() method', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.get(['Hello', 'World']);
|
|
|
|
}
|
|
|
|
`;
|
2016-12-09 07:18:04 +03:00
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello', 'World']);
|
2016-12-08 17:24:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract array of strings in TranslateService\'s instant() method', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.instant(['Hello', 'World']);
|
|
|
|
}
|
|
|
|
`;
|
2016-12-09 07:18:04 +03:00
|
|
|
const key = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(key).to.deep.equal(['Hello', 'World']);
|
2016-12-08 17:24:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not extract strings in get()/instant() methods of other services', () => {
|
2016-12-08 16:26:38 +03:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(
|
|
|
|
protected _translateService: TranslateService,
|
|
|
|
protected _otherService: OtherService
|
|
|
|
) { }
|
|
|
|
public test() {
|
|
|
|
this._otherService.get('Hello World');
|
|
|
|
this._otherService.instant('Hi there');
|
|
|
|
}
|
|
|
|
`;
|
2016-12-09 07:18:04 +03:00
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal([]);
|
2016-12-08 16:26:38 +03:00
|
|
|
});
|
|
|
|
|
2016-12-13 17:17:03 +03:00
|
|
|
it('should extract strings with liberal spacing', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(
|
|
|
|
protected _translateService: TranslateService,
|
|
|
|
protected _otherService: OtherService
|
|
|
|
) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.instant('Hello');
|
|
|
|
this._translateService.get ( 'World' );
|
|
|
|
this._translateService.instant ( ['How'] );
|
|
|
|
this._translateService.get([ 'Are' ]);
|
|
|
|
this._translateService.get([ 'You' , 'Today' ]);
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello', 'World', 'How', 'Are', 'You', 'Today']);
|
|
|
|
});
|
|
|
|
|
2017-01-16 09:13:18 +03:00
|
|
|
it('should extract strings with params', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected trans: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
trans.get("You are expected at {{time}}", {time: moment.format('H:mm')}).subscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['You are expected at {{time}}']);
|
|
|
|
});
|
|
|
|
|
2016-12-08 16:26:38 +03:00
|
|
|
});
|