Skip to content

Commit

Permalink
add bouncer test
Browse files Browse the repository at this point in the history
  • Loading branch information
domin-mnd committed Nov 4, 2023
1 parent b641885 commit dcc0a0e
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions packages/reacord/scripts/discordjs-manual-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
Select,
useInstance,
} from "../library/main.js"
import type { TextChannel } from "discord.js"
import { ChannelType, Client, IntentsBitField } from "discord.js"
import type { CommandInteraction, TextChannel } from "discord.js"
import { ChannelType, Client, Events, IntentsBitField } from "discord.js"
import "dotenv/config"
import { kebabCase } from "lodash-es"
import { useState } from "react"
import { useEffect, useState } from "react"

const client = new Client({ intents: IntentsBitField.Flags.Guilds })
const reacord = new ReacordDiscordJs(client)
Expand Down Expand Up @@ -50,6 +50,29 @@ const createTest = async (
await block(channel)
}

const executables: Record<
string,
(interaction: CommandInteraction) => unknown
> = {}
const createInteractionTest = async (
name: string,
block: (interaction: CommandInteraction) => unknown,
) => {
const slashName = kebabCase(name)

await client.application?.commands.create({
name: slashName,
description: "Test command",
})
executables[slashName] = block
}

client.on(Events.InteractionCreate, (interaction) => {
if (interaction.isCommand()) {
executables[interaction.commandName]?.(interaction)
}
})

await createTest("basic", (channel) => {
reacord.createChannelMessage(channel).render("Hello, world!")
})
Expand Down Expand Up @@ -125,6 +148,36 @@ await createTest("counter", (channel) => {
reacord.createChannelMessage(channel).render(<Counter />)
})

await createInteractionTest("bounce counter", (interaction) => {
function Counter() {
const [count, setCount] = useState(0)

useEffect(() => {
// This will reply once the count is updated to track useEffect usage.
// Not creating instances.
void interaction.followUp(`Updated the count to ${count}`)
}, [count])

return (
<>
count: {count}
<Button
style="primary"
emoji="➕"
onClick={() => setCount(count + 1)}
/>
<Button
style="primary"
emoji="➖"
onClick={() => setCount(count - 1)}
/>
<Button label="reset" onClick={() => setCount(0)} />
</>
)
}
reacord.createInteractionReply(interaction).render(<Counter />)
})

await createTest("select", (channel) => {
function FruitSelect({ onConfirm }: { onConfirm: (choice: string) => void }) {
const [value, setValue] = useState<string>()
Expand Down

0 comments on commit dcc0a0e

Please sign in to comment.