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:
parent
9d8dc793da
commit
5ace90f04d
|
@ -475,7 +475,7 @@ describe('diagnostics', () => {
|
||||||
`Module '"../node_modules/@angular/core/core"' has no exported member 'OpaqueToken'.`);
|
`Module '"../node_modules/@angular/core/core"' has no exported member 'OpaqueToken'.`);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('URL diagnostics', () => {
|
describe('templates', () => {
|
||||||
it('should report errors for invalid templateUrls', () => {
|
it('should report errors for invalid templateUrls', () => {
|
||||||
const fileName = mockHost.addCode(`
|
const fileName = mockHost.addCode(`
|
||||||
@Component({
|
@Component({
|
||||||
|
@ -508,6 +508,53 @@ describe('diagnostics', () => {
|
||||||
expect(urlDiagnostic).toBeUndefined();
|
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', () => {
|
it('should report errors for invalid styleUrls', () => {
|
||||||
const fileName = mockHost.addCode(`
|
const fileName = mockHost.addCode(`
|
||||||
@Component({
|
@Component({
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
export const toh = {
|
export const toh = {
|
||||||
'foo.ts': `export * from './app/app.component.ts';`,
|
'foo.ts': `export * from './app/app.component.ts';`,
|
||||||
app: {
|
app: {
|
||||||
'app.component.ts': `import { Component } from '@angular/core';
|
'app.component.ts': `import { Component, NgModule } from '@angular/core';
|
||||||
|
|
||||||
export class Hero {
|
export class Hero {
|
||||||
id: number;
|
id: number;
|
||||||
|
|
Loading…
Reference in New Issue