Skip to content

Commit e485236

Browse files
Keen Yee Liaukara
authored andcommitted
test(language-service): Inline test cases in parsing-cases.ts (angular#36495)
This commit removes individual components from parsing-cases.ts and colocate them with the actual tests. This makes the tests more readable. PR Close angular#36495
1 parent 41667de commit e485236

File tree

5 files changed

+45
-69
lines changed

5 files changed

+45
-69
lines changed

packages/language-service/test/completions_spec.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ describe('completions', () => {
168168
});
169169

170170
it('should be able to get completions in an empty interpolation', () => {
171-
const marker = mockHost.getLocationMarkerFor(PARSING_CASES, 'empty-interpolation');
172-
const completions = ngLS.getCompletionsAtPosition(PARSING_CASES, marker.start);
173-
expectContain(completions, CompletionKind.PROPERTY, ['title', 'subTitle']);
171+
mockHost.override(TEST_TEMPLATE, `{{ ~{cursor} }}`);
172+
const marker = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'cursor');
173+
const completions = ngLS.getCompletionsAtPosition(TEST_TEMPLATE, marker.start);
174+
expectContain(completions, CompletionKind.PROPERTY, ['title', 'hero']);
174175
});
175176

176177
it('should suggest $any() type cast function in an interpolation', () => {
@@ -282,9 +283,14 @@ describe('completions', () => {
282283

283284
describe('with a *ngIf', () => {
284285
it('should be able to get completions for exported *ngIf variable', () => {
285-
const marker = mockHost.getLocationMarkerFor(PARSING_CASES, 'promised-person-name');
286-
const completions = ngLS.getCompletionsAtPosition(PARSING_CASES, marker.start);
287-
expectContain(completions, CompletionKind.PROPERTY, ['name', 'age', 'street']);
286+
mockHost.override(TEST_TEMPLATE, `
287+
<div *ngIf="heroP | async as h">
288+
{{ h.~{cursor} }}
289+
</div>
290+
`);
291+
const marker = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'cursor');
292+
const completions = ngLS.getCompletionsAtPosition(TEST_TEMPLATE, marker.start);
293+
expectContain(completions, CompletionKind.PROPERTY, ['id', 'name']);
288294
});
289295
});
290296

@@ -368,9 +374,14 @@ describe('completions', () => {
368374
});
369375

370376
it('should be able to infer the type of a ngForOf with an async pipe', () => {
371-
const marker = mockHost.getLocationMarkerFor(PARSING_CASES, 'async-person-name');
372-
const completions = ngLS.getCompletionsAtPosition(PARSING_CASES, marker.start);
373-
expectContain(completions, CompletionKind.PROPERTY, ['name', 'age', 'street']);
377+
mockHost.override(TEST_TEMPLATE, `
378+
<div *ngFor="let h of heroesP | async">
379+
{{ h.~{cursor} }}
380+
</div>
381+
`);
382+
const marker = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'cursor');
383+
const completions = ngLS.getCompletionsAtPosition(TEST_TEMPLATE, marker.start);
384+
expectContain(completions, CompletionKind.PROPERTY, ['id', 'name']);
374385
});
375386

376387
it('should be able to resolve variable in nested loop', () => {
@@ -498,14 +509,27 @@ describe('completions', () => {
498509

499510
describe('with references', () => {
500511
it('should list references', () => {
501-
const marker = mockHost.getLocationMarkerFor(PARSING_CASES, 'test-comp-content');
502-
const completions = ngLS.getCompletionsAtPosition(PARSING_CASES, marker.start);
503-
expectContain(completions, CompletionKind.REFERENCE, ['div', 'test1', 'test2']);
512+
mockHost.override(TEST_TEMPLATE, `
513+
<div #myDiv>
514+
<test-comp #test1>
515+
{{ ~{cursor} }}
516+
</test-comp>
517+
</div>
518+
<test-comp #test2></test-comp>
519+
`);
520+
const marker = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'cursor');
521+
const completions = ngLS.getCompletionsAtPosition(TEST_TEMPLATE, marker.start);
522+
expectContain(completions, CompletionKind.REFERENCE, ['myDiv', 'test1', 'test2']);
504523
});
505524

506525
it('should reference the component', () => {
507-
const marker = mockHost.getLocationMarkerFor(PARSING_CASES, 'test-comp-after-test');
508-
const completions = ngLS.getCompletionsAtPosition(PARSING_CASES, marker.start);
526+
mockHost.override(TEST_TEMPLATE, `
527+
<test-comp #test1>
528+
{{ test1.~{cursor} }}
529+
</test-comp>
530+
`);
531+
const marker = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'cursor');
532+
const completions = ngLS.getCompletionsAtPosition(TEST_TEMPLATE, marker.start);
509533
expectContain(completions, CompletionKind.PROPERTY, ['name', 'testEvent']);
510534
});
511535

packages/language-service/test/diagnostics_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ describe('diagnostics', () => {
342342
expect(category).toBe(ts.DiagnosticCategory.Error);
343343
expect(messageText)
344344
.toBe(
345-
`Identifier 'missingField' is not defined. '{ implicitPerson: Person; }' does not contain such a member`,
345+
`Identifier 'missingField' is not defined. '{ implicitPerson: Hero; }' does not contain such a member`,
346346
);
347347
const span = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'emb');
348348
expect(start).toBe(span.start);
@@ -361,7 +361,7 @@ describe('diagnostics', () => {
361361
expect(category).toBe(ts.DiagnosticCategory.Error);
362362
expect(messageText)
363363
.toBe(
364-
`Identifier 'missingField' is not defined. 'Person' does not contain such a member`,
364+
`Identifier 'missingField' is not defined. 'Hero' does not contain such a member`,
365365
);
366366
const span = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'emb');
367367
expect(start).toBe(span.start);

packages/language-service/test/project/app/main.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@ import * as ParsingCases from './parsing-cases';
1616
imports: [CommonModule, FormsModule],
1717
declarations: [
1818
AppComponent,
19-
ParsingCases.AsyncForUsingComponent,
2019
ParsingCases.CaseIncompleteOpen,
2120
ParsingCases.CaseMissingClosing,
2221
ParsingCases.CaseUnknown,
2322
ParsingCases.CounterDirective,
24-
ParsingCases.EmptyInterpolation,
2523
ParsingCases.HintModel,
2624
ParsingCases.NoValueAttribute,
2725
ParsingCases.NumberModel,
28-
ParsingCases.References,
2926
ParsingCases.StringModel,
3027
ParsingCases.TemplateReference,
3128
ParsingCases.TestComponent,

packages/language-service/test/project/app/parsing-cases.ts

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -63,45 +63,6 @@ export class HintModel {
6363
hintChange: EventEmitter<string> = new EventEmitter();
6464
}
6565

66-
interface Person {
67-
name: string;
68-
age: number;
69-
street: string;
70-
}
71-
72-
@Component({
73-
template: `
74-
<div *ngFor="let person of people | async">
75-
{{person.~{async-person-name}name}}
76-
</div>
77-
<div *ngIf="promisedPerson | async as person">
78-
{{person.~{promised-person-name}name}}
79-
</div>
80-
`,
81-
})
82-
export class AsyncForUsingComponent {
83-
people: Promise<Person[]> = Promise.resolve([]);
84-
promisedPerson: Promise<Person> = Promise.resolve({
85-
name: 'John Doe',
86-
age: 42,
87-
street: '123 Angular Ln',
88-
});
89-
}
90-
91-
@Component({
92-
template: `
93-
<div #div>
94-
<test-comp #test1>
95-
{{~{test-comp-content}}}
96-
{{test1.~{test-comp-after-test}name}}
97-
{{div.~{test-comp-after-div}.innerText}}
98-
</test-comp>
99-
</div>
100-
<test-comp #test2></test-comp>`,
101-
})
102-
export class References {
103-
}
104-
10566
class CounterDirectiveContext<T> {
10667
constructor(public $implicit: T) {}
10768
}
@@ -121,8 +82,8 @@ export class CounterDirective implements OnChanges {
12182
}
12283

12384
interface WithContextDirectiveContext {
124-
$implicit: {implicitPerson: Person;};
125-
nonImplicitPerson: Person;
85+
$implicit: {implicitPerson: Hero;};
86+
nonImplicitPerson: Hero;
12687
}
12788

12889
@Directive({selector: '[withContext]'})
@@ -156,7 +117,9 @@ export class TemplateReference {
156117
*/
157118
title = 'Some title';
158119
hero: Hero = {id: 1, name: 'Windstorm'};
120+
heroP = Promise.resolve(this.hero);
159121
heroes: Hero[] = [this.hero];
122+
heroesP = Promise.resolve(this.heroes);
160123
tupleArray: [string, Hero] = ['test', this.hero];
161124
league: Hero[][] = [this.heroes];
162125
heroesByName: {[name: string]: Hero} = {};
@@ -171,11 +134,3 @@ export class TemplateReference {
171134
constNames = [{name: 'name'}] as const;
172135
private myField = 'My Field';
173136
}
174-
175-
@Component({
176-
template: '{{~{empty-interpolation}}}',
177-
})
178-
export class EmptyInterpolation {
179-
title = 'Some title';
180-
subTitle = 'Some sub title';
181-
}

packages/language-service/test/typescript_host_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('TypeScriptServiceHost', () => {
9494
const tsLS = ts.createLanguageService(tsLSHost);
9595
const ngLSHost = new TypeScriptServiceHost(tsLSHost, tsLS);
9696
const templates = ngLSHost.getTemplates('/app/parsing-cases.ts');
97-
expect(templates.length).toBe(8);
97+
expect(templates.length).toBe(5);
9898
});
9999

100100
it('should be able to find external template', () => {

0 commit comments

Comments
 (0)