|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +const mockExecute = vi.fn().mockResolvedValue(undefined); |
| 4 | +vi.mock('../../lib/schematics/index.js', async () => { |
| 5 | + const original = await vi.importActual('../../lib/schematics/index.js'); |
| 6 | + return { |
| 7 | + ...original, |
| 8 | + CollectionFactory: { |
| 9 | + create: () => ({ |
| 10 | + execute: mockExecute, |
| 11 | + }), |
| 12 | + }, |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +vi.mock('../../lib/utils/load-configuration.js', () => ({ |
| 17 | + loadConfiguration: vi.fn().mockResolvedValue({ |
| 18 | + language: 'ts', |
| 19 | + sourceRoot: 'src', |
| 20 | + collection: '@nestjs/schematics', |
| 21 | + entryFile: 'main', |
| 22 | + exec: 'node', |
| 23 | + projects: {}, |
| 24 | + monorepo: false, |
| 25 | + compilerOptions: {}, |
| 26 | + generateOptions: {}, |
| 27 | + }), |
| 28 | +})); |
| 29 | + |
| 30 | +import { GenerateAction } from '../../actions/generate.action.js'; |
| 31 | +import { GenerateCommandContext } from '../../commands/context/generate.context.js'; |
| 32 | +import { SchematicOption } from '../../lib/schematics/index.js'; |
| 33 | + |
| 34 | +describe('GenerateAction', () => { |
| 35 | + let action: GenerateAction; |
| 36 | + |
| 37 | + const baseContext = ( |
| 38 | + overrides: Partial<GenerateCommandContext> = {}, |
| 39 | + ): GenerateCommandContext => ({ |
| 40 | + schematic: 'resource', |
| 41 | + name: 'users', |
| 42 | + path: undefined, |
| 43 | + dryRun: false, |
| 44 | + flat: false, |
| 45 | + spec: { value: true, passedAsInput: false }, |
| 46 | + specFileSuffix: undefined, |
| 47 | + collection: undefined, |
| 48 | + project: undefined, |
| 49 | + skipImport: false, |
| 50 | + format: false, |
| 51 | + ...overrides, |
| 52 | + }); |
| 53 | + |
| 54 | + const findOption = ( |
| 55 | + schematicOptions: SchematicOption[], |
| 56 | + name: string, |
| 57 | + ): SchematicOption | undefined => |
| 58 | + schematicOptions.find((opt) => |
| 59 | + opt.toCommandString().startsWith(`--${name}=`) || |
| 60 | + opt.toCommandString() === `--${name}`, |
| 61 | + ); |
| 62 | + |
| 63 | + beforeEach(() => { |
| 64 | + mockExecute.mockClear(); |
| 65 | + action = new GenerateAction(); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('--type option', () => { |
| 69 | + it('should forward --type to the schematic when provided', async () => { |
| 70 | + await action.handle(baseContext({ type: 'rest' })); |
| 71 | + |
| 72 | + expect(mockExecute).toHaveBeenCalledTimes(1); |
| 73 | + const [, schematicOptions] = mockExecute.mock.calls[0]; |
| 74 | + const typeOption = findOption(schematicOptions, 'type'); |
| 75 | + expect(typeOption).toBeDefined(); |
| 76 | + expect(typeOption!.toCommandString()).toBe('--type="rest"'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should not forward --type when undefined', async () => { |
| 80 | + await action.handle(baseContext({ type: undefined })); |
| 81 | + |
| 82 | + const [, schematicOptions] = mockExecute.mock.calls[0]; |
| 83 | + const typeOption = findOption(schematicOptions, 'type'); |
| 84 | + expect(typeOption).toBeUndefined(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('--crud option', () => { |
| 89 | + it('should forward --crud to the schematic when true', async () => { |
| 90 | + await action.handle(baseContext({ crud: true })); |
| 91 | + |
| 92 | + const [, schematicOptions] = mockExecute.mock.calls[0]; |
| 93 | + const crudOption = findOption(schematicOptions, 'crud'); |
| 94 | + expect(crudOption).toBeDefined(); |
| 95 | + expect(crudOption!.toCommandString()).toBe('--crud'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should not forward --crud when falsy', async () => { |
| 99 | + await action.handle(baseContext({ crud: false })); |
| 100 | + |
| 101 | + const [, schematicOptions] = mockExecute.mock.calls[0]; |
| 102 | + const crudOption = findOption(schematicOptions, 'crud'); |
| 103 | + expect(crudOption).toBeUndefined(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should not forward --crud when undefined', async () => { |
| 107 | + await action.handle(baseContext({ crud: undefined })); |
| 108 | + |
| 109 | + const [, schematicOptions] = mockExecute.mock.calls[0]; |
| 110 | + const crudOption = findOption(schematicOptions, 'crud'); |
| 111 | + expect(crudOption).toBeUndefined(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should support --type and --crud together on the resource schematic', async () => { |
| 116 | + await action.handle( |
| 117 | + baseContext({ schematic: 'resource', type: 'rest', crud: true }), |
| 118 | + ); |
| 119 | + |
| 120 | + const [schematicName, schematicOptions] = mockExecute.mock.calls[0]; |
| 121 | + expect(schematicName).toBe('resource'); |
| 122 | + expect(findOption(schematicOptions, 'type')!.toCommandString()).toBe( |
| 123 | + '--type="rest"', |
| 124 | + ); |
| 125 | + expect(findOption(schematicOptions, 'crud')!.toCommandString()).toBe( |
| 126 | + '--crud', |
| 127 | + ); |
| 128 | + }); |
| 129 | +}); |
0 commit comments