Skip to content

Commit

Permalink
export functions from dialogs.tsx, pass tests (#125)
Browse files Browse the repository at this point in the history
* export functions from dialogs.tsx, pass tests

As mentioned in #124 (comment), it's possible to use regular exports from a module, but still be able to mock their implementations in jest. This PR does so, and passes tests.
  • Loading branch information
threepointone committed Dec 17, 2021
1 parent 23543fe commit 1216fc9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 48 deletions.
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 confirm()
// but typescript doesn't know we've previously replaced confirm with a mock
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

0 comments on commit 1216fc9

Please sign in to comment.