Use same structure for tests as src
This commit is contained in:
62
tests/parsers/abstract-template.parser.spec.ts
Normal file
62
tests/parsers/abstract-template.parser.spec.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { AbstractTemplateParser } from '../../src/parsers/abstract-template.parser';
|
||||
|
||||
class TestTemplateParser extends AbstractTemplateParser {
|
||||
|
||||
public isAngularComponent(filePath: string): boolean {
|
||||
return this._isAngularComponent(filePath);
|
||||
}
|
||||
|
||||
public normalizeTemplateAttributes(template: string): string {
|
||||
return this._normalizeTemplateAttributes(template);
|
||||
}
|
||||
|
||||
public extractInlineTemplate(contents: string): string {
|
||||
return this._extractInlineTemplate(contents);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
describe('AbstractTemplateParser', () => {
|
||||
|
||||
let parser: TestTemplateParser;
|
||||
|
||||
beforeEach(() => {
|
||||
parser = new TestTemplateParser();
|
||||
});
|
||||
|
||||
it('should recognize js extension as angular component', () => {
|
||||
const result = parser.isAngularComponent('test.js');
|
||||
expect(result).to.equal(true);
|
||||
});
|
||||
|
||||
it('should recognize ts extension as angular component', () => {
|
||||
const result = parser.isAngularComponent('test.ts');
|
||||
expect(result).to.equal(true);
|
||||
});
|
||||
|
||||
it('should not recognize html extension as angular component', () => {
|
||||
const result = parser.isAngularComponent('test.html');
|
||||
expect(result).to.equal(false);
|
||||
});
|
||||
|
||||
it('should extract inline template', () => {
|
||||
const contents = `
|
||||
@Component({
|
||||
selector: 'test',
|
||||
template: '<p translate>Hello World</p>'
|
||||
})
|
||||
export class TestComponent { }
|
||||
`;
|
||||
const template = parser.extractInlineTemplate(contents);
|
||||
expect(template).to.equal('<p translate>Hello World</p>');
|
||||
});
|
||||
|
||||
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>');
|
||||
});
|
||||
|
||||
});
|
83
tests/parsers/directive.parser.spec.ts
Normal file
83
tests/parsers/directive.parser.spec.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { DirectiveParser } from '../../src/parsers/directive.parser';
|
||||
|
||||
describe('DirectiveParser', () => {
|
||||
|
||||
const templateFilename: string = 'test.template.html';
|
||||
const componentFilename: string = 'test.component.ts';
|
||||
|
||||
let parser: DirectiveParser;
|
||||
|
||||
beforeEach(() => {
|
||||
parser = new DirectiveParser();
|
||||
});
|
||||
|
||||
it('should extract contents when no translate attribute value is provided', () => {
|
||||
const contents = '<div translate>Hello World</div>';
|
||||
const messages = parser.process(templateFilename, contents);
|
||||
expect(messages).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract translate attribute if provided', () => {
|
||||
const contents = '<div translate="KEY">Hello World<div>';
|
||||
const messages = parser.process(templateFilename, contents);
|
||||
expect(messages).to.deep.equal(['KEY']);
|
||||
});
|
||||
|
||||
it('should extract bound translate attribute as key if provided', () => {
|
||||
const contents = `<div [translate]="'KEY'">Hello World<div>`;
|
||||
const messages = parser.process(templateFilename, contents);
|
||||
expect(messages).to.deep.equal(['KEY']);
|
||||
});
|
||||
|
||||
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>
|
||||
`;
|
||||
const messages = parser.process(templateFilename, contents);
|
||||
expect(messages).to.deep.equal(['Hello', 'Hi']);
|
||||
});
|
||||
|
||||
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>
|
||||
`;
|
||||
const messages = parser.process(templateFilename, contents);
|
||||
expect(messages).to.deep.equal(['Hello World', 'Hi there']);
|
||||
});
|
||||
|
||||
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>
|
||||
`;
|
||||
const messages = parser.process(templateFilename, contents);
|
||||
expect(messages).to.deep.equal(['KEY', 'Hi there', 'OTHER_KEY']);
|
||||
});
|
||||
|
||||
it('should extract and parse inline template', () => {
|
||||
const contents = `
|
||||
@Component({
|
||||
selector: 'test',
|
||||
template: '<p translate>Hello World</p>'
|
||||
})
|
||||
export class TestComponent { }
|
||||
`;
|
||||
const messages = parser.process(componentFilename, contents);
|
||||
expect(messages).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
});
|
33
tests/parsers/pipe.parser.spec.ts
Normal file
33
tests/parsers/pipe.parser.spec.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { PipeParser } from '../../src/parsers/pipe.parser';
|
||||
|
||||
describe('PipeParser', () => {
|
||||
|
||||
const templateFilename: string = 'test.template.html';
|
||||
|
||||
let parser: PipeParser;
|
||||
|
||||
beforeEach(() => {
|
||||
parser = new PipeParser();
|
||||
});
|
||||
|
||||
it('should extract interpolated strings using translate pipe', () => {
|
||||
const contents = `Hello {{ 'World' | translate }}`;
|
||||
const messages = parser.process(templateFilename, contents);
|
||||
expect(messages).to.deep.equal(['World']);
|
||||
});
|
||||
|
||||
it('should extract interpolated strings using translate pipe in attributes', () => {
|
||||
const contents = `<span attr="{{ 'Hello World' | translate }}"></span>`;
|
||||
const messages = parser.process(templateFilename, contents);
|
||||
expect(messages).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract bound strings using translate pipe in attributes', () => {
|
||||
const contents = `<span [attr]="'Hello World' | translate"></span>`;
|
||||
const messages = parser.process(templateFilename, contents);
|
||||
expect(messages).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
});
|
80
tests/parsers/service.parser.spec.ts
Normal file
80
tests/parsers/service.parser.spec.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { ServiceParser } from '../../src/parsers/service.parser';
|
||||
|
||||
class TestServiceParser extends ServiceParser {
|
||||
|
||||
public extractTranslateServiceVar(contents: string): string {
|
||||
return this._extractTranslateServiceVar(contents);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
describe('ServiceParser', () => {
|
||||
|
||||
const componentFilename: string = 'test.component.ts';
|
||||
|
||||
let parser: TestServiceParser;
|
||||
|
||||
beforeEach(() => {
|
||||
parser = new TestServiceParser();
|
||||
});
|
||||
|
||||
it('should extract variable used for TranslateService', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(
|
||||
_serviceA: ServiceA,
|
||||
public _serviceB: ServiceB,
|
||||
protected _translateService: TranslateService
|
||||
) { }
|
||||
`;
|
||||
const messages = parser.extractTranslateServiceVar(contents);
|
||||
expect(messages).to.equal('_translateService');
|
||||
});
|
||||
|
||||
it('should extract string passed to translateService.get()', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
this._translateService.get('Hello World');
|
||||
}
|
||||
`;
|
||||
const messages = parser.process(componentFilename, contents);
|
||||
expect(messages).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract string passed to translateService.instant()', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
this._translateService.instant('Hello World');
|
||||
}
|
||||
`;
|
||||
const messages = parser.process(componentFilename, contents);
|
||||
expect(messages).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should not extract string passed to get() or instant() methods of other services', () => {
|
||||
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');
|
||||
}
|
||||
`;
|
||||
const messages = parser.process(componentFilename, contents);
|
||||
expect(messages).to.deep.equal([]);
|
||||
});
|
||||
|
||||
});
|
Reference in New Issue
Block a user