2016-12-08 15:53:13 +03:00
|
|
|
|
import { expect } from 'chai';
|
|
|
|
|
|
2016-12-08 16:28:59 +03:00
|
|
|
|
import { DirectiveParser } from '../../src/parsers/directive.parser';
|
2016-12-08 15:53:13 +03:00
|
|
|
|
|
2016-12-23 07:23:33 +03:00
|
|
|
|
class TestDirectiveParser extends DirectiveParser {
|
|
|
|
|
|
|
|
|
|
public normalizeTemplateAttributes(template: string): string {
|
|
|
|
|
return this._normalizeTemplateAttributes(template);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-08 15:53:13 +03:00
|
|
|
|
describe('DirectiveParser', () => {
|
|
|
|
|
|
|
|
|
|
const templateFilename: string = 'test.template.html';
|
|
|
|
|
const componentFilename: string = 'test.component.ts';
|
|
|
|
|
|
2016-12-23 07:23:33 +03:00
|
|
|
|
let parser: TestDirectiveParser;
|
2016-12-08 15:53:13 +03:00
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-12-23 07:23:33 +03:00
|
|
|
|
parser = new TestDirectiveParser();
|
2016-12-08 15:53:13 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should extract contents when no translate attribute value is provided', () => {
|
|
|
|
|
const contents = '<div translate>Hello World</div>';
|
2016-12-09 07:18:04 +03:00
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
2016-12-08 15:53:13 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should extract translate attribute if provided', () => {
|
|
|
|
|
const contents = '<div translate="KEY">Hello World<div>';
|
2016-12-09 07:18:04 +03:00
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
|
expect(keys).to.deep.equal(['KEY']);
|
2016-12-08 15:53:13 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should extract bound translate attribute as key if provided', () => {
|
2016-12-08 16:12:33 +03:00
|
|
|
|
const contents = `<div [translate]="'KEY'">Hello World<div>`;
|
2016-12-09 07:18:04 +03:00
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
|
expect(keys).to.deep.equal(['KEY']);
|
2016-12-08 15:53:13 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should extract direct text nodes when no translate attribute value is provided', () => {
|
|
|
|
|
const contents = `
|
|
|
|
|
<div translate>
|
|
|
|
|
<span>✓</span>
|
|
|
|
|
Hello <strong>World</strong>
|
|
|
|
|
Hi <em>there</em>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
2016-12-09 07:18:04 +03:00
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
|
expect(keys).to.deep.equal(['Hello', 'Hi']);
|
2016-12-08 15:53:13 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should extract direct text nodes of tags with a translate attribute', () => {
|
|
|
|
|
const contents = `
|
|
|
|
|
<div translate>
|
|
|
|
|
<span>✓</span>
|
|
|
|
|
Hello World
|
|
|
|
|
<div translate>Hi there</div>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
2016-12-09 07:18:04 +03:00
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
|
expect(keys).to.deep.equal(['Hello World', 'Hi there']);
|
2016-12-08 15:53:13 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should extract translate attribute if provided or direct text nodes if not', () => {
|
|
|
|
|
const contents = `
|
|
|
|
|
<div translate="KEY">
|
|
|
|
|
<span>✓</span>
|
|
|
|
|
Hello World
|
|
|
|
|
<p translate>Hi there</p>
|
|
|
|
|
<p [translate]="'OTHER_KEY'">Lorem Ipsum</p>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
2016-12-09 07:18:04 +03:00
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
|
expect(keys).to.deep.equal(['KEY', 'Hi there', 'OTHER_KEY']);
|
2016-12-08 15:53:13 +03: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 07:18:04 +03:00
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
2016-12-08 15:53:13 +03:00
|
|
|
|
});
|
|
|
|
|
|
2016-12-08 18:09:06 +03:00
|
|
|
|
it('should extract contents when no ng2-translate attribute value is provided', () => {
|
|
|
|
|
const contents = '<div ng2-translate>Hello World</div>';
|
2016-12-09 07:18:04 +03:00
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
2016-12-08 17:43:38 +03:00
|
|
|
|
});
|
|
|
|
|
|
2016-12-08 18:09:06 +03:00
|
|
|
|
it('should extract ng2-translate attribute if provided', () => {
|
|
|
|
|
const contents = '<div ng2-translate="KEY">Hello World<div>';
|
2016-12-09 07:18:04 +03:00
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
|
expect(keys).to.deep.equal(['KEY']);
|
2016-12-08 18:09:06 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should extract bound ng2-translate attribute as key if provided', () => {
|
|
|
|
|
const contents = `<div [ng2-translate]="'KEY'">Hello World<div>`;
|
2016-12-09 07:18:04 +03:00
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
|
expect(keys).to.deep.equal(['KEY']);
|
2016-12-08 18:09:06 +03:00
|
|
|
|
});
|
|
|
|
|
|
2016-12-10 06:14:13 +03:00
|
|
|
|
it('should not extract translate pipe in html tag', () => {
|
|
|
|
|
const contents = `<p>{{ 'Audiobooks for personal development' | translate }}</p>`;
|
|
|
|
|
const collection = parser.extract(contents, templateFilename);
|
|
|
|
|
expect(collection.values).to.deep.equal({});
|
|
|
|
|
});
|
|
|
|
|
|
2016-12-23 07:23:33 +03:00
|
|
|
|
it('should normalize bound attributes', () => {
|
|
|
|
|
const contents = `<p [translate]="'KEY'">Hello World</p>`;
|
|
|
|
|
const template = parser.normalizeTemplateAttributes(contents);
|
|
|
|
|
expect(template).to.equal('<p translate="KEY">Hello World</p>');
|
|
|
|
|
});
|
|
|
|
|
|
2017-11-07 17:13:01 +03:00
|
|
|
|
it('should extract contents from within custom tags', () => {
|
|
|
|
|
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']);
|
|
|
|
|
});
|
|
|
|
|
|
2016-12-08 15:53:13 +03:00
|
|
|
|
});
|