nx/packages/next/src/generators/component/component.spec.ts
Leosvel Pérez Espinosa 28c53f942b
feat(misc): handle artifact generators' path options including file extensions (#29111)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

Artifact generators don't handle consistently receiving a file extension
in the `path` option.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Artifact generators should handle receiving a file extension in the
`path` option. If the file extension is passed, the file path will be
treated as "complete" and used fully as provided. If the `path` provided
doesn't contain a file extension, the default extension will be appended
to it (or the one provided in a related option, e.g. `--language`,
`--js`, etc) together with the suffix for generators that use it.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-12-09 09:13:15 -05:00

109 lines
3.5 KiB
TypeScript

import { applicationGenerator } from '../application/application';
import { componentGenerator } from './component';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Tree } from '@nx/devkit';
import { libraryGenerator } from '@nx/react';
import { Linter } from '@nx/eslint';
describe('component', () => {
let tree: Tree;
const appName = 'my-app';
const libName = 'my-lib';
beforeEach(async () => {
tree = createTreeWithEmptyWorkspace();
await applicationGenerator(tree, {
directory: appName,
style: 'css',
});
await libraryGenerator(tree, {
directory: libName,
linter: Linter.EsLint,
style: 'css',
skipFormat: true,
skipTsConfig: false,
unitTestRunner: 'jest',
});
});
it('should generate component in components directory for application', async () => {
await componentGenerator(tree, {
name: 'hello',
path: `${appName}/components/hello/hello`,
style: 'css',
});
expect(tree.exists('my-app/components/hello/hello.tsx')).toBeTruthy();
expect(tree.exists('my-app/components/hello/hello.spec.tsx')).toBeTruthy();
expect(
tree.exists('my-app/components/hello/hello.module.css')
).toBeTruthy();
});
it('should handle path with file extension', async () => {
await componentGenerator(tree, {
path: `${appName}/components/hello/hello.tsx`,
style: 'css',
});
expect(tree.exists('my-app/components/hello/hello.tsx')).toBeTruthy();
expect(tree.exists('my-app/components/hello/hello.spec.tsx')).toBeTruthy();
expect(
tree.exists('my-app/components/hello/hello.module.css')
).toBeTruthy();
});
it('should generate component in default directory for library', async () => {
await componentGenerator(tree, {
name: 'hello',
path: `${libName}/src/lib/hello/hello`,
style: 'css',
});
expect(tree.exists('my-lib/src/lib/hello/hello.tsx')).toBeTruthy();
expect(tree.exists('my-lib/src/lib/hello/hello.spec.tsx')).toBeTruthy();
expect(tree.exists('my-lib/src/lib/hello/hello.module.css')).toBeTruthy();
});
it('should allow directory override', async () => {
await componentGenerator(tree, {
name: 'hello',
path: `${appName}/foo/hello/hello`,
style: 'css',
});
await componentGenerator(tree, {
name: 'world',
path: `${libName}/src/bar/world/world`,
style: 'css',
});
expect(tree.exists('my-app/foo/hello/hello.tsx')).toBeTruthy();
expect(tree.exists('my-app/foo/hello/hello.spec.tsx')).toBeTruthy();
expect(tree.exists('my-app/foo/hello/hello.module.css')).toBeTruthy();
expect(tree.exists('my-lib/src/bar/world/world.tsx')).toBeTruthy();
expect(tree.exists('my-lib/src/bar/world/world.spec.tsx')).toBeTruthy();
expect(tree.exists('my-lib/src/bar/world/world.module.css')).toBeTruthy();
});
it('should work with path as-provided', async () => {
await componentGenerator(tree, {
name: 'hello',
path: 'my-lib/src/foo/hello',
style: 'css',
});
expect(tree.exists('my-lib/src/foo/hello.tsx')).toBeTruthy();
expect(tree.exists('my-lib/src/foo/hello.spec.tsx')).toBeTruthy();
expect(tree.exists('my-lib/src/foo/hello.module.css')).toBeTruthy();
});
it('should work with path as a part of the component name', async () => {
await componentGenerator(tree, {
path: `${libName}/src/btn/btn`,
style: 'css',
});
expect(tree.exists(`${libName}/src/btn/btn.tsx`)).toBeTruthy();
});
});