|
| 1 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { EmptyTree } from '@angular-devkit/schematics'; |
| 4 | +import { test, expect } from 'vitest'; |
| 5 | + |
| 6 | +test('migrates imports from @testing-library/angular to @testing-library/angular/zoneless', async () => { |
| 7 | + const before = ` |
| 8 | +import { render, screen } from '@testing-library/angular'; |
| 9 | +import { AppComponent } from './app.component'; |
| 10 | +
|
| 11 | +describe('AppComponent', () => { |
| 12 | + it('should render', async () => { |
| 13 | + await render(AppComponent); |
| 14 | + expect(screen.getByText('Hello')).toBeInTheDocument(); |
| 15 | + }); |
| 16 | +}); |
| 17 | +`; |
| 18 | + |
| 19 | + const after = ` |
| 20 | +import { render, screen } from '@testing-library/angular/zoneless'; |
| 21 | +import { AppComponent } from './app.component'; |
| 22 | +
|
| 23 | +describe('AppComponent', () => { |
| 24 | + it('should render', async () => { |
| 25 | + await render(AppComponent); |
| 26 | + expect(screen.getByText('Hello')).toBeInTheDocument(); |
| 27 | + }); |
| 28 | +}); |
| 29 | +`; |
| 30 | + |
| 31 | + const tree = await setup({ |
| 32 | + 'src/app.spec.ts': before, |
| 33 | + }); |
| 34 | + |
| 35 | + expect(tree.readContent('src/app.spec.ts')).toBe(after); |
| 36 | +}); |
| 37 | + |
| 38 | +test('migrates imports with double quotes', async () => { |
| 39 | + const before = `import { render } from "@testing-library/angular";`; |
| 40 | + const after = `import { render } from "@testing-library/angular/zoneless";`; |
| 41 | + |
| 42 | + const tree = await setup({ |
| 43 | + 'src/test.spec.ts': before, |
| 44 | + }); |
| 45 | + |
| 46 | + expect(tree.readContent('src/test.spec.ts')).toBe(after); |
| 47 | +}); |
| 48 | + |
| 49 | +test('migrates multiple imports in the same file', async () => { |
| 50 | + const before = ` |
| 51 | +import { render, screen } from '@testing-library/angular'; |
| 52 | +import { fireEvent } from '@testing-library/angular'; |
| 53 | +`; |
| 54 | + |
| 55 | + const after = ` |
| 56 | +import { render, screen } from '@testing-library/angular/zoneless'; |
| 57 | +import { fireEvent } from '@testing-library/angular/zoneless'; |
| 58 | +`; |
| 59 | + |
| 60 | + const tree = await setup({ |
| 61 | + 'src/multi.spec.ts': before, |
| 62 | + }); |
| 63 | + |
| 64 | + expect(tree.readContent('src/multi.spec.ts')).toBe(after); |
| 65 | +}); |
| 66 | + |
| 67 | +test('does not modify imports from other packages', async () => { |
| 68 | + const before = ` |
| 69 | +import { render } from '@testing-library/angular'; |
| 70 | +import { screen } from '@testing-library/dom'; |
| 71 | +import { Component } from '@angular/core'; |
| 72 | +`; |
| 73 | + |
| 74 | + const after = ` |
| 75 | +import { render } from '@testing-library/angular/zoneless'; |
| 76 | +import { screen } from '@testing-library/dom'; |
| 77 | +import { Component } from '@angular/core'; |
| 78 | +`; |
| 79 | + |
| 80 | + const tree = await setup({ |
| 81 | + 'src/other.spec.ts': before, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(tree.readContent('src/other.spec.ts')).toBe(after); |
| 85 | +}); |
| 86 | + |
| 87 | +test('handles files without @testing-library/angular imports', async () => { |
| 88 | + const content = ` |
| 89 | +import { Component } from '@angular/core'; |
| 90 | +
|
| 91 | +@Component({ |
| 92 | + selector: 'app-root', |
| 93 | + template: '<h1>Hello</h1>' |
| 94 | +}) |
| 95 | +export class AppComponent {} |
| 96 | +`; |
| 97 | + |
| 98 | + const tree = await setup({ |
| 99 | + 'src/regular.ts': content, |
| 100 | + }); |
| 101 | + |
| 102 | + expect(tree.readContent('src/regular.ts')).toBe(content); |
| 103 | +}); |
| 104 | + |
| 105 | +test('migrates multiple files', async () => { |
| 106 | + const tree = await setup({ |
| 107 | + 'src/file1.spec.ts': `import { render } from '@testing-library/angular';`, |
| 108 | + 'src/file2.spec.ts': `import { screen } from '@testing-library/angular';`, |
| 109 | + 'src/file3.spec.ts': `import { fireEvent } from '@testing-library/angular';`, |
| 110 | + }); |
| 111 | + |
| 112 | + expect(tree.readContent('src/file1.spec.ts')).toBe(`import { render } from '@testing-library/angular/zoneless';`); |
| 113 | + expect(tree.readContent('src/file2.spec.ts')).toBe(`import { screen } from '@testing-library/angular/zoneless';`); |
| 114 | + expect(tree.readContent('src/file3.spec.ts')).toBe(`import { fireEvent } from '@testing-library/angular/zoneless';`); |
| 115 | +}); |
| 116 | + |
| 117 | +async function setup(files: Record<string, string>) { |
| 118 | + const collectionPath = path.join(__dirname, '../../../../dist/@testing-library/angular/schematics/collection.json'); |
| 119 | + const schematicRunner = new SchematicTestRunner('schematics', collectionPath); |
| 120 | + |
| 121 | + const tree = new UnitTestTree(new EmptyTree()); |
| 122 | + |
| 123 | + for (const [filePath, content] of Object.entries(files)) { |
| 124 | + tree.create(filePath, content); |
| 125 | + } |
| 126 | + |
| 127 | + await schematicRunner.runSchematic('migrate-to-zoneless', {}, tree); |
| 128 | + |
| 129 | + return tree; |
| 130 | +} |
0 commit comments