From b1504c3bcca4d4c313e5d795ace8b074fb1f8890 Mon Sep 17 00:00:00 2001 From: cexbrayat Date: Thu, 31 Oct 2024 18:54:12 +0100 Subject: [PATCH] fix(@schematics/angular): component spec with export default The generated spec was using the same import when it should be adapted if `exportDefault` is used: ```ts import { UserComponent } from './user.component.ts` ``` It now produces: ```ts import UserComponent from './user.component.ts` ``` (cherry picked from commit 51fd98018ee4e0bfd4886e5b663d6ad70925e437) --- .../__name@dasherize__.__type@dasherize__.spec.ts.template | 2 +- packages/schematics/angular/component/index_spec.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.spec.ts.template b/packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.spec.ts.template index 1ce9bc7bc745..070ef601b634 100644 --- a/packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.spec.ts.template +++ b/packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.spec.ts.template @@ -1,6 +1,6 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { <%= classify(name) %><%= classify(type) %> } from './<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>'; +import <% if(!exportDefault) { %>{ <% }%><%= classify(name) %><%= classify(type) %> <% if(!exportDefault) {%>} <% }%>from './<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>'; describe('<%= classify(name) %><%= classify(type) %>', () => { let component: <%= classify(name) %><%= classify(type) %>; diff --git a/packages/schematics/angular/component/index_spec.ts b/packages/schematics/angular/component/index_spec.ts index 16352ac96482..7cfa8461d4f0 100644 --- a/packages/schematics/angular/component/index_spec.ts +++ b/packages/schematics/angular/component/index_spec.ts @@ -511,6 +511,9 @@ describe('Component Schematic', () => { const tree = await schematicRunner.runSchematic('component', options, appTree); const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts'); expect(tsContent).toContain('export default class FooComponent'); + + const specContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts'); + expect(specContent).toContain("import FooComponent from './foo.component';"); }); it('should export the component as a named export when exportDefault is false', async () => { @@ -519,5 +522,8 @@ describe('Component Schematic', () => { const tree = await schematicRunner.runSchematic('component', options, appTree); const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts'); expect(tsContent).toContain('export class FooComponent'); + + const specContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts'); + expect(specContent).toContain("import { FooComponent } from './foo.component';"); }); });