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
This commit is contained in:
parent
a32df388f8
commit
d30cd3309b
|
@ -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 !);
|
||||
}
|
||||
|
|
|
@ -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: '<div *ngFor></div> ~{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: '<div></div>'
|
||||
})
|
||||
|
@ -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: '<a *ngIf="(auth.isAdmin | async) || (event.leads && event.leads[(auth.uid | async)])"></a>'
|
||||
})
|
||||
|
@ -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: '<form><input [(ngModel)]="name" required /></form>'
|
||||
})
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue