ngx-translate-extract/tests/parsers/pipe.parser.spec.ts

205 lines
8.2 KiB
TypeScript
Raw Permalink Normal View History

2016-12-08 14:12:43 +01:00
import { expect } from 'chai';
2016-12-08 14:28:59 +01:00
import { PipeParser } from '../../src/parsers/pipe.parser';
2016-12-08 14:12:43 +01:00
describe('PipeParser', () => {
const templateFilename: string = 'test.template.html';
let parser: PipeParser;
beforeEach(() => {
parser = new PipeParser();
});
2017-01-13 09:34:34 +01:00
it('should only extract string using pipe', () => {
const contents = `<button [style.background]="'lime'">{{ 'SomeKey_NotWorking' | translate }}</button>`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['SomeKey_NotWorking']);
});
it('should extract string using pipe, but between quotes only', () => {
const contents = `<input class="form-control" type="text" placeholder="{{'user.settings.form.phone.placeholder' | translate}}" [formControl]="settingsForm.controls['phone']">`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['user.settings.form.phone.placeholder']);
});
2016-12-08 14:12:43 +01:00
it('should extract interpolated strings using translate pipe', () => {
const contents = `Hello {{ 'World' | translate }}`;
const keys = parser.extract(contents, templateFilename).keys();
2019-07-08 14:36:44 +02:00
expect(keys).to.deep.equal(['World']);
});
it('should extract interpolated strings when translate pipe is used before other pipes', () => {
2019-07-08 14:36:44 +02:00
const contents = `Hello {{ 'World' | translate | upper }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['World']);
2016-12-08 14:12:43 +01:00
});
it('should extract interpolated strings when translate pipe is used after other pipes', () => {
const contents = `Hello {{ 'World' | upper | translate }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['World']);
});
it('should extract strings from ternary operators inside interpolations', () => {
const contents = `{{ (condition ? 'Hello' : 'World') | translate }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello', 'World']);
});
it('should extract strings from ternary operators right expression', () => {
const contents = `{{ condition ? null : ('World' | translate) }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['World']);
});
it('should extract strings from ternary operators inside attribute bindings', () => {
const contents = `<span [attr]="condition ? null : ('World' | translate)"></span>`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['World']);
});
it('should extract strings from ternary operators left expression', () => {
const contents = `{{ condition ? ('World' | translate) : null }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['World']);
});
it('should extract strings inside string concatenation', () => {
const contents = `{{ 'a' + ('Hello' | translate) + 'b' + 'c' + ('World' | translate) + 'd' }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello', 'World']);
});
it('should extract strings from object', () => {
const contents = `{{ { foo: 'Hello' | translate, bar: ['World' | translate], deep: { nested: { baz: 'Yes' | translate } } } | json }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello', 'World', 'Yes']);
});
it('should extract strings from ternary operators inside attribute bindings', () => {
const contents = `<span [attr]="(condition ? 'Hello' : 'World') | translate"></span>`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello', 'World']);
});
it('should extract strings from nested expressions', () => {
const contents = `<span [attr]="{ foo: ['a' + ((condition ? 'Hello' : 'World') | translate) + 'b'] }"></span>`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello', 'World']);
});
it('should extract strings from nested ternary operators ', () => {
const contents = `<h3>{{ (condition ? 'Hello' : anotherCondition ? 'Nested' : 'World' ) | translate }}</h3>`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello', 'Nested', 'World']);
});
it('should extract strings from ternary operators inside attribute interpolations', () => {
const contents = `<span attr="{{(condition ? 'Hello' : 'World') | translate}}"></span>`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello', 'World']);
});
it('should extract strings with escaped quotes', () => {
const contents = `Hello {{ 'World\\'s largest potato' | translate }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal([`World's largest potato`]);
});
it('should extract strings with multiple escaped quotes', () => {
const contents = `{{ 'C\\'est ok. C\\'est ok' | translate }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal([`C'est ok. C'est ok`]);
});
2016-12-08 14:12:43 +01:00
it('should extract interpolated strings using translate pipe in attributes', () => {
2019-06-13 12:17:04 +02:00
const contents = `<span attr="{{ 'Hello World' | translate }}"></span>`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello World']);
2016-12-08 14:12:43 +01:00
});
it('should extract bound strings using translate pipe in attributes', () => {
2019-06-13 12:17:04 +02:00
const contents = `<span [attr]="'Hello World' | translate"></span>`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello World']);
2016-12-08 14:12:43 +01:00
});
it('should extract multiple entries from nodes', () => {
2016-12-20 16:18:40 +01:00
const contents = `
<ion-header>
<ion-navbar color="brand">
2019-06-13 12:17:04 +02:00
<ion-title>{{ 'Info' | translate }}</ion-title>
2016-12-20 16:18:40 +01:00
</ion-navbar>
</ion-header>
<ion-content>
<content-loading *ngIf="isLoading">
2019-06-13 12:17:04 +02:00
{{ 'Loading...' | translate }}
2016-12-20 16:18:40 +01:00
</content-loading>
</ion-content>
`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Info', 'Loading...']);
});
2016-12-20 16:42:05 +01:00
it('should extract strings on same line', () => {
2019-06-13 12:17:04 +02:00
const contents = `<span [attr]="'Hello' | translate"></span><span [attr]="'World' | translate"></span>`;
2016-12-20 16:42:05 +01:00
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello', 'World']);
});
it('should extract strings from this template', () => {
const contents = `
<ion-list inset>
<ion-item>
<ion-icon item-left name="person" color="dark"></ion-icon>
2019-06-13 12:17:04 +02:00
<ion-input formControlName="name" type="text" [placeholder]="'Name' | translate"></ion-input>
2016-12-20 16:42:05 +01:00
</ion-item>
<ion-item>
<p color="danger" danger *ngFor="let error of form.get('name').getError('remote')">
{{ error }}
</p>
</ion-item>
</ion-list>
<div class="form-actions">
2019-06-13 12:17:04 +02:00
<button ion-button (click)="onSubmit()" color="secondary" block>{{ 'Create account' | translate }}</button>
2016-12-20 16:42:05 +01:00
</div>
`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Name', 'Create account']);
});
2019-06-13 12:17:04 +02:00
it('should not extract variables', () => {
const contents = '<p>{{ message | translate }}</p>';
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal([]);
});
it('should be able to extract without html', () => {
const contents = `{{ 'message' | translate }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['message']);
});
it('should ignore calculated values', () => {
const contents = `{{ 'SOURCES.' + source.name + '.NAME_PLURAL' | translate }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal([]);
});
2020-03-22 10:45:52 +01:00
it('should not extract pipe argument', () => {
const contents = `{{ value | valueToTranslationKey: 'argument' | translate }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal([]);
});
it('should extract strings from piped arguments inside a function calls on templates', () => {
const contents = `{{ callMe('Hello' | translate, 'World' | translate ) }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal([`Hello`, `World`]);
});
2016-12-08 14:12:43 +01:00
});