Zach Arend de8f0fe5ee test(language-service): convert ivy diagnostics tests from legacy (#39957)
This commit updates the tests for the diagnostics in the ivy language
service to use the new in-memory test environment.

PR Close #39957
2020-12-04 10:16:43 -08:00

109 lines
3.3 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {absoluteFrom} from '@angular/compiler-cli/src/ngtsc/file_system';
import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {LanguageServiceTestEnvironment} from '@angular/language-service/ivy/test/env';
import * as ts from 'typescript';
import {createModuleWithDeclarations} from './test_utils';
describe('getSemanticDiagnostics', () => {
let env: LanguageServiceTestEnvironment;
beforeEach(() => {
initMockFileSystem('Native');
});
it('should not produce error for a minimal component defintion', () => {
const appFile = {
name: absoluteFrom('/app.ts'),
contents: `
import {Component, NgModule} from '@angular/core';
@Component({
template: ''
})
export class AppComponent {}
`
};
env = createModuleWithDeclarations([appFile]);
const diags = env.ngLS.getSemanticDiagnostics(absoluteFrom('/app.ts'));
expect(diags.length).toEqual(0);
});
it('should report member does not exist', () => {
const appFile = {
name: absoluteFrom('/app.ts'),
contents: `
import {Component, NgModule} from '@angular/core';
@Component({
template: '{{nope}}'
})
export class AppComponent {}
`
};
env = createModuleWithDeclarations([appFile]);
const diags = env.ngLS.getSemanticDiagnostics(absoluteFrom('/app.ts'));
expect(diags.length).toBe(1);
const {category, file, start, length, messageText} = diags[0];
expect(category).toBe(ts.DiagnosticCategory.Error);
expect(file?.fileName).toBe('/app.ts');
expect(messageText).toBe(`Property 'nope' does not exist on type 'AppComponent'.`);
});
it('should process external template', () => {
const appFile = {
name: absoluteFrom('/app.ts'),
contents: `
import {Component, NgModule} from '@angular/core';
@Component({
templateUrl: './app.html'
})
export class AppComponent {}
`
};
const templateFile = {
name: absoluteFrom('/app.html'),
contents: `
Hello world!
`
};
env = createModuleWithDeclarations([appFile], [templateFile]);
const diags = env.ngLS.getSemanticDiagnostics(absoluteFrom('/app.html'));
expect(diags).toEqual([]);
});
it('should report member does not exist in external template', () => {
const appFile = {
name: absoluteFrom('/app.ts'),
contents: `
import {Component, NgModule} from '@angular/core';
@Component({
templateUrl: './app.html'
})
export class AppComponent {}
`
};
const templateFile = {name: absoluteFrom('/app.html'), contents: `{{nope}}`};
env = createModuleWithDeclarations([appFile], [templateFile]);
const diags = env.ngLS.getSemanticDiagnostics(absoluteFrom('/app.html'));
expect(diags.length).toBe(1);
const {category, file, start, length, messageText} = diags[0];
expect(category).toBe(ts.DiagnosticCategory.Error);
expect(file?.fileName).toBe('/app.html');
expect(messageText).toBe(`Property 'nope' does not exist on type 'AppComponent'.`);
});
});