From d30cd3309b844a7dcdc6b18fea0bd05f92fe3ace Mon Sep 17 00:00:00 2001 From: ayazhafiz Date: Tue, 10 Sep 2019 11:21:12 -0500 Subject: [PATCH] refactor(language-service): move to using mockHost as much as possible (#32589) Update tests that still do not use `mockHost` for certain operations, like `addCode`. PR Close #32589 --- .../language-service/test/completions_spec.ts | 19 ++++++++---------- .../language-service/test/diagnostics_spec.ts | 20 ++++++------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/packages/language-service/test/completions_spec.ts b/packages/language-service/test/completions_spec.ts index 03c9cb3a46..edf2b8be09 100644 --- a/packages/language-service/test/completions_spec.ts +++ b/packages/language-service/test/completions_spec.ts @@ -48,7 +48,7 @@ describe('completions', () => { }); it('should be able to infer the type of a ngForOf', () => { - addCode( + addCodeAndCallback( ` interface Person { name: string, @@ -63,7 +63,7 @@ describe('completions', () => { }); it('should be able to infer the type of a ngForOf with an async pipe', () => { - addCode( + addCodeAndCallback( ` interface Person { name: string, @@ -142,13 +142,13 @@ describe('completions', () => { export class MyComponent { }`; - addCode(code, fileName => { contains(fileName, 'inside-template', 'h1'); }); + addCodeAndCallback(code, fileName => { contains(fileName, 'inside-template', 'h1'); }); }).not.toThrow(); }); it('should hot crash with an incomplete class', () => { expect(() => { - addCode('\nexport class', fileName => { ngHost.getAnalyzedModules(); }); + addCodeAndCallback('\nexport class', fileName => { ngHost.getAnalyzedModules(); }); }).not.toThrow(); }); @@ -182,7 +182,7 @@ export class MyComponent { }); it('should work with input and output', () => { - addCode( + addCodeAndCallback( ` @Component({ selector: 'foo-component', @@ -202,14 +202,11 @@ export class MyComponent { }); }); - 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); + function addCodeAndCallback(code: string, cb: (fileName: string, content?: string) => void) { + const fileName = mockHost.addCode(code); ngHost.getAnalyzedModules(); try { - cb(fileName, newContent); + cb(fileName, mockHost.getFileContent(fileName) !); } finally { mockHost.override(fileName, undefined !); } diff --git a/packages/language-service/test/diagnostics_spec.ts b/packages/language-service/test/diagnostics_spec.ts index 810aefcbed..dc1409fb24 100644 --- a/packages/language-service/test/diagnostics_spec.ts +++ b/packages/language-service/test/diagnostics_spec.ts @@ -15,7 +15,7 @@ import {MockTypescriptHost} from './test_utils'; /** * Note: If we want to test that a specific diagnostic message is emitted, then - * use the `addCode()` helper method to add code to an existing file and check + * use the `mockHost.addCode()` helper method to add code to an existing file and check * that the diagnostic messages contain the expected output. * * If the goal is to assert that there is no error in a specific file, then use @@ -90,7 +90,7 @@ describe('diagnostics', () => { }); it('should not crash with a incomplete *ngFor', () => { - const fileName = addCode(` + const fileName = mockHost.addCode(` @Component({ template: '
~{after-div}' }) @@ -99,7 +99,7 @@ describe('diagnostics', () => { }); it('should report a component not in a module', () => { - const fileName = addCode(` + const fileName = mockHost.addCode(` @Component({ template: '
' }) @@ -132,7 +132,7 @@ describe('diagnostics', () => { }); it('should not throw getting diagnostics for an index expression', () => { - const fileName = addCode(` + const fileName = mockHost.addCode(` @Component({ template: '' }) @@ -141,7 +141,7 @@ describe('diagnostics', () => { }); it('should not throw using a directive with no value', () => { - const fileName = addCode(` + const fileName = mockHost.addCode(` @Component({ template: '
' }) @@ -185,7 +185,7 @@ describe('diagnostics', () => { }); it('should not throw for an invalid class', () => { - const fileName = addCode(` + const fileName = mockHost.addCode(` @Component({ template: '' }) class`); @@ -501,12 +501,4 @@ describe('diagnostics', () => { }); */ - function addCode(code: string) { - const fileName = '/app/app.component.ts'; - const originalContent = mockHost.getFileContent(fileName); - const newContent = originalContent + code; - mockHost.override(fileName, newContent); - return fileName; - } - });