2016-12-08 14:26:38 +01:00
|
|
|
import { expect } from 'chai';
|
|
|
|
|
2016-12-08 14:28:59 +01:00
|
|
|
import { ServiceParser } from '../../src/parsers/service.parser';
|
2016-12-08 14:26:38 +01:00
|
|
|
|
|
|
|
describe('ServiceParser', () => {
|
|
|
|
const componentFilename: string = 'test.component.ts';
|
|
|
|
|
2019-09-19 15:22:39 +02:00
|
|
|
let parser: ServiceParser;
|
2016-12-08 14:26:38 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-09-19 15:22:39 +02:00
|
|
|
parser = new ServiceParser();
|
2016-12-08 14:26:38 +01:00
|
|
|
});
|
|
|
|
|
2019-06-13 11:23:37 +02:00
|
|
|
it('should support extracting binary expressions', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
const message = 'The Message';
|
|
|
|
this._translateService.get(message || 'Fallback message');
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Fallback message']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support conditional operator', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
const message = 'The Message';
|
|
|
|
this._translateService.get(message ? message : 'Fallback message');
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Fallback message']);
|
|
|
|
});
|
|
|
|
|
2019-09-18 14:16:47 +02:00
|
|
|
it("should extract strings in TranslateService's get() method", () => {
|
2016-12-08 14:26:38 +01:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.get('Hello World');
|
|
|
|
}
|
|
|
|
`;
|
2016-12-09 05:18:04 +01:00
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
2016-12-08 14:26:38 +01:00
|
|
|
});
|
|
|
|
|
2019-09-18 14:16:47 +02:00
|
|
|
it("should extract strings in TranslateService's instant() method", () => {
|
2016-12-08 14:26:38 +01:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.instant('Hello World');
|
|
|
|
}
|
|
|
|
`;
|
2016-12-09 05:18:04 +01:00
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
2016-12-08 14:26:38 +01:00
|
|
|
});
|
|
|
|
|
2019-09-18 14:16:47 +02:00
|
|
|
it("should extract strings in TranslateService's stream() method", () => {
|
2017-07-05 15:18:41 +02:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.stream('Hello World');
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
|
|
|
});
|
|
|
|
|
2019-09-18 14:16:47 +02:00
|
|
|
it("should extract array of strings in TranslateService's get() method", () => {
|
2016-12-08 15:24:23 +01:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.get(['Hello', 'World']);
|
|
|
|
}
|
|
|
|
`;
|
2016-12-09 05:18:04 +01:00
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello', 'World']);
|
2016-12-08 15:24:23 +01:00
|
|
|
});
|
|
|
|
|
2019-09-18 14:16:47 +02:00
|
|
|
it("should extract array of strings in TranslateService's instant() method", () => {
|
2016-12-08 15:24:23 +01:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.instant(['Hello', 'World']);
|
|
|
|
}
|
|
|
|
`;
|
2016-12-09 05:18:04 +01:00
|
|
|
const key = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(key).to.deep.equal(['Hello', 'World']);
|
2016-12-08 15:24:23 +01:00
|
|
|
});
|
|
|
|
|
2019-09-18 14:16:47 +02:00
|
|
|
it("should extract array of strings in TranslateService's stream() method", () => {
|
2017-07-05 15:18:41 +02:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.stream(['Hello', 'World']);
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const key = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(key).to.deep.equal(['Hello', 'World']);
|
|
|
|
});
|
|
|
|
|
2019-08-26 12:29:52 +02:00
|
|
|
it('should extract string arrays encapsulated in backticks', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.get([\`Hello\`, \`World\`]);
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello', 'World']);
|
|
|
|
});
|
|
|
|
|
2017-07-05 15:18:41 +02:00
|
|
|
it('should not extract strings in get()/instant()/stream() methods of other services', () => {
|
2016-12-08 14:26:38 +01: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');
|
2017-07-05 15:18:41 +02:00
|
|
|
this._otherService.stream('Hi there');
|
2016-12-08 14:26:38 +01:00
|
|
|
}
|
|
|
|
`;
|
2016-12-09 05:18:04 +01:00
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal([]);
|
2016-12-08 14:26:38 +01:00
|
|
|
});
|
|
|
|
|
2016-12-13 15:17:03 +01: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-28 15:06:05 +01:00
|
|
|
it('should not extract string when not accessing property', () => {
|
2017-01-16 07:13:18 +01:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected trans: TranslateService) { }
|
|
|
|
public test() {
|
2017-03-20 15:29:41 +01:00
|
|
|
trans.get("You are expected at {{time}}", {time: moment.format('H:mm')}).subscribe();
|
2017-01-16 07:13:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
2017-01-28 15:06:05 +01:00
|
|
|
expect(keys).to.deep.equal([]);
|
2017-01-16 07:13:18 +01:00
|
|
|
});
|
|
|
|
|
2017-03-20 15:29:41 +01:00
|
|
|
it('should extract string with params on same line', () => {
|
2017-01-28 15:22:08 +01:00
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService: TranslateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.get('You are expected at {{time}}', {time: moment.format('H:mm')});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['You are expected at {{time}}']);
|
2017-03-20 15:29:41 +01:00
|
|
|
});
|
2017-01-28 15:22:08 +01:00
|
|
|
|
2017-05-05 11:31:30 +02:00
|
|
|
it('should not crash when constructor parameter has no type', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected _translateService) { }
|
|
|
|
public test() {
|
|
|
|
this._translateService.instant('Hello World');
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal([]);
|
|
|
|
});
|
|
|
|
|
2019-07-08 14:36:44 +02:00
|
|
|
it('should not extract variables', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({ })
|
|
|
|
export class AppComponent {
|
|
|
|
public constructor(protected translateService: TranslateService) { }
|
|
|
|
public test() {
|
2019-07-17 13:00:29 +02:00
|
|
|
this.translateService.get(["yes", variable]).then(translations => {
|
2019-07-08 14:36:44 +02:00
|
|
|
console.log(translations[variable]);
|
|
|
|
});
|
|
|
|
}
|
2019-09-16 16:40:37 +02:00
|
|
|
}
|
2019-07-08 14:36:44 +02:00
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['yes']);
|
|
|
|
});
|
|
|
|
|
2017-05-09 20:08:44 +02:00
|
|
|
it('should extract strings from all classes in the file', () => {
|
|
|
|
const contents = `
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
export class Stuff {
|
|
|
|
thing: string;
|
2017-05-10 14:10:13 +02:00
|
|
|
translate: any;
|
2017-05-09 20:08:44 +02:00
|
|
|
constructor(thing: string) {
|
2017-05-10 14:10:13 +02:00
|
|
|
this.translate.get('Not me');
|
2017-05-09 20:08:44 +02:00
|
|
|
this.thing = thing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Injectable()
|
2017-05-10 14:10:13 +02:00
|
|
|
export class MyComponent {
|
|
|
|
constructor(public translate: TranslateService) {
|
|
|
|
this.translate.instant("Extract me!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export class OtherClass {
|
|
|
|
constructor(thing: string, _translate: TranslateService) {
|
|
|
|
this._translate.get("Do not extract me");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Injectable()
|
2017-05-09 20:08:44 +02:00
|
|
|
export class AuthService {
|
|
|
|
constructor(public translate: TranslateService) {
|
2017-05-10 14:10:13 +02:00
|
|
|
this.translate.instant("Hello!");
|
2017-05-09 20:08:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
2017-05-10 14:10:13 +02:00
|
|
|
expect(keys).to.deep.equal(['Extract me!', 'Hello!']);
|
2017-05-09 20:08:44 +02:00
|
|
|
});
|
|
|
|
|
2019-09-16 16:40:37 +02:00
|
|
|
it('should extract strings when TranslateService is declared as a property', () => {
|
|
|
|
const contents = `
|
|
|
|
export class MyComponent {
|
|
|
|
protected translateService: TranslateService;
|
|
|
|
public constructor() {
|
|
|
|
this.translateService = new TranslateService();
|
|
|
|
}
|
|
|
|
public test() {
|
|
|
|
this.translateService.instant('Hello World');
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract strings passed to TranslateServices methods only', () => {
|
|
|
|
const contents = `
|
|
|
|
export class AppComponent implements OnInit {
|
|
|
|
constructor(protected config: Config, protected translateService: TranslateService) {}
|
|
|
|
|
|
|
|
public ngOnInit(): void {
|
|
|
|
this.localizeBackButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected localizeBackButton(): void {
|
|
|
|
this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
|
|
|
|
this.config.set('backButtonText', this.translateService.instant('Back'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Back']);
|
|
|
|
});
|
2016-12-08 14:26:38 +01:00
|
|
|
});
|