Skip to content

Commit

Permalink
test: add tests for dapp add command (#2143)
Browse files Browse the repository at this point in the history
  • Loading branch information
realdreamer committed May 27, 2024
1 parent 5a9d2de commit fb8227d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
6 changes: 2 additions & 4 deletions packages/tools/kadena-cli/src/dapp/commands/dappCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ export const createDappCommand: (program: Command, version: string) => void =
[dappOptions.dappTemplate()],
async (option, { values }) => {
const config = await option.dappTemplate();
log.debug('dapp-create-command', config);
log.debug('dapp-add-command', config);

if (values[0] === undefined) {
throw new CommandError({
errors: [
'Project name is required, e.g. `kadena dapp create my-dapp`',
],
errors: ['Project name is required, e.g. `kadena dapp add my-dapp`'],
exitCode: 1,
});
}
Expand Down
79 changes: 79 additions & 0 deletions packages/tools/kadena-cli/src/dapp/tests/dappCreate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { spawnSync } from 'child_process';
import type { MockInstance } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { services } from '../../services/index.js';
import { mockPrompts, runCommand } from '../../utils/test.util.js';

// Mock the entire child_process module
// Using spyOn for spawnSync will not work because it is a read-only function from child_process
vi.mock('child_process', () => {
return {
spawnSync: vi.fn(),
};
});

describe('dapp add command', () => {
beforeEach(() => {
(spawnSync as unknown as MockInstance).mockReset();
});

it('should delete account alias file', async () => {
const mockOutput = 'success';
(spawnSync as unknown as MockInstance).mockImplementation(() => ({
pid: 12345,
output: [Buffer.from(''), Buffer.from(mockOutput), Buffer.from('')],
stdout: Buffer.from(mockOutput),
stderr: Buffer.from(''),
status: 0,
signal: null,
error: undefined,
}));
mockPrompts({
select: {
'What template do you want to use?': 'vuejs',
},
verbose: true,
});

await runCommand('dapp add my-first-app');

expect(spawnSync).toHaveBeenCalledWith(
'npx',
[
'@kadena/create-kadena-app',
'generate-project',
'-n',
'my-first-app',
'-t',
'vuejs',
],
{ stdio: 'inherit' },
);
});

it('should throw an error when user runs the command without project directory', async () => {
mockPrompts({
select: {
'What template do you want to use?': 'vuejs',
},
verbose: true,
});
const { stderr } = await runCommand('dapp add');
expect(stderr).toContain(
'Project name is required, e.g. `kadena dapp add my-dapp`',
);
});

it('should thrown an error when user passes the existing project directory', async () => {
await services.filesystem.ensureDirectoryExists('test-my-first-app');
const { stderr } = await runCommand(
'dapp add test-my-first-app --dapp-template=nextjs',
);
expect(stderr).toContain(
'Project directory test-my-first-app already exists',
);

// clean up the created directory
await services.filesystem.deleteDirectory('test-my-first-app');
});
});

0 comments on commit fb8227d

Please sign in to comment.