|
| 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 {absoluteFrom} from '@angular/compiler-cli'; |
| 10 | +import {initMockFileSystem} from '@angular/compiler-cli/private/testing'; |
| 11 | +import {runTsurgeMigration} from '../../utils/tsurge/testing'; |
| 12 | +import {ModelOutputMigration} from './migration'; |
| 13 | + |
| 14 | +describe('ModelOutput migration', () => { |
| 15 | + beforeEach(() => { |
| 16 | + initMockFileSystem('Native'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should migrate model() to input() + linkedSignal() when there is a conflicting output', async () => { |
| 20 | + const {fs} = await runTsurgeMigration(new ModelOutputMigration(), [ |
| 21 | + { |
| 22 | + name: absoluteFrom('/index.ts'), |
| 23 | + isProgramRootFile: true, |
| 24 | + contents: ` |
| 25 | + import { Component, model, output } from '@angular/core'; |
| 26 | +
|
| 27 | + @Component({ |
| 28 | + selector: 'my-comp', |
| 29 | + template: '' |
| 30 | + }) |
| 31 | + export class MyComp { |
| 32 | + foo = model(0); |
| 33 | + fooChange = output<number>(); |
| 34 | + } |
| 35 | + `, |
| 36 | + }, |
| 37 | + ]); |
| 38 | + |
| 39 | + const content = fs.readFile(absoluteFrom('/index.ts')); |
| 40 | + expect(content).toContain("fooInput = input(0, {alias: 'foo'});"); |
| 41 | + expect(content).toContain('foo = linkedSignal(this.fooInput);'); |
| 42 | + expect(content).toContain('fooChange = output<number>();'); |
| 43 | + expect(content).toContain( |
| 44 | + "import { Component, model, output, input, linkedSignal } from '@angular/core';", |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should handle generic types', async () => { |
| 49 | + const {fs} = await runTsurgeMigration(new ModelOutputMigration(), [ |
| 50 | + { |
| 51 | + name: absoluteFrom('/index.ts'), |
| 52 | + isProgramRootFile: true, |
| 53 | + contents: ` |
| 54 | + import { Component, model, output } from '@angular/core'; |
| 55 | +
|
| 56 | + @Component({ |
| 57 | + selector: 'my-comp', |
| 58 | + template: '' |
| 59 | + }) |
| 60 | + export class MyComp { |
| 61 | + bar = model<string>('initial'); |
| 62 | + barChange = output<string>(); |
| 63 | + } |
| 64 | + `, |
| 65 | + }, |
| 66 | + ]); |
| 67 | + |
| 68 | + const content = fs.readFile(absoluteFrom('/index.ts')); |
| 69 | + expect(content).toContain("barInput = input<string>('initial', {alias: 'bar'});"); |
| 70 | + expect(content).toContain('bar = linkedSignal(this.barInput);'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should preserve modifiers', async () => { |
| 74 | + const {fs} = await runTsurgeMigration(new ModelOutputMigration(), [ |
| 75 | + { |
| 76 | + name: absoluteFrom('/index.ts'), |
| 77 | + isProgramRootFile: true, |
| 78 | + contents: ` |
| 79 | + import { Component, model, output } from '@angular/core'; |
| 80 | +
|
| 81 | + @Component({ |
| 82 | + selector: 'my-comp', |
| 83 | + template: '' |
| 84 | + }) |
| 85 | + export class MyComp { |
| 86 | + public val = model(123); |
| 87 | + public valChange = output<number>(); |
| 88 | + } |
| 89 | + `, |
| 90 | + }, |
| 91 | + ]); |
| 92 | + |
| 93 | + const content = fs.readFile(absoluteFrom('/index.ts')); |
| 94 | + expect(content).toContain("public valInput = input(123, {alias: 'val'});"); |
| 95 | + expect(content).toContain('public val = linkedSignal(this.valInput);'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should handle model.required()', async () => { |
| 99 | + const {fs} = await runTsurgeMigration(new ModelOutputMigration(), [ |
| 100 | + { |
| 101 | + name: absoluteFrom('/index.ts'), |
| 102 | + isProgramRootFile: true, |
| 103 | + contents: ` |
| 104 | + import { Component, model, output } from '@angular/core'; |
| 105 | +
|
| 106 | + @Component({ |
| 107 | + selector: 'my-comp', |
| 108 | + template: '' |
| 109 | + }) |
| 110 | + export class MyComp { |
| 111 | + foo = model.required<number>(); |
| 112 | + fooChange = output<number>(); |
| 113 | + } |
| 114 | + `, |
| 115 | + }, |
| 116 | + ]); |
| 117 | + |
| 118 | + const content = fs.readFile(absoluteFrom('/index.ts')); |
| 119 | + expect(content).toContain("fooInput = input.required<number>({alias: 'foo'});"); |
| 120 | + expect(content).toContain('foo = linkedSignal(this.fooInput);'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle @Output() decorator as conflict', async () => { |
| 124 | + const {fs} = await runTsurgeMigration(new ModelOutputMigration(), [ |
| 125 | + { |
| 126 | + name: absoluteFrom('/index.ts'), |
| 127 | + isProgramRootFile: true, |
| 128 | + contents: ` |
| 129 | + import { Component, model, Output, EventEmitter } from '@angular/core'; |
| 130 | +
|
| 131 | + @Component({ |
| 132 | + selector: 'my-comp', |
| 133 | + template: '' |
| 134 | + }) |
| 135 | + export class MyComp { |
| 136 | + baz = model(true); |
| 137 | + @Output() bazChange = new EventEmitter<boolean>(); |
| 138 | + } |
| 139 | + `, |
| 140 | + }, |
| 141 | + ]); |
| 142 | + |
| 143 | + const content = fs.readFile(absoluteFrom('/index.ts')); |
| 144 | + expect(content).toContain("bazInput = input(true, {alias: 'baz'});"); |
| 145 | + expect(content).toContain('baz = linkedSignal(this.bazInput);'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should NOT migrate model() if there is no conflicting output', async () => { |
| 149 | + const originalContent = ` |
| 150 | + import { Component, model } from '@angular/core'; |
| 151 | +
|
| 152 | + @Component({ |
| 153 | + selector: 'my-comp', |
| 154 | + template: '' |
| 155 | + }) |
| 156 | + export class MyComp { |
| 157 | + foo = model(0); |
| 158 | + } |
| 159 | + `; |
| 160 | + const {fs} = await runTsurgeMigration(new ModelOutputMigration(), [ |
| 161 | + { |
| 162 | + name: absoluteFrom('/index.ts'), |
| 163 | + isProgramRootFile: true, |
| 164 | + contents: originalContent, |
| 165 | + }, |
| 166 | + ]); |
| 167 | + |
| 168 | + const content = fs.readFile(absoluteFrom('/index.ts')); |
| 169 | + // It might normalize some spaces or imports, so check the core logic |
| 170 | + expect(content).not.toContain('input('); |
| 171 | + expect(content).not.toContain('linkedSignal('); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should merge existing options in model()', async () => { |
| 175 | + const {fs} = await runTsurgeMigration(new ModelOutputMigration(), [ |
| 176 | + { |
| 177 | + name: absoluteFrom('/index.ts'), |
| 178 | + isProgramRootFile: true, |
| 179 | + contents: ` |
| 180 | + import { Component, model, output } from '@angular/core'; |
| 181 | +
|
| 182 | + @Component({ |
| 183 | + selector: 'my-comp', |
| 184 | + template: '' |
| 185 | + }) |
| 186 | + export class MyComp { |
| 187 | + foo = model(0, {debugName: 'my-foo'}); |
| 188 | + fooChange = output<number>(); |
| 189 | + } |
| 190 | + `, |
| 191 | + }, |
| 192 | + ]); |
| 193 | + |
| 194 | + const content = fs.readFile(absoluteFrom('/index.ts')); |
| 195 | + expect(content).toContain("fooInput = input(0, {alias: 'foo', debugName: 'my-foo'});"); |
| 196 | + }); |
| 197 | +}); |
0 commit comments