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
|
|
|
|
|
|
|
describe('DirectiveParser', () => {
|
|
|
|
const templateFilename: string = 'test.template.html';
|
|
|
|
const componentFilename: string = 'test.component.ts';
|
|
|
|
|
2019-06-18 16:54:58 +03:00
|
|
|
let parser: DirectiveParser;
|
2016-12-08 15:53:13 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-06-18 16:54:58 +03:00
|
|
|
parser = new DirectiveParser();
|
2016-12-08 15:53:13 +03:00
|
|
|
});
|
|
|
|
|
2020-05-28 01:09:53 +03: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']);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Source: // https://github.com/ngx-translate/core/blob/7241c863b2eead26e082cd0b7ee15bac3f9336fc/projects/ngx-translate/core/tests/translate.directive.spec.ts#L93
|
|
|
|
it('should extract keys the same way TranslateDirective is using them', () => {
|
|
|
|
const contents = `<div #withOtherElements translate>TEST1 <span>Hey</span> TEST2</div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['TEST1', 'TEST2']);
|
|
|
|
});
|
|
|
|
|
2019-06-18 16:54:58 +03:00
|
|
|
it('should not choke when no html is present in template', () => {
|
|
|
|
const contents = 'Hello World';
|
2016-12-09 07:18:04 +03:00
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2019-06-18 16:54:58 +03:00
|
|
|
expect(keys).to.deep.equal([]);
|
2016-12-08 15:53:13 +03:00
|
|
|
});
|
|
|
|
|
2019-06-18 16:54:58 +03:00
|
|
|
it('should use contents as key when there is no translate attribute value provided', () => {
|
|
|
|
const contents = '<div translate>Hello World</div>';
|
2016-12-09 07:18:04 +03:00
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2019-06-18 16:54:58 +03:00
|
|
|
expect(keys).to.deep.equal(['Hello World']);
|
2016-12-08 15:53:13 +03:00
|
|
|
});
|
|
|
|
|
2019-06-18 16:54:58 +03:00
|
|
|
it('should use translate attribute value as key when provided', () => {
|
|
|
|
const contents = '<div translate="MY_KEY">Hello World<div>';
|
2016-12-09 07:18:04 +03:00
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2019-06-18 16:54:58 +03:00
|
|
|
expect(keys).to.deep.equal(['MY_KEY']);
|
2016-12-08 15:53:13 +03:00
|
|
|
});
|
|
|
|
|
2019-06-18 16:54:58 +03:00
|
|
|
it('should not process children when translate attribute is present', () => {
|
|
|
|
const contents = `<div translate>Hello <strong translate>World</strong></div>`;
|
2016-12-09 07:18:04 +03:00
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2020-05-28 01:09:53 +03:00
|
|
|
expect(keys).to.deep.equal(['Hello', 'World']);
|
2016-12-08 15:53:13 +03:00
|
|
|
});
|
|
|
|
|
2020-04-01 13:19:30 +03:00
|
|
|
it('should not exclude html tags in children', () => {
|
|
|
|
const contents = `<div translate>Hello <strong>World</strong></div>`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2020-05-28 01:09:53 +03:00
|
|
|
expect(keys).to.deep.equal(['Hello']);
|
2020-04-01 13:19:30 +03:00
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-06-18 16:54:58 +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 17:43:38 +03:00
|
|
|
});
|
|
|
|
|
2019-06-18 16:54:58 +03:00
|
|
|
it('should extract translate attribute value 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 18:09:06 +03:00
|
|
|
});
|
|
|
|
|
2016-12-10 06:14:13 +03:00
|
|
|
it('should not extract translate pipe in html tag', () => {
|
2019-06-13 13:17:04 +03:00
|
|
|
const contents = `<p>{{ 'Audiobooks for personal development' | translate }}</p>`;
|
2016-12-10 06:14:13 +03:00
|
|
|
const collection = parser.extract(contents, templateFilename);
|
|
|
|
expect(collection.values).to.deep.equal({});
|
|
|
|
});
|
|
|
|
|
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']);
|
|
|
|
});
|
|
|
|
|
2019-06-13 13:32:18 +03:00
|
|
|
it('should not cause error when no html is present in template', () => {
|
|
|
|
const contents = `
|
|
|
|
import { Component } from '@angular/core';
|
|
|
|
@Component({
|
|
|
|
template: '{{ variable }}'
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
variable: string
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, componentFilename).keys();
|
|
|
|
expect(keys).to.deep.equal([]);
|
|
|
|
});
|
2020-03-22 12:23:45 +03:00
|
|
|
|
|
|
|
it('should extract contents without line breaks', () => {
|
|
|
|
const contents = `
|
|
|
|
<p translate>
|
2020-05-28 01:09:53 +03:00
|
|
|
Please leave a message for your client letting them know why you
|
2020-03-22 12:23:45 +03:00
|
|
|
rejected the field and what they need to do to fix it.
|
|
|
|
</p>
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2020-03-25 13:47:44 +03:00
|
|
|
expect(keys).to.deep.equal([
|
|
|
|
'Please leave a message for your client letting them know why you rejected the field and what they need to do to fix it.'
|
|
|
|
]);
|
2020-03-22 12:23:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract contents without indent spaces', () => {
|
|
|
|
const contents = `
|
2020-05-28 01:09:53 +03: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 12:23:45 +03:00
|
|
|
at the top.
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
2020-03-25 13:47:44 +03: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 12:23:45 +03:00
|
|
|
});
|
2020-03-22 13:13:16 +03:00
|
|
|
|
2020-03-31 13:24:57 +03:00
|
|
|
it('should extract contents without indent spaces and trim leading/trailing whitespace', () => {
|
|
|
|
const contents = `
|
|
|
|
<div translate>
|
2020-05-28 01:09:53 +03:00
|
|
|
this is an example
|
|
|
|
of a long label
|
2020-03-31 13:24:57 +03:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<p translate>
|
2020-05-28 01:09:53 +03:00
|
|
|
this is an example
|
|
|
|
of a long label
|
2020-03-31 13:24:57 +03:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
const keys = parser.extract(contents, templateFilename).keys();
|
|
|
|
expect(keys).to.deep.equal(['this is an example of a long label']);
|
|
|
|
});
|
2016-12-08 15:53:13 +03:00
|
|
|
});
|