Skip to content

Commit

Permalink
chore(testing dbAuth): Add "yes" prompt testing for WebAuthn (redwood…
Browse files Browse the repository at this point in the history
…js#10823)

Make sure to test prompt answer "yes" and "no" for the WebAuthn prompt.
This required pretty involved mocking of Listr2 that I hope we can reuse
for other tests as well in the future
  • Loading branch information
Tobbe authored Jun 15, 2024
1 parent 081c492 commit 8b40ed2
Showing 1 changed file with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import { getPaths } from '../../../../lib'
import * as dbAuth from '../dbAuth'

vi.mock('listr2', async () => {
const listrImpl = (tasks) => {
const ctx = {}
const listrImpl = (tasks, listrOptions) => {
return {
ctx,
run: async () => {
mockExecutedTaskTitles = []
mockSkippedTaskTitles = []
Expand All @@ -43,12 +45,28 @@ vi.mock('listr2', async () => {
const augmentedTask = {
...task,
newListr: listrImpl,
prompt: () => {},
prompt: async (options) => {
const enquirer = listrOptions?.injectWrapper?.enquirer

if (enquirer) {
if (!Array.isArray(options)) {
options = [{ ...options, name: 'default' }]
} else if (options.length === 1) {
options[0].name = 'default'
}

const response = await enquirer.prompt(options)

if (options.length === 1) {
return response.default
}
}
},
skip: (msg) => {
mockSkippedTaskTitles.push(msg || task.title)
},
}
await task.task({}, augmentedTask)
await task.task(ctx, augmentedTask)

// storing the title after running the task in case the task
// modifies its own title
Expand Down Expand Up @@ -124,10 +142,38 @@ beforeEach(() => {
})

describe('dbAuth handler WebAuthn task title', () => {
it('is correct after prompting', async () => {
it('is correct after prompt answer "Yes"', async () => {
const customEnquirer = new Enquirer({ show: false })
customEnquirer.on('prompt', (prompt) => {
prompt.submit()
if (prompt.state.message.includes('Enable WebAuthn')) {
prompt.on('run', () => {
return prompt.keypress('y')
})
} else {
prompt.submit()
}
})

await dbAuth.handler({
enquirer: customEnquirer,
listr2: { silentRendererCondition: true },
})

expect(mockExecutedTaskTitles[1]).toEqual(
'Querying WebAuthn addition: WebAuthn addition included',
)
})

it('is correct after prompt answer "No"', async () => {
const customEnquirer = new Enquirer({ show: false })
customEnquirer.on('prompt', (prompt) => {
if (prompt.state.message.includes('Enable WebAuthn')) {
prompt.on('run', () => {
return prompt.keypress('N')
})
} else {
prompt.submit()
}
})

await dbAuth.handler({
Expand Down

0 comments on commit 8b40ed2

Please sign in to comment.