Skip to content
GitHub

createModalFields

function in meocord/testing Since 4.1.0

createModalFields(
  values: Record<string, string | string[] | Attachment[]>,
): ModalSubmitFields

Builds the fields of a submitted form, as discord.js does when a user submits one.

Use it for a mock ModalSubmitInteraction: discord.js keeps the ModalSubmitFields constructor private, so a test cannot build one directly.

Parameters

NameTypeDescription
valuesRecord< string, string | string[] | Attachment[] >

Each field's value, keyed by its customId: a string for a text input, an array of strings for a select's chosen values, or an array of Attachments for a file upload. An empty array is a select with nothing chosen.

Returns

ModalSubmitFields

Fields that getTextInputValue, getStringSelectValues, getUploadedFiles and a handler's params all read.

Examples

TypeScript
import { expect } from 'vitest'

@Controller()
class FeedbackController {
  @Command('feedback/{topic}', CommandType.MODAL_SUBMIT)
  async submit(interaction: ModalSubmitInteraction, { topic, body }: { topic: string; body: string }) {
    await respond(interaction).send(`Thanks: ${topic}, ${body}`)
  }
}
const module = MeoCordTestingModule.create({ controllers: [FeedbackController] }).compile()
const fields = createModalFields({ body: 'It crashed' })
const interaction = createMockInteraction(ModalSubmitInteraction, { customId: 'feedback/bugs', fields })
await module.invoke(FeedbackController, 'submit', interaction)
expect(getResponse(interaction).calls[0].payload).toMatchObject({ content: 'Thanks: bugs, It crashed' })