-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: move the confirm mock code to its own file (#148)
* test: move the confirm mock code to its own file This commit moves the mocking code for the `confirm()` helper out of the main test file and into a helper file. It also adds an equivalent helper for `prompt()`. Apart from keeping the main test file simpler, it also will allow reuse in other test files in the future.
- Loading branch information
1 parent
3a23c5c
commit 03b8851
Showing
4 changed files
with
87 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,9 @@ | |
"sourcemap": true | ||
} | ||
] | ||
} | ||
}, | ||
"setupFilesAfterEnv": [ | ||
"<rootDir>/src/__tests__/jest.setup.ts" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { confirm, prompt } from "../dialogs"; | ||
|
||
jest.mock("../dialogs"); | ||
|
||
// By default (if not configured by mockConfirm()) calls to `confirm()` should throw. | ||
(confirm as jest.Mock).mockImplementation(() => { | ||
throw new Error( | ||
"Unexpected call to `confirm()`. You should use `mockConfirm()` to mock calls to `confirm()` with expectations. Search the codebase for `mockConfirm` to learn more." | ||
); | ||
}); | ||
|
||
// By default (if not configured by mockPrompt()) calls to `prompt()` should throw. | ||
(prompt as jest.Mock).mockImplementation(() => { | ||
throw new Error( | ||
"Unexpected call to `prompt()`. You should use `mockPrompt()` to mock calls to `prompt()` with expectations. Search the codebase for `mockPrompt` to learn more." | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { confirm, prompt } from "../dialogs"; | ||
|
||
/** | ||
* The expected values for a confirmation request. | ||
*/ | ||
export interface ConfirmExpectation { | ||
/** The text expected to be seen in the confirmation dialog. */ | ||
text: string; | ||
/** The mock response send back from the confirmation dialog. */ | ||
result: boolean; | ||
} | ||
|
||
/** | ||
* 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. | ||
*/ | ||
export function mockConfirm(...expectations: ConfirmExpectation[]) { | ||
(confirm as jest.Mock).mockImplementation((text: string) => { | ||
for (const { text: expectedText, result } of expectations) { | ||
if (text === expectedText) { | ||
return Promise.resolve(result); | ||
} | ||
} | ||
throw new Error(`Unexpected confirmation message: ${text}`); | ||
}); | ||
} | ||
|
||
/** | ||
* The expected values for a prompt request. | ||
*/ | ||
export interface PromptExpectation { | ||
/** The text expected to be seen in the prompt dialog. */ | ||
text: string; | ||
/** The type of the prompt. */ | ||
type: "text" | "password"; | ||
/** The mock response send back from the prompt dialog. */ | ||
result: string; | ||
} | ||
|
||
/** | ||
* Mock the implementation of `prompt()` that will respond with configured results | ||
* for configured prompt text messages. | ||
* | ||
* If there is a call to `prompt()` that does not match any of the expectations | ||
* then an error is thrown. | ||
*/ | ||
export function mockPrompt(...expectations: PromptExpectation[]) { | ||
(prompt as jest.Mock).mockImplementation( | ||
(text: string, type: "text" | "password") => { | ||
for (const { | ||
text: expectedText, | ||
type: expectedType, | ||
result, | ||
} of expectations) { | ||
if (text === expectedText && type == expectedType) { | ||
return Promise.resolve(result); | ||
} | ||
} | ||
throw new Error(`Unexpected confirmation message: ${text}`); | ||
} | ||
); | ||
} |