Skip to content

Commit

Permalink
test: move confirm() and prompt() into an object for easier mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Dec 17, 2021
1 parent c909cc8 commit 3c0b7eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
10 changes: 8 additions & 2 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) {
);
}

export function confirm(text: string): Promise<boolean> {
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) {
);
}

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

// }

// Export as an object so that it is easier to mock for testing
export const dialogs = {
confirm,
prompt,
};
16 changes: 11 additions & 5 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 { confirm, prompt } from "./dialogs";
import { dialogs } from "./dialogs";
import { version as wranglerVersion } from "../package.json";
import {
login,
Expand Down Expand Up @@ -214,7 +214,9 @@ export async function main(argv: string[]): Promise<void> {

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

// -- snip, end --

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

// -- snip, end --

if (await confirm("Are you sure you want to delete this secret?")) {
if (
await dialogs.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 3c0b7eb

Please sign in to comment.