2016-12-08 13:53:13 +01:00
|
|
|
import { expect } from 'chai';
|
|
|
|
|
2016-12-08 14:28:59 +01:00
|
|
|
import { DirectiveParser } from '../../src/parsers/directive.parser';
|
2016-12-08 13:53:13 +01:00
|
|
|
|
|
|
|
describe('DirectiveParser', () => {
|
|
|
|
const templateFilename: string = 'test.template.html';
|
|
|
|
const componentFilename: string = 'test.component.ts';
|
|
|
|
|
2019-06-18 15:54:58 +02:00
|
|
|
let parser: DirectiveParser;
|
2016-12-08 13:53:13 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-06-18 15:54:58 +02:00
|
|
|
parser = new DirectiveParser();
|
2016-12-08 13:53:13 +01:00
|
|
|
});
|
|
|
|
|
2020-05-28 19:35:26 +02:00
|
|
|
|
|
|
|
it('should extract keys when using literal map in bound attribute', () => {
|
|
|
|
const contents = `<div [translate]="{ key1: 'value1' | translate, key2: 'value2' | translate }"></div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['value1', 'value2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract keys when using literal arrays in bound attribute', () => {
|
|
|
|
const contents = `<div [translate]="[ 'value1' | translate, 'value2' | translate ]"></div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['value1', 'value2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract keys when using binding pipe in bound attribute', () => {
|
|
|
|
const contents = `<div [translate]="'KEY1' | withPipe"></div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['KEY1']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract keys when using binary expression in bound attribute', () => {
|
|
|
|
const contents = `<div [translate]="keyVar || 'KEY1'"></div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['KEY1']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract keys when using literal primitive in bound attribute', () => {
|
|
|
|
const contents = `<div [translate]="'KEY1'"></div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['KEY1']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract keys when using conditional in bound attribute', () => {
|
|
|
|
const contents = `<div [translate]="condition ? 'KEY1' : 'KEY2'"></div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['KEY1', 'KEY2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract keys when using nested conditionals in bound attribute', () => {
|
|
|
|
const contents = `<div [translate]="isSunny ? (isWarm ? 'Sunny and warm' : 'Sunny but cold') : 'Not sunny'"></div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Sunny and warm', 'Sunny but cold', 'Not sunny']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract keys when using interpolation', () => {
|
|
|
|
const contents = `<div translate="{{ 'KEY1' + key2 + 'KEY3' }}"></div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['KEY1', 'KEY3']);
|
|
|
|
});
|
|
|
|
|
2020-05-28 00:09:53 +02:00
|
|
|
it('should extract keys keeping proper whitespace', () => {
|
|
|
|
const contents = `
|
|
|
|
<div translate>
|
|
|
|
Wubba
|
|
|
|
Lubba
|
|
|
|
Dub Dub
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Wubba Lubba Dub Dub']);
|
|
|
|
});
|
|
|
|
|
2020-05-28 00:28:13 +02:00
|
|
|
it('should use element contents as key when no translate attribute value is present', () => {
|
2019-06-18 15:54:58 +02:00
|
|
|
const contents = '<div translate>Hello World</div>';
|
2016-12-09 05:18:04 +01:00
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2019-06-18 15:54:58 +02:00
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
2016-12-08 13:53:13 +01:00
|
|
|
});
|
|
|
|
|
2020-05-28 00:28:13 +02:00
|
|
|
it('should use translate attribute value as key when present', () => {
|
2019-06-18 15:54:58 +02:00
|
|
|
const contents = '<div translate="MY_KEY">Hello World<div>';
|
2016-12-09 05:18:04 +01:00
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2019-06-18 15:54:58 +02:00
|
|
|
expect(keys).to.deep.equal(['MY_KEY']);
|
2016-12-08 13:53:13 +01:00
|
|
|
});
|
|
|
|
|
2020-05-28 00:28:13 +02:00
|
|
|
it('should extract keys from child elements when translate attribute is present', () => {
|
2019-06-18 15:54:58 +02:00
|
|
|
const contents = `<div translate>Hello <strong translate>World</strong></div>`;
|
2016-12-09 05:18:04 +01:00
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2020-05-28 00:09:53 +02:00
|
|
|
expect(keys).to.deep.equal(['Hello', 'World']);
|
2016-12-08 13:53:13 +01:00
|
|
|
});
|
|
|
|
|
2020-05-28 00:28:13 +02:00
|
|
|
it('should not extract keys from child elements when translate attribute is not present', () => {
|
2020-04-01 12:19:30 +02:00
|
|
|
const contents = `<div translate>Hello <strong>World</strong></div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2020-05-28 00:09:53 +02:00
|
|
|
expect(keys).to.deep.equal(['Hello']);
|
2020-04-01 12:19:30 +02:00
|
|
|
});
|
|
|
|
|
2016-12-08 13:53:13 +01:00
|
|
|
it('should extract and parse inline template', () => {
|
|
|
|
const contents = `
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
template: '<p translate>Hello World</p>'
|
|
|
|
})
|
|
|
|
export class TestComponent { }
|
|
|
|
`;
|
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 13:53:13 +01:00
|
|
|
});
|
|
|
|
|
2019-06-18 15:54:58 +02:00
|
|
|
it('should extract contents when no translate attribute value is provided', () => {
|
|
|
|
const contents = '<div translate>Hello World</div>';
|
2016-12-09 05:18:04 +01:00
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
2016-12-08 15:43:38 +01:00
|
|
|
});
|
|
|
|
|
2019-06-18 15:54:58 +02:00
|
|
|
it('should extract translate attribute value if provided', () => {
|
|
|
|
const contents = '<div translate="KEY">Hello World<div>';
|
2016-12-09 05:18:04 +01:00
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['KEY']);
|
2016-12-08 16:09:06 +01:00
|
|
|
});
|
|
|
|
|
2016-12-10 04:14:13 +01:00
|
|
|
it('should not extract translate pipe in html tag', () => {
|
2019-06-13 12:17:04 +02:00
|
|
|
const contents = `<p>{{ 'Audiobooks for personal development' | translate }}</p>`;
|
2016-12-10 04:14:13 +01:00
|
|
|
const collection = parser.extract(contents, templateFilename);
|
|
|
|
expect(collection.values).to.deep.equal({});
|
|
|
|
});
|
|
|
|
|
2020-05-28 00:28:13 +02:00
|
|
|
it('should extract contents from custom elements', () => {
|
2017-11-07 15:13:01 +01:00
|
|
|
const contents = `<custom-table><tbody><tr><td translate>Hello World</td></tr></tbody></custom-table>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
|
|
|
});
|
|
|
|
|
2020-05-28 00:28:13 +02:00
|
|
|
it('should extract from template without leading/trailing whitespace', () => {
|
2020-03-22 10:23:45 +01:00
|
|
|
const contents = `
|
2020-05-28 00:09:53 +02:00
|
|
|
<div *ngIf="!isLoading && studentsToGrid && studentsToGrid.length == 0" class="no-students" mt-rtl translate>There
|
|
|
|
are currently no students in this class. The good news is, adding students is really easy! Just use the options
|
2020-03-22 10:23:45 +01:00
|
|
|
at the top.
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2020-03-25 11:47:44 +01:00
|
|
|
expect(keys).to.deep.equal([
|
|
|
|
'There are currently no students in this class. The good news is, adding students is really easy! Just use the options at the top.'
|
|
|
|
]);
|
2020-03-22 10:23:45 +01:00
|
|
|
});
|
2020-03-22 11:13:16 +01:00
|
|
|
|
2020-05-28 00:28:13 +02:00
|
|
|
it('should extract keys from element without leading/trailing whitespace', () => {
|
2020-03-31 12:24:57 +02:00
|
|
|
const contents = `
|
|
|
|
<div translate>
|
2020-05-28 00:09:53 +02:00
|
|
|
this is an example
|
|
|
|
of a long label
|
2020-03-31 12:24:57 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<p translate>
|
2020-05-28 00:09:53 +02:00
|
|
|
this is an example
|
2020-05-28 00:28:13 +02:00
|
|
|
of another a long label
|
2020-03-31 12:24:57 +02:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2020-05-28 00:28:13 +02:00
|
|
|
expect(keys).to.deep.equal(['this is an example of a long label', 'this is an example of another a long label']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should collapse excessive whitespace', () => {
|
|
|
|
const contents = '<p translate>this is an example</p>';
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['this is an example']);
|
2020-03-31 12:24:57 +02:00
|
|
|
});
|
2020-05-28 19:35:26 +02:00
|
|
|
|
2016-12-08 13:53:13 +01:00
|
|
|
});
|