From d1b77747536b4cab945c7c19c0dd183f48f6f6cb Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Mon, 8 Feb 2021 11:14:16 -0800 Subject: [PATCH] refactor(language-service): migrate quick_info_spec to new testing package (#40966) refactor(language-service): migrate quick_info_spec to new testing package PR Close #40966 --- .../ivy/test/quick_info_spec.ts | 61 +++++++++---------- .../ivy/testing/src/buffer.ts | 4 ++ 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/packages/language-service/ivy/test/quick_info_spec.ts b/packages/language-service/ivy/test/quick_info_spec.ts index d7713bc047..793cff68c2 100644 --- a/packages/language-service/ivy/test/quick_info_spec.ts +++ b/packages/language-service/ivy/test/quick_info_spec.ts @@ -6,18 +6,14 @@ * found in the LICENSE file at https://angular.io/license */ -import {absoluteFrom, AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system'; -import {initMockFileSystem, TestFile} from '@angular/compiler-cli/src/ngtsc/file_system/testing'; +import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing'; import * as ts from 'typescript/lib/tsserverlibrary'; +import {extractCursorInfo, LanguageServiceTestEnv, Project} from '../testing'; -import {LanguageServiceTestEnvironment} from './env'; - -function quickInfoSkeleton(): TestFile[] { - return [ - { - name: absoluteFrom('/app.ts'), - contents: ` +function quickInfoSkeleton(): {[fileName: string]: string} { + return { + 'app.ts': ` import {Component, Directive, EventEmitter, Input, NgModule, Output, Pipe, PipeTransform} from '@angular/core'; import {CommonModule} from '@angular/common'; @@ -92,22 +88,19 @@ function quickInfoSkeleton(): TestFile[] { }) export class AppModule {} `, - isRoot: true, - }, - { - name: absoluteFrom('/app.html'), - contents: `Will be overridden`, - } - ]; + 'app.html': `Will be overridden`, + }; } describe('quick info', () => { - let env: LanguageServiceTestEnvironment; + let env: LanguageServiceTestEnv; + let project: Project; describe('strict templates (happy path)', () => { beforeEach(() => { initMockFileSystem('Native'); - env = LanguageServiceTestEnvironment.setup(quickInfoSkeleton()); + env = LanguageServiceTestEnv.setup(); + project = env.addProject('test', quickInfoSkeleton()); }); describe('elements', () => { @@ -476,9 +469,10 @@ describe('quick info', () => { }); it('should provide documentation', () => { - const {cursor} = - env.updateFileWithCursor(absoluteFrom('/app.html'), `
{{¦title}}
`); - const quickInfo = env.ngLS.getQuickInfoAtPosition(absoluteFrom('/app.html'), cursor); + const template = project.openFile('app.html'); + template.contents = `
{{title}}
`; + template.moveCursorToText('{{¦title}}'); + const quickInfo = template.getQuickInfoAtPosition(); const documentation = toText(quickInfo!.documentation); expect(documentation).toBe('This is the title of the `AppCmp` Component.'); }); @@ -486,10 +480,13 @@ describe('quick info', () => { }); describe('non-strict compiler options', () => { - it('should find input binding on text attribute when strictAttributeTypes is false', () => { + beforeEach(() => { initMockFileSystem('Native'); - env = - LanguageServiceTestEnvironment.setup(quickInfoSkeleton(), {strictAttributeTypes: false}); + env = LanguageServiceTestEnv.setup(); + }); + + it('should find input binding on text attribute when strictAttributeTypes is false', () => { + project = env.addProject('test', quickInfoSkeleton(), {strictAttributeTypes: false}); expectQuickInfo({ templateOverride: ``, expectedSpanText: 'tcName', @@ -498,9 +495,7 @@ describe('quick info', () => { }); it('can still get quick info when strictOutputEventTypes is false', () => { - initMockFileSystem('Native'); - env = LanguageServiceTestEnvironment.setup( - quickInfoSkeleton(), {strictOutputEventTypes: false}); + project = env.addProject('test', quickInfoSkeleton(), {strictOutputEventTypes: false}); expectQuickInfo({ templateOverride: ``, expectedSpanText: 'test', @@ -509,9 +504,8 @@ describe('quick info', () => { }); it('should work for pipes even if checkTypeOfPipes is false', () => { - initMockFileSystem('Native'); // checkTypeOfPipes is set to false when strict templates is false - env = LanguageServiceTestEnvironment.setup(quickInfoSkeleton(), {strictTemplates: false}); + project = env.addProject('test', quickInfoSkeleton(), {strictTemplates: false}); const templateOverride = `

The hero's birthday is {{birthday | da¦te: "MM/dd/yy"}}

`; expectQuickInfo( {templateOverride, expectedSpanText: 'date', expectedDisplayString: '(pipe) DatePipe'}); @@ -522,10 +516,13 @@ describe('quick info', () => { {templateOverride, expectedSpanText, expectedDisplayString}: {templateOverride: string, expectedSpanText: string, expectedDisplayString: string}): ts.QuickInfo { - const {cursor, text} = env.updateFileWithCursor(absoluteFrom('/app.html'), templateOverride); + const {text} = extractCursorInfo(templateOverride); + const template = project.openFile('app.html'); + template.contents = text; env.expectNoSourceDiagnostics(); - env.expectNoTemplateDiagnostics(absoluteFrom('/app.ts'), 'AppCmp'); - const quickInfo = env.ngLS.getQuickInfoAtPosition(absoluteFrom('/app.html'), cursor); + + template.moveCursorToText(templateOverride); + const quickInfo = template.getQuickInfoAtPosition(); expect(quickInfo).toBeTruthy(); const {textSpan, displayParts} = quickInfo!; expect(text.substring(textSpan.start, textSpan.start + textSpan.length)) diff --git a/packages/language-service/ivy/testing/src/buffer.ts b/packages/language-service/ivy/testing/src/buffer.ts index 1664349891..7c0fc9798c 100644 --- a/packages/language-service/ivy/testing/src/buffer.ts +++ b/packages/language-service/ivy/testing/src/buffer.ts @@ -80,4 +80,8 @@ export class OpenBuffer { getTcb() { return this.ngLS.getTcb(this.scriptInfo.fileName, this._cursor); } + + getQuickInfoAtPosition() { + return this.ngLS.getQuickInfoAtPosition(this.scriptInfo.fileName, this._cursor); + } }