@@ -13,7 +13,7 @@ import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/test
1313import { runfiles } from '@bazel/runfiles' ;
1414import shx from 'shelljs' ;
1515
16- describe ( 'control flow migration' , ( ) => {
16+ describe ( 'control flow migration (ng update) ' , ( ) => {
1717 let runner : SchematicTestRunner ;
1818 let host : TempScopedNodeJsSyncHost ;
1919 let tree : UnitTestTree ;
@@ -6831,3 +6831,189 @@ describe('control flow migration', () => {
68316831 } ) ;
68326832 } ) ;
68336833} ) ;
6834+
6835+ describe ( 'control flow migration (ng generate)' , ( ) => {
6836+ let runner : SchematicTestRunner ;
6837+ let host : TempScopedNodeJsSyncHost ;
6838+ let tree : UnitTestTree ;
6839+ let tmpDirPath : string ;
6840+ let previousWorkingDir : string ;
6841+ let errorOutput : string [ ] = [ ] ;
6842+ let warnOutput : string [ ] = [ ] ;
6843+
6844+ function writeFile ( filePath : string , contents : string ) {
6845+ host . sync . write ( normalize ( filePath ) , virtualFs . stringToFileBuffer ( contents ) ) ;
6846+ }
6847+
6848+ function runMigration ( path : string | undefined = undefined , format : boolean = true ) {
6849+ return runner . runSchematic ( 'control-flow-migration' , { path, format} , tree ) ;
6850+ }
6851+
6852+ beforeEach ( ( ) => {
6853+ runner = new SchematicTestRunner ( 'test' , runfiles . resolvePackageRelative ( '../collection.json' ) ) ;
6854+ host = new TempScopedNodeJsSyncHost ( ) ;
6855+ tree = new UnitTestTree ( new HostTree ( host ) ) ;
6856+
6857+ errorOutput = [ ] ;
6858+ warnOutput = [ ] ;
6859+ runner . logger . subscribe ( ( e : logging . LogEntry ) => {
6860+ if ( e . level === 'error' ) {
6861+ errorOutput . push ( e . message ) ;
6862+ } else if ( e . level === 'warn' ) {
6863+ warnOutput . push ( e . message ) ;
6864+ }
6865+ } ) ;
6866+
6867+ writeFile ( '/tsconfig.json' , '{}' ) ;
6868+ writeFile (
6869+ '/angular.json' ,
6870+ JSON . stringify ( {
6871+ version : 1 ,
6872+ projects : { t : { root : '' , architect : { build : { options : { tsConfig : './tsconfig.json' } } } } } ,
6873+ } ) ,
6874+ ) ;
6875+
6876+ previousWorkingDir = shx . pwd ( ) ;
6877+ tmpDirPath = getSystemPath ( host . root ) ;
6878+
6879+ // Switch into the temporary directory path. This allows us to run
6880+ // the schematic against our custom unit test tree.
6881+ shx . cd ( tmpDirPath ) ;
6882+ } ) ;
6883+
6884+ afterEach ( ( ) => {
6885+ shx . cd ( previousWorkingDir ) ;
6886+ shx . rm ( '-r' , tmpDirPath ) ;
6887+ } ) ;
6888+
6889+ describe ( 'path' , ( ) => {
6890+ it ( 'should throw an error if no files match the passed-in path' , async ( ) => {
6891+ let error : string | null = null ;
6892+
6893+ writeFile (
6894+ 'dir.ts' ,
6895+ `
6896+ import {Directive} from '@angular/core';
6897+ @Directive({selector: '[dir]'})
6898+ export class MyDir {}
6899+ ` ,
6900+ ) ;
6901+
6902+ try {
6903+ await runMigration ( './foo' ) ;
6904+ } catch ( e : any ) {
6905+ error = e . message ;
6906+ }
6907+
6908+ expect ( error ) . toMatch (
6909+ / C o u l d n o t f i n d a n y f i l e s t o m i g r a t e u n d e r t h e p a t h .* \/ f o o \. C a n n o t r u n t h e c o n t r o l f l o w m i g r a t i o n / ,
6910+ ) ;
6911+ } ) ;
6912+
6913+ it ( 'should throw an error if a path outside of the project is passed in' , async ( ) => {
6914+ let error : string | null = null ;
6915+
6916+ writeFile (
6917+ 'dir.ts' ,
6918+ `
6919+ import {Directive} from '@angular/core';
6920+ @Directive({selector: '[dir]'})
6921+ export class MyDir {}
6922+ ` ,
6923+ ) ;
6924+
6925+ try {
6926+ await runMigration ( '../foo' ) ;
6927+ } catch ( e : any ) {
6928+ error = e . message ;
6929+ }
6930+ expect ( error ) . toBe ( 'Cannot run control flow migration outside of the current project.' ) ;
6931+ } ) ;
6932+
6933+ it ( 'should only migrate the paths that were passed in' , async ( ) => {
6934+ writeFile (
6935+ 'comp.ts' ,
6936+ `
6937+ import {Component} from '@angular/core';
6938+ import {NgIf} from '@angular/common';
6939+ @Component({
6940+ imports: [NgIf, NgFor,NgSwitch,NgSwitchCase ,NgSwitchDefault],
6941+ template: \`<div><span *ngIf="toggle">This should be hidden</span></div>\`
6942+ })
6943+ class Comp {
6944+ toggle = false;
6945+ }
6946+ ` ,
6947+ ) ;
6948+
6949+ writeFile (
6950+ 'skip.ts' ,
6951+ `
6952+ import {Component} from '@angular/core';
6953+ import {NgIf} from '@angular/common';
6954+ @Component({
6955+ imports: [NgIf],
6956+ template: \`<div *ngIf="show">Show me</div>\`
6957+ })
6958+ class Comp {
6959+ show = false;
6960+ }
6961+ ` ,
6962+ ) ;
6963+
6964+ await runMigration ( './comp.ts' ) ;
6965+ const migratedContent = tree . readContent ( '/comp.ts' ) ;
6966+ const skippedContent = tree . readContent ( '/skip.ts' ) ;
6967+
6968+ expect ( migratedContent ) . toContain (
6969+ 'template: `<div>@if (toggle) {<span>This should be hidden</span>}</div>`' ,
6970+ ) ;
6971+ expect ( migratedContent ) . toContain ( 'imports: []' ) ;
6972+ expect ( migratedContent ) . not . toContain ( `import {NgIf} from '@angular/common';` ) ;
6973+ expect ( skippedContent ) . toContain ( 'template: `<div *ngIf="show">Show me</div>`' ) ;
6974+ expect ( skippedContent ) . toContain ( 'imports: [NgIf]' ) ;
6975+ expect ( skippedContent ) . toContain ( `import {NgIf} from '@angular/common';` ) ;
6976+ } ) ;
6977+ } ) ;
6978+
6979+ it ( 'should migrate an if else case and not format' , async ( ) => {
6980+ writeFile (
6981+ '/comp.ts' ,
6982+ `
6983+ import {Component} from '@angular/core';
6984+ import {NgIf} from '@angular/common';
6985+ @Component({
6986+ templateUrl: './comp.html'
6987+ })
6988+ class Comp {
6989+ show = false;
6990+ }
6991+ ` ,
6992+ ) ;
6993+
6994+ writeFile (
6995+ '/comp.html' ,
6996+ [
6997+ `<div>` ,
6998+ `<span *ngIf="show;else elseBlock">Content here</span>` ,
6999+ `<ng-template #elseBlock>Else Content</ng-template>` ,
7000+ `</div>` ,
7001+ ] . join ( '\n' ) ,
7002+ ) ;
7003+
7004+ await runMigration ( undefined , false ) ;
7005+ const content = tree . readContent ( '/comp.html' ) ;
7006+
7007+ expect ( content ) . toBe (
7008+ [
7009+ `<div>` ,
7010+ `@if (show) {` ,
7011+ `<span>Content here</span>` ,
7012+ `} @else {` ,
7013+ `Else Content` ,
7014+ `}\n` ,
7015+ `</div>` ,
7016+ ] . join ( '\n' ) ,
7017+ ) ;
7018+ } ) ;
7019+ } ) ;
0 commit comments