Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 46 additions & 0 deletions src/__tests__/lib/user-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
booleanInput,
displayNoneForEmpty,
integerInput,
numberInput,
optionalIntegerInput,
optionalNumberInput,
optionalStringInput,
Expand Down Expand Up @@ -129,6 +130,22 @@ describe('optionalStringInput', () => {
expect(generatedValidate('bad input')).toBe('please enter better input')
expect(validateMock).toHaveBeenCalledExactlyOnceWith('bad input')
})

it('allows empty even with custom validate function', async () => {
const validateMock = jest.fn<ValidateFunction<string>>().mockReturnValue(true)
inputMock.mockResolvedValueOnce('')

expect(await optionalStringInput('prompt message', { validate: validateMock })).toBe(undefined)

expect(inputMock).toHaveBeenCalledWith(expect.objectContaining({
message: 'prompt message',
validate: expect.any(Function),
}))

const generatedValidate = (inputMock.mock.calls[0][0] as { validate: ValidateFunction<string> }).validate
expect(generatedValidate('')).toBeTrue()
expect(validateMock).toHaveBeenCalledTimes(0)
})
})

describe('stringInput', () => {
Expand Down Expand Up @@ -428,6 +445,35 @@ describe('optionalNumberInput', () => {
})
})

describe('numberInput', () => {
it('requires input', async () => {
inputMock.mockResolvedValueOnce('43.9999')

expect(await numberInput('prompt message')).toBe(43.9999)

expect(inputMock).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ required: true }))
})

it('incorporates supplied validation', async () => {
inputMock.mockResolvedValueOnce('13.31')

const validateMock = jest.fn<ValidateFunction<number | undefined>>()

expect(await numberInput('prompt message', { validate: validateMock })).toBe(13.31)

expect(inputMock).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({
validate: expect.any(Function),
}))

const generatedValidate = (inputMock.mock.calls[0][0] as { validate: ValidateFunction<string> }).validate

validateMock.mockReturnValue(true)

expect(generatedValidate('77')).toBe(true)
expect(validateMock).toHaveBeenCalledExactlyOnceWith(77)
})
})

describe('booleanInput', () => {
it('defaults to true', async () => {
confirmMock.mockResolvedValueOnce(false)
Expand Down
Loading