|
| 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 {diffText, runTsurgeMigration} from '../../utils/tsurge/testing'; |
| 11 | +import {NgClassMigration} from './ngclass-to-class-migration'; |
| 12 | +import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing'; |
| 13 | + |
| 14 | +describe('ngClass migrator', () => { |
| 15 | + beforeEach(() => { |
| 16 | + initMockFileSystem('Native'); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('No change cases', () => { |
| 20 | + it('should not change static HTML elements', async () => { |
| 21 | + await verifyDeclarationNoChange(`<button id="123"></button>`); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should not change existing [class] bindings', async () => { |
| 25 | + await verifyDeclarationNoChange(`<div [class.active]="isActive"></div>`); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should not change empty ngClass binding', async () => { |
| 29 | + await verifyDeclarationNoChange(`<div [ngClass]="{}"></div>`); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should not change ngClass with empty string key', async () => { |
| 33 | + await verifyDeclarationNoChange(`<div [ngClass]="{'': condition}"></div>`); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should not change ngClass with empty array', async () => { |
| 37 | + await verifyDeclarationNoChange(`<div [ngClass]="[]"></div>`); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('Simple ngClass object migrations', () => { |
| 42 | + it('should migrate single condition', async () => { |
| 43 | + await verifyDeclaration({ |
| 44 | + before: `<div [ngClass]="{'active': isActive}"></div>`, |
| 45 | + after: `<div [class.active]="isActive"></div>`, |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should migrate multiple class conditions', async () => { |
| 50 | + await verifyDeclaration({ |
| 51 | + before: `<div [ngClass]="{'class1': condition1, 'class2': condition2}"></div>`, |
| 52 | + after: `<div [class.class1]="condition1" [class.class2]="condition2"></div>`, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should migrate quoted class names', async () => { |
| 57 | + await verifyDeclaration({ |
| 58 | + before: `<div [ngClass]="{'admin-panel': isAdmin, 'user-dense': isDense}"></div>`, |
| 59 | + after: `<div [class.admin-panel]="isAdmin" [class.user-dense]="isDense"></div>`, |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should split and migrate multiple classes in one key', async () => { |
| 64 | + await verifyDeclaration({ |
| 65 | + before: `<div [ngClass]="{'class1 class2': condition}"></div>`, |
| 66 | + after: `<div [class.class1]="condition" [class.class2]="condition"></div>`, |
| 67 | + }); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('Complex and multi-element migrations', () => { |
| 72 | + it('should migrate complex object literals with mixed class keys', async () => { |
| 73 | + await verifyDeclaration({ |
| 74 | + before: `<div [ngClass]="{'class1 class2': condition, 'class3': anotherCondition}"></div>`, |
| 75 | + after: `<div [class.class1]="condition" [class.class2]="condition" [class.class3]="anotherCondition"></div>`, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should trim and migrate keys with extra whitespace', async () => { |
| 80 | + await verifyDeclaration({ |
| 81 | + before: `<div [ngClass]="{' class1 ': condition, 'class2': anotherCondition}"></div>`, |
| 82 | + after: `<div [class.class1]="condition" [class.class2]="anotherCondition"></div>`, |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should migrate multiple ngClass bindings across multiple elements', async () => { |
| 87 | + await verifyDeclaration({ |
| 88 | + before: ` |
| 89 | + <div [ngClass]="{'class1': condition1, 'class2': condition2}"></div> |
| 90 | + <div [ngClass]="{'class3': condition3}"></div>`, |
| 91 | + after: ` |
| 92 | + <div [class.class1]="condition1" [class.class2]="condition2"></div> |
| 93 | + <div [class.class3]="condition3"></div>`, |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('Non-migratable and edge cases', () => { |
| 99 | + it('should not migrate invalid object literal syntax', async () => { |
| 100 | + await verifyDeclarationNoChange(`<div [ngClass]="{foo isActive}"></div>`); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should not migrate string literal class list', async () => { |
| 104 | + await verifyDeclarationNoChange(`<div [ngClass]="'class1 class2'"></div>`); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should not migrate dynamic variable bindings', async () => { |
| 108 | + await verifyDeclarationNoChange(`<div [ngClass]="dynamicClassObject"></div>`); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should not migrate function call bindings', async () => { |
| 112 | + await verifyDeclarationNoChange(`<div [ngClass]="getClasses()"></div>`); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +async function verifyDeclaration(testCase: {before: string; after: string}) { |
| 118 | + await verify({ |
| 119 | + before: populateDeclarationTestCase(testCase.before.trim()), |
| 120 | + after: populateExpectedResult(testCase.after.trim()), |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +async function verifyDeclarationNoChange(beforeAndAfter: string) { |
| 125 | + await verifyDeclaration({before: beforeAndAfter, after: beforeAndAfter}); |
| 126 | +} |
| 127 | + |
| 128 | +async function verify(testCase: {before: string; after: string}) { |
| 129 | + const {fs} = await runTsurgeMigration(new NgClassMigration(), [ |
| 130 | + { |
| 131 | + name: absoluteFrom('/app.component.ts'), |
| 132 | + isProgramRootFile: true, |
| 133 | + contents: testCase.before, |
| 134 | + }, |
| 135 | + ]); |
| 136 | + |
| 137 | + const actual = fs.readFile(absoluteFrom('/app.component.ts')).trim(); |
| 138 | + const expected = testCase.after.trim(); |
| 139 | + |
| 140 | + expect(actual).withContext(diffText(expected, actual)).toEqual(expected); |
| 141 | +} |
| 142 | + |
| 143 | +function populateDeclarationTestCase(declaration: string): string { |
| 144 | + return `import {Component} from '@angular/core'; |
| 145 | +@Component({ template: \`${declaration}\` }) |
| 146 | +export class AppComponent {}`; |
| 147 | +} |
| 148 | + |
| 149 | +function populateExpectedResult(declaration: string): string { |
| 150 | + return `import {Component} from '@angular/core'; |
| 151 | +@Component({ template: \`${declaration}\` }) |
| 152 | +export class AppComponent {}`; |
| 153 | +} |
0 commit comments