test(language-service): add missing tests for templateUrls (#32592)

Add missing tests for duplicate, missing template diagnostics

PR Close #32592
This commit is contained in:
ayazhafiz 2019-09-10 14:51:16 -05:00 committed by Andrew Kushnir
parent 9d8dc793da
commit 5ace90f04d
2 changed files with 49 additions and 2 deletions

View File

@ -475,7 +475,7 @@ describe('diagnostics', () => {
`Module '"../node_modules/@angular/core/core"' has no exported member 'OpaqueToken'.`);
});
describe('URL diagnostics', () => {
describe('templates', () => {
it('should report errors for invalid templateUrls', () => {
const fileName = mockHost.addCode(`
@Component({
@ -508,6 +508,53 @@ describe('diagnostics', () => {
expect(urlDiagnostic).toBeUndefined();
});
it('should report diagnostic for missing template or templateUrl', () => {
const fileName = mockHost.addCode(`
@Component({
selector: 'app-example',
})
export class AppExample {}
@NgModule({
declarations: [AppExample],
})
export class AppModule {}`);
const diags = ngLS.getDiagnostics(fileName);
const missingTemplateError = diags.find(
d => d.messageText === `Component 'AppExample' must have a template or templateUrl`);
expect(missingTemplateError).toBeDefined();
const {start, length} = missingTemplateError !;
const content = mockHost.getFileContent(fileName) !;
expect(start).toBe(content.lastIndexOf('Component'));
expect(length).toBe('Component'.length);
});
it('should report diagnostic for both template and templateUrl', () => {
const fileName = mockHost.addCode(`
@Component({
selector: 'app-example',
template: '<div></div>',
templateUrl: './example.html',
})
export class AppExample {}
@NgModule({
declarations: [AppExample],
})
export class AppModule {}`);
const diags = ngLS.getDiagnostics(fileName);
const dupTemplateError = diags.find(
d => d.messageText ===
`Component 'AppExample' must not have both template and templateUrl`);
expect(dupTemplateError).toBeDefined();
const {start, length} = dupTemplateError !;
const content = mockHost.getFileContent(fileName) !;
expect(start).toBe(content.lastIndexOf('Component'));
expect(length).toBe('Component'.length);
});
it('should report errors for invalid styleUrls', () => {
const fileName = mockHost.addCode(`
@Component({

View File

@ -9,7 +9,7 @@
export const toh = {
'foo.ts': `export * from './app/app.component.ts';`,
app: {
'app.component.ts': `import { Component } from '@angular/core';
'app.component.ts': `import { Component, NgModule } from '@angular/core';
export class Hero {
id: number;