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

export functions from dialogs.tsx, pass tests #125

Merged
merged 3 commits into from
Dec 17, 2021
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
5 changes: 5 additions & 0 deletions .changeset/purple-rice-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Export regular functions from dialog.ts, pass tests (followup from https://github.com/cloudflare/wrangler2/pull/124)
50 changes: 30 additions & 20 deletions packages/wrangler/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,39 @@ import * as TOML from "@iarna/toml";
import { main } from "../index";
import { setMock, unsetAllMocks } from "./mock-cfetch";
import { existsSync } from "node:fs";
import { dialogs } from "../dialogs";
import { confirm } from "../dialogs";

jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch"));

jest.mock("../dialogs", () => {
return {
// @ts-expect-error typescript doesn't know that jest.requireActual
// returns the 'object' form of the dialogs module
...jest.requireActual("../dialogs"),
confirm: jest.fn().mockName("confirmMock"),
};
});

/**
* Mock the implementation of `confirm()` that will respond with configured results
* for configured confirmation text messages.
*
* If there is a call to `confirm()` that does not match any of the expectations
* then an error is thrown.
*/
function mockConfirm(...expectations: { text: string; result: boolean }[]) {
// @ts-expect-error - we're mocking the implementation of a function
// but typescript doesn't know we've replaced confirm up there
confirm.mockImplementationOnce((text: string) => {
for (const { text: expectedText, result } of expectations) {
if (text === expectedText) {
return result;
}
}
throw new Error(`Unexpected confirmation message: ${text}`);
});
}

async function w(cmd?: string) {
const logSpy = jest.spyOn(console, "log").mockImplementation();
const errorSpy = jest.spyOn(console, "error").mockImplementation();
Expand Down Expand Up @@ -222,22 +251,3 @@ describe("wrangler", () => {
});
});
});

/**
* Create a mock version of `confirm()` that will respond with configured results
* for configured confirmation text messages.
*
* If there is a call to `confirm()` that does not match any of the expectations
* then an error is thrown.
*/
function mockConfirm(...expectations: { text: string; result: boolean }[]) {
const mockImplementation = async (text: string) => {
for (const { text: expectedText, result } of expectations) {
if (text === expectedText) {
return result;
}
}
throw new Error(`Unexpected confirmation message: ${text}`);
};
return jest.spyOn(dialogs, "confirm").mockImplementation(mockImplementation);
}
18 changes: 2 additions & 16 deletions packages/wrangler/src/dialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function Confirm(props: ConfirmProps) {
);
}

function confirm(text: string): Promise<boolean> {
export function confirm(text: string): Promise<boolean> {
return new Promise((resolve) => {
const { unmount } = render(
<Confirm
Expand Down Expand Up @@ -61,7 +61,7 @@ function Prompt(props: PromptProps) {
);
}

async function prompt(text: string, type: "text" | "password" = "text") {
export async function prompt(text: string, type: "text" | "password" = "text") {
return new Promise((resolve) => {
const { unmount } = render(
<Prompt
Expand All @@ -75,17 +75,3 @@ async function prompt(text: string, type: "text" | "password" = "text") {
);
});
}

// this one feels a little non "standard"
// export async function select(
// title: string,
// choices: { label: string; value: string }[]
// ): Promise<{ label: string; value: string }>{

// }

// Export as an object so that it is easier to mock for testing
export const dialogs = {
confirm,
prompt,
};
18 changes: 6 additions & 12 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type yargs from "yargs";
import { findUp } from "find-up";
import TOML from "@iarna/toml";
import type { Config } from "./config";
import { dialogs } from "./dialogs";
import { confirm, prompt } from "./dialogs";
import { version as wranglerVersion } from "../package.json";
import {
login,
Expand Down Expand Up @@ -202,7 +202,7 @@ export async function main(argv: string[]): Promise<void> {
const destination = path.join(process.cwd(), "wrangler.toml");
if (fs.existsSync(destination)) {
console.error(`${destination} file already exists!`);
const result = await dialogs.confirm(
const result = await confirm(
"Do you want to continue initializing this project?"
);
if (!result) {
Expand All @@ -229,9 +229,7 @@ export async function main(argv: string[]): Promise<void> {

if (!pathToPackageJson) {
if (
await dialogs.confirm(
"No package.json found. Would you like to create one?"
)
await confirm("No package.json found. Would you like to create one?")
) {
await writeFile(
path.join(process.cwd(), "package.json"),
Expand All @@ -256,7 +254,7 @@ export async function main(argv: string[]): Promise<void> {
// and make a tsconfig?
let pathToTSConfig = await findUp("tsconfig.json");
if (!pathToTSConfig) {
if (await dialogs.confirm("Would you like to use typescript?")) {
if (await confirm("Would you like to use typescript?")) {
await writeFile(
path.join(process.cwd(), "tsconfig.json"),
JSON.stringify(
Expand Down Expand Up @@ -915,7 +913,7 @@ export async function main(argv: string[]): Promise<void> {

// -- snip, end --

const secretValue = await dialogs.prompt(
const secretValue = await prompt(
"Enter a secret value:",
"password"
);
Expand Down Expand Up @@ -1015,11 +1013,7 @@ export async function main(argv: string[]): Promise<void> {

// -- snip, end --

if (
await dialogs.confirm(
"Are you sure you want to delete this secret?"
)
) {
if (await confirm("Are you sure you want to delete this secret?")) {
console.log(
`Deleting the secret ${args.key} on script ${scriptName}.`
);
Expand Down