-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add test for confirm to process
- Loading branch information
Showing
9 changed files
with
150 additions
and
72 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
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
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
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
import { promptParam } from './prompt'; | ||
import { CLIProcess } from '../processing/types'; | ||
import { RcJson } from '../../types'; | ||
|
||
type Precondition = (context: RcJson) => Promise<boolean> | boolean; | ||
|
||
type ConfirmProcess = { | ||
prompt: string, | ||
process: CLIProcess, | ||
denied?: CLIProcess | ||
precondition?: Precondition, | ||
} | ||
|
||
const confirmedPrompt = async (prompt: string): Promise<boolean> => { | ||
return await promptParam({ | ||
type: 'confirm', | ||
message: prompt, | ||
initial: true, | ||
}); | ||
} | ||
|
||
|
||
const isPreconditionMet = async (context: RcJson, precondition?: Precondition): Promise<boolean> => { | ||
return precondition === undefined ? true : precondition(context); | ||
} | ||
|
||
export function confirmToProcess({ prompt, process, precondition }: ConfirmProcess): CLIProcess { | ||
return async (context: RcJson): Promise<RcJson> => { | ||
|
||
const shouldPrompt = await isPreconditionMet(context, precondition); | ||
if (!shouldPrompt) { | ||
return context; | ||
} | ||
|
||
const shouldProcess = await confirmedPrompt(prompt); | ||
if (!shouldProcess) { | ||
return context; | ||
} | ||
|
||
return process(context); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
packages/cli/src/lib/core/prompt/confirm-to-process.unit.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { confirmToProcess } from './confirm-to-process'; | ||
import { RcJson } from '../../types'; | ||
import prompt = require('./prompt'); | ||
|
||
jest.mock('./prompt'); | ||
|
||
describe('confirmToProcess', () => { | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}) | ||
|
||
it('should check if precondition is met if one is given', async () => { | ||
const mockPrecondition = jest.fn(); | ||
await confirmToProcess({ | ||
prompt: 'Confirm should process?', | ||
process: jest.fn(), | ||
precondition: mockPrecondition, | ||
})({} as RcJson); | ||
expect(mockPrecondition).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not prompt if precondition is not met', async () => { | ||
const promptParamSpy = jest.spyOn(prompt, 'promptParam'); | ||
await confirmToProcess({ | ||
prompt: 'Confirm should process?', | ||
process: jest.fn(), | ||
precondition: () => false, | ||
})({} as RcJson); | ||
expect(promptParamSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should prompt if precondition is met', async () => { | ||
const promptParamSpy = jest.spyOn(prompt, 'promptParam'); | ||
await confirmToProcess({ | ||
prompt: 'Confirm should process?', | ||
process: jest.fn(), | ||
precondition: () => true, | ||
})({} as RcJson); | ||
expect(promptParamSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should prompt if no precondition is passed', async () => { | ||
const promptParamSpy = jest.spyOn(prompt, 'promptParam'); | ||
await confirmToProcess({ | ||
prompt: 'Confirm should process?', | ||
process: jest.fn(), | ||
})({} as RcJson); | ||
expect(promptParamSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not process if prompt is denied', async () => { | ||
jest.spyOn(prompt, 'promptParam').mockResolvedValue(false); | ||
const processSpy = jest.fn(); | ||
await confirmToProcess({ | ||
prompt: 'Confirm should process?', | ||
process: processSpy, | ||
})({} as RcJson); | ||
expect(processSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should process if prompt is accepted', async () => { | ||
jest.spyOn(prompt, 'promptParam').mockResolvedValue(true); | ||
const processSpy = jest.fn(); | ||
await confirmToProcess({ | ||
prompt: 'Confirm should process?', | ||
process: processSpy, | ||
})({} as RcJson); | ||
expect(processSpy).toHaveBeenCalled(); | ||
}); | ||
}) |
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,16 @@ | ||
import { prompt } from 'enquirer'; | ||
|
||
export async function promptParam<T>(cfg: {initial?: T, skip?: boolean, message: string, type?: any, [key: string]: any}): Promise<T> { | ||
const { type, initial, message, skip, result } = cfg; | ||
|
||
const { param } = await prompt<{ param: T }>([{ | ||
name: 'param', | ||
type: type || 'input', | ||
initial, | ||
message, | ||
skip, | ||
result | ||
}]); | ||
|
||
return param; | ||
} |