Skip to content

Commit

Permalink
fix(components/packages): check for existing waitForAsync import (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhwhite authored Jul 19, 2024
1 parent cadc111 commit ecaf9b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,28 @@ describe('Angular waitForAsync updates', () => {
`it('should create the app', waitForAsync(() => {`,
);
});

it('should update async imports when waitForAsync is already imported', async () => {
tree.create(
'/projects/my-app/src/app/test.component.spec.ts',
`import { async, ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { AppComponent } from './app.component';
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
}));`,
);
await runner.runSchematic('ng-wait-for-async-test', {}, tree);
const content = tree.readContent(
'/projects/my-app/src/app/test.component.spec.ts',
);
expect(content).toContain(
`import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';`,
);
expect(content).toContain(
`it('should create the app', waitForAsync(() => {`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,23 @@ export default function () {
importSpecifier.getStart(sourceFile),
importSpecifier.getWidth(sourceFile),
);
recorder.insertLeft(importSpecifier.getStart(sourceFile), 'waitForAsync');
if (!isImported(sourceFile, 'waitForAsync', '@angular/core/testing')) {
recorder.insertLeft(
importSpecifier.getStart(sourceFile),
'waitForAsync',
);
} else if (
content.charAt(
importSpecifier.getStart(sourceFile) +
importSpecifier.getWidth(sourceFile),
) === ','
) {
recorder.remove(
importSpecifier.getStart(sourceFile) +
importSpecifier.getWidth(sourceFile),
1,
);
}

const asyncCalls = findNodes(
sourceFile,
Expand Down

0 comments on commit ecaf9b8

Please sign in to comment.