2020-12-04 11:33:27 -05:00
|
|
|
/**
|
|
|
|
* @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 {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
|
|
|
|
|
2021-02-08 13:11:39 -05:00
|
|
|
import {createModuleAndProjectWithDeclarations, LanguageServiceTestEnv} from '../testing';
|
2020-12-04 11:33:27 -05:00
|
|
|
|
|
|
|
describe('get typecheck block', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
initMockFileSystem('Native');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find the typecheck block for an inline template', () => {
|
2021-02-08 13:11:39 -05:00
|
|
|
const files = {
|
|
|
|
'app.ts': `
|
2020-12-04 11:33:27 -05:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
2021-02-08 13:11:39 -05:00
|
|
|
template: '<div>{{ myProp }}</div>',
|
2020-12-04 11:33:27 -05:00
|
|
|
})
|
|
|
|
export class AppCmp {
|
|
|
|
myProp!: string;
|
2021-02-08 13:11:39 -05:00
|
|
|
}`
|
|
|
|
};
|
|
|
|
const env = LanguageServiceTestEnv.setup();
|
|
|
|
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
|
|
|
|
project.expectNoSourceDiagnostics();
|
2020-12-04 11:33:27 -05:00
|
|
|
|
2021-02-08 13:11:39 -05:00
|
|
|
const appFile = project.openFile('app.ts');
|
|
|
|
appFile.moveCursorToText('{{ my¦Prop }}');
|
|
|
|
const result = appFile.getTcb();
|
2020-12-04 11:33:27 -05:00
|
|
|
if (result === undefined) {
|
|
|
|
fail('Expected a valid TCB response');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const {content, selections} = result;
|
|
|
|
expect(selections.length).toBe(1);
|
|
|
|
const {start, length} = selections[0];
|
|
|
|
expect(content.substring(start, start + length)).toContain('myProp');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find the typecheck block for an external template', () => {
|
2021-02-08 13:11:39 -05:00
|
|
|
const files = {
|
|
|
|
'app.ts': `
|
2020-12-04 11:33:27 -05:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: './app.html',
|
|
|
|
})
|
|
|
|
export class AppCmp {
|
|
|
|
myProp!: string;
|
|
|
|
}`,
|
2021-02-08 13:11:39 -05:00
|
|
|
'app.html': '<div>{{ myProp }}</div>'
|
|
|
|
};
|
|
|
|
const env = LanguageServiceTestEnv.setup();
|
|
|
|
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
|
|
|
|
project.expectNoSourceDiagnostics();
|
2020-12-04 11:33:27 -05:00
|
|
|
|
2021-02-08 13:11:39 -05:00
|
|
|
const htmlFile = project.openFile('app.html');
|
|
|
|
htmlFile.moveCursorToText('{{ my¦Prop }}');
|
|
|
|
const result = htmlFile.getTcb();
|
2020-12-04 11:33:27 -05:00
|
|
|
if (result === undefined) {
|
|
|
|
fail('Expected a valid TCB response');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const {content, selections} = result;
|
|
|
|
expect(selections.length).toBe(1);
|
|
|
|
const {start, length} = selections[0];
|
|
|
|
expect(content.substring(start, start + length)).toContain('myProp');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not find typecheck blocks outside a template', () => {
|
2021-02-08 13:11:39 -05:00
|
|
|
const files = {
|
|
|
|
'app.ts': `
|
2020-12-04 11:33:27 -05:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: '<div>{{ myProp }}</div>',
|
|
|
|
})
|
|
|
|
export class AppCmp {
|
2021-02-08 13:11:39 -05:00
|
|
|
myProp!: string;
|
|
|
|
}`
|
|
|
|
};
|
|
|
|
const env = LanguageServiceTestEnv.setup();
|
|
|
|
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
|
|
|
|
project.expectNoSourceDiagnostics();
|
2020-12-04 11:33:27 -05:00
|
|
|
|
2021-02-08 13:11:39 -05:00
|
|
|
const appFile = project.openFile('app.ts');
|
|
|
|
appFile.moveCursorToText('my¦Prop!: string;');
|
|
|
|
const result = appFile.getTcb();
|
2020-12-04 11:33:27 -05:00
|
|
|
expect(result).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|