refactor(language-service): cleanup tests for Hover (#32378)

Move generic test methods to `MockTypescriptHost` so they could be
shared across all tests.

This is in preparation for adding more tests to Hover when new features
get added.

PR Close #32378
This commit is contained in:
Keen Yee Liau
2019-08-28 12:55:12 -07:00
committed by Miško Hevery
parent 852afb312a
commit 18ce58c2bc
3 changed files with 213 additions and 197 deletions

View File

@ -6,114 +6,151 @@
* found in the LICENSE file at https://angular.io/license
*/
import 'reflect-metadata';
import * as ts from 'typescript';
import {createLanguageService} from '../src/language_service';
import {Hover} from '../src/types';
import {LanguageService} from '../src/types';
import {TypeScriptServiceHost} from '../src/typescript_host';
import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';
describe('hover', () => {
let documentRegistry = ts.createDocumentRegistry();
let mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], toh);
let service = ts.createLanguageService(mockHost, documentRegistry);
let ngHost = new TypeScriptServiceHost(mockHost, service);
let ngService = createLanguageService(ngHost);
let mockHost: MockTypescriptHost;
let tsLS: ts.LanguageService;
let ngLSHost: TypeScriptServiceHost;
let ngLS: LanguageService;
beforeEach(() => {
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], toh);
tsLS = ts.createLanguageService(mockHost);
ngLSHost = new TypeScriptServiceHost(mockHost, tsLS);
ngLS = createLanguageService(ngLSHost);
});
it('should be able to find field in an interpolation', () => {
hover(
` @Component({template: '{{«name»}}'}) export class MyComponent { name: string; }`,
'(property) MyComponent.name');
const fileName = mockHost.addCode(`
@Component({
template: '{{«name»}}'
})
export class MyComponent {
name: string;
}`);
const marker = mockHost.getReferenceMarkerFor(fileName, 'name');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
expect(quickInfo).toBeTruthy();
const {textSpan, displayParts} = quickInfo !;
expect(textSpan).toEqual(marker);
expect(toText(displayParts)).toBe('(property) MyComponent.name');
});
it('should be able to find a field in a attribute reference', () => {
hover(
` @Component({template: '<input [(ngModel)]="«name»">'}) export class MyComponent { name: string; }`,
'(property) MyComponent.name');
const fileName = mockHost.addCode(`
@Component({
template: '<input [(ngModel)]="«name»">'
})
export class MyComponent {
name: string;
}`);
const marker = mockHost.getReferenceMarkerFor(fileName, 'name');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
expect(quickInfo).toBeTruthy();
const {textSpan, displayParts} = quickInfo !;
expect(textSpan).toEqual(marker);
expect(toText(displayParts)).toBe('(property) MyComponent.name');
});
it('should be able to find a method from a call', () => {
hover(
` @Component({template: '<div (click)="«ᐱmyClickᐱ()»;"></div>'}) export class MyComponent { myClick() { }}`,
'(method) MyComponent.myClick');
const fileName = mockHost.addCode(`
@Component({
template: '<div (click)="«ᐱmyClickᐱ()»;"></div>'
})
export class MyComponent {
myClick() { }
}`);
const marker = mockHost.getDefinitionMarkerFor(fileName, 'myClick');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
expect(quickInfo).toBeTruthy();
const {textSpan, displayParts} = quickInfo !;
expect(textSpan).toEqual(marker);
expect(textSpan.length).toBe('myClick()'.length);
expect(toText(displayParts)).toBe('(method) MyComponent.myClick');
});
it('should be able to find a field reference in an *ngIf', () => {
hover(
` @Component({template: '<div *ngIf="«include»"></div>'}) export class MyComponent { include = true;}`,
'(property) MyComponent.include');
const fileName = mockHost.addCode(`
@Component({
template: '<div *ngIf="«include»"></div>'
})
export class MyComponent {
include = true;
}`);
const marker = mockHost.getReferenceMarkerFor(fileName, 'include');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
expect(quickInfo).toBeTruthy();
const {textSpan, displayParts} = quickInfo !;
expect(textSpan).toEqual(marker);
expect(toText(displayParts)).toBe('(property) MyComponent.include');
});
it('should be able to find a reference to a component', () => {
hover(
` @Component({template: '«<ᐱtestᐱ-comp></test-comp>»'}) export class MyComponent { }`,
'(component) TestComponent');
const fileName = mockHost.addCode(`
@Component({
template: '«<ᐱtestᐱ-comp></test-comp>»'
})
export class MyComponent { }`);
const marker = mockHost.getDefinitionMarkerFor(fileName, 'test');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
expect(quickInfo).toBeTruthy();
const {textSpan, displayParts} = quickInfo !;
expect(textSpan).toEqual(marker);
expect(toText(displayParts)).toBe('(component) TestComponent');
});
it('should be able to find an event provider', () => {
hover(
` @Component({template: '<test-comp «(ᐱtestᐱ)="myHandler()"»></div>'}) export class MyComponent { myHandler() {} }`,
'(event) TestComponent.testEvent');
const fileName = mockHost.addCode(`
@Component({
template: '<test-comp «(ᐱtestᐱ)="myHandler()"»></div>'
})
export class MyComponent {
myHandler() {}
}`);
const marker = mockHost.getDefinitionMarkerFor(fileName, 'test');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
expect(quickInfo).toBeTruthy();
const {textSpan, displayParts} = quickInfo !;
expect(textSpan).toEqual(marker);
expect(toText(displayParts)).toBe('(event) TestComponent.testEvent');
});
it('should be able to find an input provider', () => {
hover(
` @Component({template: '<test-comp «[ᐱtcNameᐱ]="name"»></div>'}) export class MyComponent { name = 'my name'; }`,
'(property) TestComponent.name');
const fileName = mockHost.addCode(`
@Component({
template: '<test-comp «[ᐱtcNameᐱ]="name"»></div>'
})
export class MyComponent {
name = 'my name';
}`);
const marker = mockHost.getDefinitionMarkerFor(fileName, 'tcName');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
expect(quickInfo).toBeTruthy();
const {textSpan, displayParts} = quickInfo !;
expect(textSpan).toEqual(marker);
expect(toText(displayParts)).toBe('(property) TestComponent.name');
});
it('should be able to ignore a reference declaration', () => {
addCode(
` @Component({template: '<div #«chart»></div>'}) export class MyComponent { }`,
fileName => {
const markers = mockHost.getReferenceMarkers(fileName) !;
const hover = ngService.getHoverAt(fileName, markers.references.chart[0].start);
expect(hover).toBeUndefined();
});
const fileName = mockHost.addCode(`
@Component({
template: '<div #«chart»></div>'
})
export class MyComponent { }`);
const marker = mockHost.getReferenceMarkerFor(fileName, 'chart');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
expect(quickInfo).toBeUndefined();
});
function hover(code: string, hoverText: string) {
addCode(code, fileName => {
let tests = 0;
const markers = mockHost.getReferenceMarkers(fileName) !;
const keys = Object.keys(markers.references).concat(Object.keys(markers.definitions));
for (const referenceName of keys) {
const references = (markers.references[referenceName] ||
[]).concat(markers.definitions[referenceName] || []);
for (const reference of references) {
tests++;
const quickInfo = ngService.getHoverAt(fileName, reference.start);
if (!quickInfo) throw new Error(`Expected a hover at location ${reference.start}`);
expect(quickInfo.textSpan).toEqual({
start: reference.start,
length: reference.end - reference.start,
});
expect(toText(quickInfo)).toEqual(hoverText);
}
}
expect(tests).toBeGreaterThan(0); // If this fails the test is wrong.
});
}
function addCode(code: string, cb: (fileName: string, content?: string) => void) {
const fileName = '/app/app.component.ts';
const originalContent = mockHost.getFileContent(fileName);
const newContent = originalContent + code;
mockHost.override(fileName, originalContent + code);
try {
cb(fileName, newContent);
} finally {
mockHost.override(fileName, undefined !);
}
}
function toText(quickInfo: ts.QuickInfo): string {
const displayParts = quickInfo.displayParts || [];
return displayParts.map(p => p.text).join('');
}
});
function toText(displayParts?: ts.SymbolDisplayPart[]): string {
return (displayParts || []).map(p => p.text).join('');
}