start of embed + more test coverage

This commit is contained in:
MapleLeaf
2021-12-16 22:38:57 -06:00
parent 3c4e3ac422
commit c1d93b8468
7 changed files with 142 additions and 33 deletions

View File

@@ -1,8 +1,10 @@
/* eslint-disable unicorn/numeric-separators-style */
/* eslint-disable unicorn/no-null */
import type { ExecutionContext } from "ava"
import test from "ava"
import type { Message } from "discord.js"
import { Client, TextChannel } from "discord.js"
import { createRoot, Text } from "reacord"
import { createRoot, Embed, Text } from "reacord"
import { pick } from "reacord-helpers/pick.js"
import { raise } from "reacord-helpers/raise.js"
import React from "react"
@@ -38,35 +40,55 @@ test.beforeEach(async () => {
await Promise.all(messages.map((message) => message.delete()))
})
test.serial("basic text & content updates", async (t) => {
test.serial("kitchen sink + destroy", async (t) => {
const root = createRoot(channel)
await root.render("hi world")
await assertMessages(t, [{ content: "hi world" }])
await root.render(
<>
{"hi world"} {"hi moon"}
message <Text>content</Text>
no space
<Embed color="#feeeef">
description <Text>more description</Text>
</Embed>
<Embed>
another <Text>hi</Text>
</Embed>
</>,
)
await assertMessages(t, [{ content: "hi world hi moon" }])
await root.render(<Text>hi world</Text>)
await assertMessages(t, [{ content: "hi world" }])
await root.render(<Text></Text>)
await assertMessages(t, [{ content: "_ _" }])
await root.render(<Text>hi world</Text>)
await assertMessages(t, [{ content: "hi world" }])
await root.render([])
await assertMessages(t, [{ content: "_ _" }])
await assertMessages(t, [
{
content: "message contentno space",
embeds: [
{
color: 0xfeeeef,
description: "description more description",
},
{ color: null, description: "another hi" },
],
},
])
await root.destroy()
await assertMessages(t, [])
})
test.serial("updates", async (t) => {
const root = createRoot(channel)
// rapid updates
void root.render("hi world")
await root.render("hi moon")
await assertMessages(t, [{ content: "hi moon" }])
// regular update after initial render
await root.render(<Text>hi sun</Text>)
await assertMessages(t, [{ content: "hi sun" }])
// update that requires cloning a node
await root.render(<Text>the</Text>)
await assertMessages(t, [{ content: "the" }])
})
test.serial("nested text", async (t) => {
const root = createRoot(channel)
@@ -81,9 +103,31 @@ test.serial("nested text", async (t) => {
await assertMessages(t, [{ content: "hi world hi moon hi sun" }])
})
test.serial("empty embed fallback", async (t) => {
const root = createRoot(channel)
await root.render(<Embed />)
await assertMessages(t, [{ embeds: [{ color: null, description: "_ _" }] }])
})
test.serial("invalid children error", (t) => {
const root = createRoot(channel)
t.throws(() =>
root.render(
<Text>
<Embed />
</Text>,
),
)
})
type MessageData = ReturnType<typeof extractMessageData>
function extractMessageData(message: Message) {
return pick(message, "content", "embeds", "components")
return {
content: message.content,
embeds: message.embeds.map((embed) => pick(embed, "color", "description")),
}
}
async function assertMessages(
@@ -95,7 +139,6 @@ async function assertMessages(
const messageDataFallback: MessageData = {
content: "",
embeds: [],
components: [],
}
t.deepEqual(