Skip to content

Commit

Permalink
fix(angular): update getComponentsInfo to filter null values (#18431)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhwhite committed Aug 2, 2023
1 parent 510e03d commit f02d230
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,31 @@ export const Heading: Story = {
};
"
`;
exports[`angularStories generator: applications should ignore a path when using a routing module 1`] = `
"import type { Meta, StoryObj } from '@storybook/angular';
import { ComponentComponent } from './component.component';
import { within } from '@storybook/testing-library';
import { expect } from '@storybook/jest';
const meta: Meta<ComponentComponent> = {
component: ComponentComponent,
title: 'ComponentComponent',
};
export default meta;
type Story = StoryObj<ComponentComponent>;
export const Primary: Story = {
args: {},
};
export const Heading: Story = {
args: {},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
expect(canvas.getByText(/component works!/gi)).toBeTruthy();
},
};
"
`;
48 changes: 48 additions & 0 deletions packages/angular/src/generators/stories/stories-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { componentGenerator } from '../component/component';
import { scamGenerator } from '../scam/scam';
import { generateTestApplication } from '../utils/testing';
import { angularStoriesGenerator } from './stories';
import { stripIndents } from '@nx/devkit';

// need to mock cypress otherwise it'll use the nx installed version from package.json
// which is v9 while we are testing for the new v10 version
Expand Down Expand Up @@ -104,6 +105,53 @@ describe('angularStories generator: applications', () => {
).toBeFalsy();
});

it('should ignore a path when using a routing module', async () => {
tree.write(
`apps/${appName}/src/app/component/component.module.ts`,
stripIndents`
import { NgModule } from '@angular/core';
@NgModule({})
export class ComponentModule {}
`
);
tree.write(
`apps/${appName}/src/app/component/component-routing.module.ts`,
stripIndents`
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class ComponentRoutingModule {}
`
);
await componentGenerator(tree, {
name: 'component/component',
project: appName,
flat: true,
});

await angularStoriesGenerator(tree, {
name: appName,
ignorePaths: [`apps/${appName}/src/app/app.component.ts`],
});

expect(
tree.read(
`apps/${appName}/src/app/component/component.component.stories.ts`,
'utf-8'
)
).toMatchSnapshot();
expect(
tree.exists(`apps/${appName}/src/app/app.component.stories.ts`)
).toBeFalsy();
});

it('should generate stories file for inline scam component', async () => {
await scamGenerator(tree, {
name: 'my-scam',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,39 @@ export function getComponentsInfo(
moduleFilePaths: string[],
projectName: string
): ComponentInfo[] {
return moduleFilePaths.flatMap((moduleFilePath) => {
const file = getTsSourceFile(tree, moduleFilePath);
const declaredComponents = getModuleDeclaredComponents(
file,
moduleFilePath,
projectName
);
if (declaredComponents.length === 0) {
return undefined;
}

if (!tsModule) {
tsModule = ensureTypescript();
}
const imports = file.statements.filter(
(statement) => statement.kind === tsModule.SyntaxKind.ImportDeclaration
);

const componentsInfo = declaredComponents.map((componentName) =>
getComponentInfo(
tree,
entryPoint,
return moduleFilePaths
.flatMap((moduleFilePath) => {
const file = getTsSourceFile(tree, moduleFilePath);
const declaredComponents = getModuleDeclaredComponents(
file,
imports,
moduleFilePath,
componentName
)
);
projectName
);
if (declaredComponents.length === 0) {
return undefined;
}

return componentsInfo;
});
if (!tsModule) {
tsModule = ensureTypescript();
}
const imports = file.statements.filter(
(statement) => statement.kind === tsModule.SyntaxKind.ImportDeclaration
);

const componentsInfo = declaredComponents.map((componentName) =>
getComponentInfo(
tree,
entryPoint,
file,
imports,
moduleFilePath,
componentName
)
);

return componentsInfo;
})
.filter((f) => f !== undefined);
}

export function getStandaloneComponentsInfo(
Expand Down

1 comment on commit f02d230

@vercel
Copy link

@vercel vercel bot commented on f02d230 Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.