|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol'; |
| 10 | +import { ServerNotification, ServerRequest } from '@modelcontextprotocol/sdk/types'; |
| 11 | +import ts from 'typescript'; |
| 12 | +import { migrateSingleFile } from './migrate_single_file'; |
| 13 | + |
| 14 | +const fakeExtras = { |
| 15 | + sendDebugMessage: jasmine.createSpy(), |
| 16 | + sendNotification: jasmine.createSpy(), |
| 17 | +} as unknown as RequestHandlerExtra<ServerRequest, ServerNotification>; |
| 18 | + |
| 19 | +describe('migrateSingleFile', () => { |
| 20 | + it('should identify test files by extension', async () => { |
| 21 | + const fileName = 'test.spec.ts'; |
| 22 | + const sourceFile = ts.createSourceFile(fileName, '', ts.ScriptTarget.ESNext, true); |
| 23 | + |
| 24 | + const result = await migrateSingleFile(sourceFile, fakeExtras); |
| 25 | + |
| 26 | + expect(result?.content[0].text).toContain( |
| 27 | + 'The test file `test.spec.ts` is not yet configured for zoneless change detection.' + |
| 28 | + ' You need to enable it for the entire test suite and then identify which specific tests fail.', |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should identify test files by TestBed import', async () => { |
| 33 | + const fileName = 'test.ts'; |
| 34 | + const content = `import { TestBed } from '@angular/core/testing';`; |
| 35 | + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.ESNext, true); |
| 36 | + |
| 37 | + const result = await migrateSingleFile(sourceFile, fakeExtras); |
| 38 | + |
| 39 | + expect(result?.content[0].text).toContain( |
| 40 | + 'The test file `test.ts` is not yet configured for zoneless change detection.' + |
| 41 | + ' You need to enable it for the entire test suite and then identify which specific tests fail.', |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return unsupported zone usages message if NgZone is used', async () => { |
| 46 | + const fileName = 'app.component.ts'; |
| 47 | + const content = ` |
| 48 | + import { Component, NgZone } from '@angular/core'; |
| 49 | +
|
| 50 | + @Component({ |
| 51 | + selector: 'app-root', |
| 52 | + template: 'Hello', |
| 53 | + }) |
| 54 | + export class AppComponent { |
| 55 | + constructor(private zone: NgZone) { |
| 56 | + this.zone.onMicrotaskEmpty(() => {}); |
| 57 | + } |
| 58 | + } |
| 59 | + `; |
| 60 | + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.ESNext, true); |
| 61 | + |
| 62 | + const result = await migrateSingleFile(sourceFile, fakeExtras); |
| 63 | + |
| 64 | + expect(result?.content[0].text).toContain( |
| 65 | + 'The component uses NgZone APIs that are incompatible with zoneless applications', |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should return null if component already has ChangeDetectionStrategy.OnPush', async () => { |
| 70 | + const fileName = 'app.component.ts'; |
| 71 | + const content = ` |
| 72 | + import { Component, ChangeDetectionStrategy } from '@angular/core'; |
| 73 | +
|
| 74 | + @Component({ |
| 75 | + selector: 'app-root', |
| 76 | + template: 'Hello', |
| 77 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 78 | + }) |
| 79 | + export class AppComponent {} |
| 80 | + `; |
| 81 | + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.ESNext, true); |
| 82 | + |
| 83 | + const result = await migrateSingleFile(sourceFile, fakeExtras); |
| 84 | + |
| 85 | + expect(result).toBeNull(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return null if component has ChangeDetectionStrategy.Default', async () => { |
| 89 | + const fileName = 'app.component.ts'; |
| 90 | + const content = ` |
| 91 | + import { Component, ChangeDetectionStrategy } from '@angular/core'; |
| 92 | +
|
| 93 | + @Component({ |
| 94 | + selector: 'app-root', |
| 95 | + template: 'Hello', |
| 96 | + changeDetection: ChangeDetectionStrategy.Default, |
| 97 | + }) |
| 98 | + export class AppComponent {} |
| 99 | + `; |
| 100 | + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.ESNext, true); |
| 101 | + |
| 102 | + const result = await migrateSingleFile(sourceFile, fakeExtras); |
| 103 | + |
| 104 | + expect(result).toBeNull(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return migration instructions for a component without a change detection strategy', async () => { |
| 108 | + const fileName = 'app.component.ts'; |
| 109 | + const content = ` |
| 110 | + import { Component } from '@angular/core'; |
| 111 | +
|
| 112 | + @Component({ |
| 113 | + selector: 'app-root', |
| 114 | + template: 'Hello', |
| 115 | + }) |
| 116 | + export class AppComponent {} |
| 117 | + `; |
| 118 | + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.ESNext, true); |
| 119 | + |
| 120 | + const result = await migrateSingleFile(sourceFile, fakeExtras); |
| 121 | + |
| 122 | + expect(result?.content[0].text).toContain( |
| 123 | + 'The component does not currently use a change detection strategy, which means it may rely on Zone.js', |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should return null for a file that is not a component', async () => { |
| 128 | + const fileName = 'some.service.ts'; |
| 129 | + const content = ` |
| 130 | + import { Injectable } from '@angular/core'; |
| 131 | +
|
| 132 | + @Injectable({ providedIn: 'root' }) |
| 133 | + export class SomeService {} |
| 134 | + `; |
| 135 | + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.ESNext, true); |
| 136 | + |
| 137 | + const result = await migrateSingleFile(sourceFile, fakeExtras); |
| 138 | + |
| 139 | + expect(result).toBeNull(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should return null for an empty file', async () => { |
| 143 | + const fileName = 'empty.ts'; |
| 144 | + const content = ``; |
| 145 | + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.ESNext, true); |
| 146 | + |
| 147 | + const result = await migrateSingleFile(sourceFile, fakeExtras); |
| 148 | + |
| 149 | + expect(result).toBeNull(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments