refactor(language-service): remove callback functions in test code (#32656)

Removes `addCodeAndCallback` function, opting instead to add code to a
file and then testing expectations on that fileName. Also renames
`contains` to `expectContains` to clarify its expectations.

PR Close #32656
This commit is contained in:
ayazhafiz 2019-09-12 21:34:05 -05:00 committed by Kara Erickson
parent bbb2798d41
commit c7ea3260bc

View File

@ -21,24 +21,24 @@ describe('completions', () => {
let ngService = createLanguageService(ngHost); let ngService = createLanguageService(ngHost);
it('should be able to get entity completions', it('should be able to get entity completions',
() => { contains('/app/test.ng', 'entity-amp', '&', '>', '<', 'ι'); }); () => { expectContains('/app/test.ng', 'entity-amp', '&', '>', '<', 'ι'); });
it('should be able to return html elements', () => { it('should be able to return html elements', () => {
let htmlTags = ['h1', 'h2', 'div', 'span']; let htmlTags = ['h1', 'h2', 'div', 'span'];
let locations = ['empty', 'start-tag-h1', 'h1-content', 'start-tag', 'start-tag-after-h']; let locations = ['empty', 'start-tag-h1', 'h1-content', 'start-tag', 'start-tag-after-h'];
for (let location of locations) { for (let location of locations) {
contains('/app/test.ng', location, ...htmlTags); expectContains('/app/test.ng', location, ...htmlTags);
} }
}); });
it('should be able to return element diretives', it('should be able to return element diretives',
() => { contains('/app/test.ng', 'empty', 'my-app'); }); () => { expectContains('/app/test.ng', 'empty', 'my-app'); });
it('should be able to return h1 attributes', it('should be able to return h1 attributes',
() => { contains('/app/test.ng', 'h1-after-space', 'id', 'dir', 'lang', 'onclick'); }); () => { expectContains('/app/test.ng', 'h1-after-space', 'id', 'dir', 'lang', 'onclick'); });
it('should be able to find common angular attributes', it('should be able to find common angular attributes',
() => { contains('/app/test.ng', 'div-attributes', '(click)', '[ngClass]'); }); () => { expectContains('/app/test.ng', 'div-attributes', '(click)', '[ngClass]'); });
it('should be able to get completions in some random garbage', () => { it('should be able to get completions in some random garbage', () => {
const fileName = '/app/test.ng'; const fileName = '/app/test.ng';
@ -48,8 +48,7 @@ describe('completions', () => {
}); });
it('should be able to infer the type of a ngForOf', () => { it('should be able to infer the type of a ngForOf', () => {
addCodeAndCallback( const fileName = mockHost.addCode(`
`
interface Person { interface Person {
name: string, name: string,
street: string street: string
@ -58,13 +57,12 @@ describe('completions', () => {
@Component({template: '<div *ngFor="let person of people">{{person.~{name}name}}</div'}) @Component({template: '<div *ngFor="let person of people">{{person.~{name}name}}</div'})
export class MyComponent { export class MyComponent {
people: Person[] people: Person[]
}`, }`);
() => { contains('/app/app.component.ts', 'name', 'name', 'street'); }); expectContains(fileName, 'name', 'name', 'street');
}); });
it('should be able to infer the type of a ngForOf with an async pipe', () => { it('should be able to infer the type of a ngForOf with an async pipe', () => {
addCodeAndCallback( const fileName = mockHost.addCode(`
`
interface Person { interface Person {
name: string, name: string,
street: string street: string
@ -73,8 +71,8 @@ describe('completions', () => {
@Component({template: '<div *ngFor="let person of people | async">{{person.~{name}name}}</div'}) @Component({template: '<div *ngFor="let person of people | async">{{person.~{name}name}}</div'})
export class MyComponent { export class MyComponent {
people: Promise<Person[]>; people: Promise<Person[]>;
}`, }`);
() => { contains('/app/app.component.ts', 'name', 'name', 'street'); }); expectContains(fileName, 'name', 'name', 'street');
}); });
it('should be able to complete every character in the file', () => { it('should be able to complete every character in the file', () => {
@ -135,20 +133,22 @@ describe('completions', () => {
describe('with regression tests', () => { describe('with regression tests', () => {
it('should not crash with an incomplete component', () => { it('should not crash with an incomplete component', () => {
expect(() => { expect(() => {
const code = ` const fileName = mockHost.addCode(`
@Component({ @Component({
template: '~{inside-template}' template: '~{inside-template}'
}) })
export class MyComponent { export class MyComponent {
}`; }`);
addCodeAndCallback(code, fileName => { contains(fileName, 'inside-template', 'h1'); });
expectContains(fileName, 'inside-template', 'h1');
}).not.toThrow(); }).not.toThrow();
}); });
it('should hot crash with an incomplete class', () => { it('should hot crash with an incomplete class', () => {
expect(() => { expect(() => {
addCodeAndCallback('\nexport class', fileName => { ngHost.getAnalyzedModules(); }); mockHost.addCode('\nexport class');
ngHost.getAnalyzedModules();
}).not.toThrow(); }).not.toThrow();
}); });
@ -178,12 +178,11 @@ export class MyComponent {
} }
`); `);
ngHost.getAnalyzedModules(); ngHost.getAnalyzedModules();
contains('/app/my.component.ts', 'tree', 'children'); expectContains('/app/my.component.ts', 'tree', 'children');
}); });
it('should work with input and output', () => { it('should work with input and output', () => {
addCodeAndCallback( const fileName = mockHost.addCode(`
`
@Component({ @Component({
selector: 'foo-component', selector: 'foo-component',
template: \` template: \`
@ -195,24 +194,12 @@ export class MyComponent {
text: string; text: string;
value: number; value: number;
} }
`, `);
(fileName) => { expectContains(fileName, 'stringMarker', '[model]', '(model)');
contains(fileName, 'stringMarker', '[model]', '(model)'); expectContains(fileName, 'numberMarker', '[inputAlias]', '(outputAlias)');
contains(fileName, 'numberMarker', '[inputAlias]', '(outputAlias)');
});
}); });
function addCodeAndCallback(code: string, cb: (fileName: string, content?: string) => void) { function expectContains(fileName: string, locationMarker: string, ...names: string[]) {
const fileName = mockHost.addCode(code);
ngHost.getAnalyzedModules();
try {
cb(fileName, mockHost.getFileContent(fileName) !);
} finally {
mockHost.override(fileName, undefined !);
}
}
function contains(fileName: string, locationMarker: string, ...names: string[]) {
let location = mockHost.getMarkerLocations(fileName) ![locationMarker]; let location = mockHost.getMarkerLocations(fileName) ![locationMarker];
if (location == null) { if (location == null) {
throw new Error(`No marker ${locationMarker} found.`); throw new Error(`No marker ${locationMarker} found.`);