Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for prompt display based on flag input during init #672

Merged
merged 3 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
73 changes: 42 additions & 31 deletions packages/cli/src/commands/hydrogen/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,69 +25,80 @@ describe('init', () => {
...stubs,
});

describe('installDeps', () => {
it('prompts the user to install dependencies when installDeps is not passed', async () => {
describe.each([
{flag: 'template', value: 'hello-world'},
{flag: 'installDeps', value: true},
{flag: 'language', value: 'ts'},
{flag: 'path', value: './my-app'},
])('flag $flag', ({flag, value}) => {
it(`does not prompt the user for ${flag} when a value is passed in options`, async () => {
await temporaryDirectoryTask(async (tmpDir) => {
// Given
const options = defaultOptions({path: tmpDir});

vi.mocked(ui.prompt).mockImplementation(() =>
Promise.resolve({installDeps: 'false'}),
);
const options = defaultOptions({
path: tmpDir,
[flag as string]: value,
});

// When
await runInit(options);

// Then
expect(ui.prompt).toHaveBeenCalledWith(
expect(ui.prompt).not.toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
name: 'installDeps',
name: flag,
}),
]),
);
expect(installNodeModules).not.toHaveBeenCalled();
});
});

it('does not prompt the user to install dependencies when installDeps is true', async () => {
it(`prompts the user for ${flag} when no value is passed in options`, async () => {
await temporaryDirectoryTask(async (tmpDir) => {
// Given
const options = defaultOptions({installDeps: true, path: tmpDir});
const options = defaultOptions({
path: tmpDir,
[flag as string]: undefined,
});

// When
await runInit(options);

// Then
expect(ui.prompt).not.toHaveBeenCalledWith(
expect(ui.prompt).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
name: 'installDeps',
name: flag,
}),
]),
);
expect(installNodeModules).toHaveBeenCalled();
});
});
});

it('does not show a prompt to install dependencies when installDeps is false', async () => {
await temporaryDirectoryTask(async (tmpDir) => {
// Given
const options = defaultOptions({installDeps: false, path: tmpDir});
it('installs dependencies when installDeps is true', async () => {
await temporaryDirectoryTask(async (tmpDir) => {
// Given
const options = defaultOptions({installDeps: true, path: tmpDir});

// When
await runInit(options);
// When
await runInit(options);

// Then
expect(ui.prompt).not.toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
name: 'installDeps',
}),
]),
);
expect(installNodeModules).not.toHaveBeenCalled();
});
// Then
expect(installNodeModules).toHaveBeenCalled();
});
});

it('does not install dependencies when installDeps is false', async () => {
await temporaryDirectoryTask(async (tmpDir) => {
// Given
const options = defaultOptions({installDeps: false, path: tmpDir});

// When
await runInit(options);

// Then
expect(installNodeModules).not.toHaveBeenCalled();
});
});
});
2 changes: 1 addition & 1 deletion packages/cli/src/commands/hydrogen/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class Init extends Command {
}),
'install-deps': Flags.boolean({
description: 'Auto install dependencies using the active package manager',
env: 'SHOPIFY_HYDROGEN_INSTALL_DEPS',
env: 'SHOPIFY_HYDROGEN_FLAG_INSTALL_DEPS',
allowNo: true,
}),
};
Expand Down