2016-11-22 12:10:23 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. 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 * as ts from 'typescript';
|
|
|
|
|
|
|
|
import {createLanguageService} from '../src/language_service';
|
2019-08-28 15:55:12 -04:00
|
|
|
import {LanguageService} from '../src/types';
|
2016-11-22 12:10:23 -05:00
|
|
|
import {TypeScriptServiceHost} from '../src/typescript_host';
|
|
|
|
|
2017-01-03 20:21:45 -05:00
|
|
|
import {MockTypescriptHost} from './test_utils';
|
2016-11-22 12:10:23 -05:00
|
|
|
|
|
|
|
describe('hover', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
let mockHost: MockTypescriptHost;
|
|
|
|
let tsLS: ts.LanguageService;
|
|
|
|
let ngLSHost: TypeScriptServiceHost;
|
|
|
|
let ngLS: LanguageService;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-09-17 17:33:41 -04:00
|
|
|
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
|
2019-08-28 15:55:12 -04:00
|
|
|
tsLS = ts.createLanguageService(mockHost);
|
|
|
|
ngLSHost = new TypeScriptServiceHost(mockHost, tsLS);
|
|
|
|
ngLS = createLanguageService(ngLSHost);
|
|
|
|
});
|
2016-11-22 12:10:23 -05:00
|
|
|
|
|
|
|
it('should be able to find field in an interpolation', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
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');
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find a field in a attribute reference', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
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');
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find a method from a call', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
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');
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find a field reference in an *ngIf', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
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');
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find a reference to a component', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
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');
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find an event provider', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
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');
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find an input provider', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
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');
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
2017-07-07 11:46:18 -04:00
|
|
|
it('should be able to ignore a reference declaration', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
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();
|
2017-07-07 11:46:18 -04:00
|
|
|
});
|
2019-05-08 13:30:54 -04:00
|
|
|
});
|
2019-08-28 15:55:12 -04:00
|
|
|
|
|
|
|
function toText(displayParts?: ts.SymbolDisplayPart[]): string {
|
|
|
|
return (displayParts || []).map(p => p.text).join('');
|
|
|
|
}
|