Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions actions/generate.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ const mapContextToSchematicOptions = (
options.push(new SchematicOption('project', context.project));
if (context.skipImport !== undefined)
options.push(new SchematicOption('skipImport', context.skipImport));
if (context.type !== undefined)
options.push(new SchematicOption('type', context.type));
if (context.crud === true)
options.push(new SchematicOption('crud', true));
// 'schematic', 'spec', 'flat', 'specFileSuffix' are handled separately
return options;
};
2 changes: 2 additions & 0 deletions commands/context/generate.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export interface GenerateCommandContext {
project?: string;
skipImport: boolean;
format: boolean;
type?: string;
crud?: boolean;
}
7 changes: 7 additions & 0 deletions commands/generate.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export class GenerateCommand extends AbstractCommand {
'-c, --collection [collectionName]',
'Schematics collection to use.',
)
.option(
'--type [type]',
'Transport layer type (rest, graphql-code-first, graphql-schema-first, microservice, ws).',
)
.option('--crud', 'Generate CRUD entry points for a resource.')
.action(
async (
schematic: string,
Expand All @@ -71,6 +76,8 @@ export class GenerateCommand extends AbstractCommand {
project: options.project,
skipImport: options.skipImport,
format: options.format === true,
type: options.type,
crud: options.crud === true ? true : undefined,
};

await this.action.handle(context);
Expand Down
129 changes: 129 additions & 0 deletions test/actions/generate.action.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

const mockExecute = vi.fn().mockResolvedValue(undefined);
vi.mock('../../lib/schematics/index.js', async () => {
const original = await vi.importActual('../../lib/schematics/index.js');
return {
...original,
CollectionFactory: {
create: () => ({
execute: mockExecute,
}),
},
};
});

vi.mock('../../lib/utils/load-configuration.js', () => ({
loadConfiguration: vi.fn().mockResolvedValue({
language: 'ts',
sourceRoot: 'src',
collection: '@nestjs/schematics',
entryFile: 'main',
exec: 'node',
projects: {},
monorepo: false,
compilerOptions: {},
generateOptions: {},
}),
}));

import { GenerateAction } from '../../actions/generate.action.js';
import { GenerateCommandContext } from '../../commands/context/generate.context.js';
import { SchematicOption } from '../../lib/schematics/index.js';

describe('GenerateAction', () => {
let action: GenerateAction;

const baseContext = (
overrides: Partial<GenerateCommandContext> = {},
): GenerateCommandContext => ({
schematic: 'resource',
name: 'users',
path: undefined,
dryRun: false,
flat: false,
spec: { value: true, passedAsInput: false },
specFileSuffix: undefined,
collection: undefined,
project: undefined,
skipImport: false,
format: false,
...overrides,
});

const findOption = (
schematicOptions: SchematicOption[],
name: string,
): SchematicOption | undefined =>
schematicOptions.find((opt) =>
opt.toCommandString().startsWith(`--${name}=`) ||
opt.toCommandString() === `--${name}`,
);

beforeEach(() => {
mockExecute.mockClear();
action = new GenerateAction();
});

describe('--type option', () => {
it('should forward --type to the schematic when provided', async () => {
await action.handle(baseContext({ type: 'rest' }));

expect(mockExecute).toHaveBeenCalledTimes(1);
const [, schematicOptions] = mockExecute.mock.calls[0];
const typeOption = findOption(schematicOptions, 'type');
expect(typeOption).toBeDefined();
expect(typeOption!.toCommandString()).toBe('--type="rest"');
});

it('should not forward --type when undefined', async () => {
await action.handle(baseContext({ type: undefined }));

const [, schematicOptions] = mockExecute.mock.calls[0];
const typeOption = findOption(schematicOptions, 'type');
expect(typeOption).toBeUndefined();
});
});

describe('--crud option', () => {
it('should forward --crud to the schematic when true', async () => {
await action.handle(baseContext({ crud: true }));

const [, schematicOptions] = mockExecute.mock.calls[0];
const crudOption = findOption(schematicOptions, 'crud');
expect(crudOption).toBeDefined();
expect(crudOption!.toCommandString()).toBe('--crud');
});

it('should not forward --crud when falsy', async () => {
await action.handle(baseContext({ crud: false }));

const [, schematicOptions] = mockExecute.mock.calls[0];
const crudOption = findOption(schematicOptions, 'crud');
expect(crudOption).toBeUndefined();
});

it('should not forward --crud when undefined', async () => {
await action.handle(baseContext({ crud: undefined }));

const [, schematicOptions] = mockExecute.mock.calls[0];
const crudOption = findOption(schematicOptions, 'crud');
expect(crudOption).toBeUndefined();
});
});

it('should support --type and --crud together on the resource schematic', async () => {
await action.handle(
baseContext({ schematic: 'resource', type: 'rest', crud: true }),
);

const [schematicName, schematicOptions] = mockExecute.mock.calls[0];
expect(schematicName).toBe('resource');
expect(findOption(schematicOptions, 'type')!.toCommandString()).toBe(
'--type="rest"',
);
expect(findOption(schematicOptions, 'crud')!.toCommandString()).toBe(
'--crud',
);
});
});