improve testing helpers

This commit is contained in:
MapleLeaf
2021-12-26 23:43:54 -06:00
parent 486df6479f
commit 4f978c101a
5 changed files with 246 additions and 232 deletions

View File

@@ -1,23 +0,0 @@
import { nextTick } from "node:process"
import { promisify } from "node:util"
import { omit } from "../helpers/omit"
import type { TestAdapter } from "../library/testing"
const nextTickPromise = promisify(nextTick)
export async function assertMessages(
adapter: TestAdapter,
expected: ReturnType<typeof extractMessageDataSample>,
) {
await nextTickPromise()
expect(extractMessageDataSample(adapter)).toEqual(expected)
}
function extractMessageDataSample(adapter: TestAdapter) {
return adapter.messages.map((message) => ({
...message.options,
actionRows: message.options.actionRows.map((row) =>
row.map((component) => omit(component, ["customId"])),
),
}))
}

View File

@@ -10,16 +10,18 @@ import {
Reacord, Reacord,
} from "../library/main" } from "../library/main"
import { TestAdapter, TestCommandInteraction } from "../library/testing" import { TestAdapter, TestCommandInteraction } from "../library/testing"
import { assertMessages } from "./assert-messages" import { setupReacordTesting } from "./setup-testing"
const adapter = new TestAdapter() const adapter = new TestAdapter()
const reacord = new Reacord({ adapter }) const reacord = new Reacord({ adapter })
const reply = reacord.createCommandReply(new TestCommandInteraction(adapter)) const reply = reacord.createCommandReply(new TestCommandInteraction(adapter))
const { assertRender } = setupReacordTesting()
test("kitchen sink", async () => { test("kitchen sink", async () => {
const now = new Date() const now = new Date()
reply.render( await assertRender(
<> <>
<Embed color={0xfe_ee_ef}> <Embed color={0xfe_ee_ef}>
<EmbedAuthor name="author" iconUrl="https://example.com/author.png" /> <EmbedAuthor name="author" iconUrl="https://example.com/author.png" />
@@ -36,9 +38,7 @@ test("kitchen sink", async () => {
/> />
</Embed> </Embed>
</>, </>,
) [
await assertMessages(adapter, [
{ {
actionRows: [], actionRows: [],
content: "", content: "",
@@ -75,11 +75,12 @@ test("kitchen sink", async () => {
}, },
], ],
}, },
]) ],
)
}) })
test("author variants", async () => { test("author variants", async () => {
reply.render( await assertRender(
<> <>
<Embed> <Embed>
<EmbedAuthor iconUrl="https://example.com/author.png"> <EmbedAuthor iconUrl="https://example.com/author.png">
@@ -90,9 +91,7 @@ test("author variants", async () => {
<EmbedAuthor iconUrl="https://example.com/author.png" /> <EmbedAuthor iconUrl="https://example.com/author.png" />
</Embed> </Embed>
</>, </>,
) [
await assertMessages(adapter, [
{ {
content: "", content: "",
actionRows: [], actionRows: [],
@@ -111,11 +110,12 @@ test("author variants", async () => {
}, },
], ],
}, },
]) ],
)
}) })
test("field variants", async () => { test("field variants", async () => {
reply.render( await assertRender(
<> <>
<Embed> <Embed>
<EmbedField name="field name" value="field value" /> <EmbedField name="field name" value="field value" />
@@ -126,9 +126,7 @@ test("field variants", async () => {
<EmbedField name="field name" /> <EmbedField name="field name" />
</Embed> </Embed>
</>, </>,
) [
await assertMessages(adapter, [
{ {
content: "", content: "",
actionRows: [], actionRows: [],
@@ -157,12 +155,14 @@ test("field variants", async () => {
}, },
], ],
}, },
]) ],
)
}) })
test("footer variants", async () => { test("footer variants", async () => {
const now = new Date() const now = new Date()
reply.render(
await assertRender(
<> <>
<Embed> <Embed>
<EmbedFooter text="footer text" /> <EmbedFooter text="footer text" />
@@ -180,9 +180,7 @@ test("footer variants", async () => {
<EmbedFooter iconUrl="https://example.com/footer.png" timestamp={now} /> <EmbedFooter iconUrl="https://example.com/footer.png" timestamp={now} />
</Embed> </Embed>
</>, </>,
) [
await assertMessages(adapter, [
{ {
content: "", content: "",
actionRows: [], actionRows: [],
@@ -213,13 +211,14 @@ test("footer variants", async () => {
}, },
], ],
}, },
]) ],
)
}) })
test("embed props", async () => { test("embed props", async () => {
const now = new Date() const now = new Date()
reply.render( await assertRender(
<Embed <Embed
title="title text" title="title text"
description="description text" description="description text"
@@ -246,9 +245,7 @@ test("embed props", async () => {
{ name: "block field", value: "block field value" }, { name: "block field", value: "block field value" },
]} ]}
/>, />,
) [
await assertMessages(adapter, [
{ {
content: "", content: "",
actionRows: [], actionRows: [],
@@ -277,5 +274,6 @@ test("embed props", async () => {
}, },
], ],
}, },
]) ],
)
}) })

View File

@@ -1,22 +1,17 @@
import React from "react" import React from "react"
import { Link, Reacord } from "../library/main" import { Link } from "../library/main"
import { TestAdapter, TestCommandInteraction } from "../library/testing" import { setupReacordTesting } from "./setup-testing"
import { assertMessages } from "./assert-messages"
const adapter = new TestAdapter() const { assertRender } = setupReacordTesting()
const reacord = new Reacord({ adapter })
const reply = reacord.createCommandReply(new TestCommandInteraction(adapter))
test("link", async () => { test("link", async () => {
reply.render( await assertRender(
<> <>
<Link url="https://example.com/">link text</Link> <Link url="https://example.com/">link text</Link>
<Link label="link text" url="https://example.com/" /> <Link label="link text" url="https://example.com/" />
<Link label="link text" url="https://example.com/" disabled /> <Link label="link text" url="https://example.com/" disabled />
</>, </>,
) [
await assertMessages(adapter, [
{ {
content: "", content: "",
embeds: [], embeds: [],
@@ -41,5 +36,6 @@ test("link", async () => {
], ],
], ],
}, },
]) ],
)
}) })

View File

@@ -1,16 +1,15 @@
import * as React from "react" import * as React from "react"
import { Button, Embed, EmbedField, EmbedTitle, Reacord } from "../library/main" import { Button, Embed, EmbedField, EmbedTitle } from "../library/main"
import { TestAdapter, TestCommandInteraction } from "../library/testing" import { TestCommandInteraction } from "../library/testing"
import { assertMessages } from "./assert-messages" import { setupReacordTesting } from "./setup-testing"
const { reacord, adapter, assertMessages } = setupReacordTesting()
test("rendering behavior", async () => { test("rendering behavior", async () => {
const adapter = new TestAdapter()
const reacord = new Reacord({ adapter })
const reply = reacord.createCommandReply(new TestCommandInteraction(adapter)) const reply = reacord.createCommandReply(new TestCommandInteraction(adapter))
reply.render(<KitchenSinkCounter onDeactivate={() => reply.deactivate()} />) reply.render(<KitchenSinkCounter onDeactivate={() => reply.deactivate()} />)
await assertMessages(adapter, [ await assertMessages([
{ {
content: "count: 0", content: "count: 0",
embeds: [], embeds: [],
@@ -37,7 +36,7 @@ test("rendering behavior", async () => {
]) ])
adapter.findButtonByLabel("show embed").click() adapter.findButtonByLabel("show embed").click()
await assertMessages(adapter, [ await assertMessages([
{ {
content: "count: 0", content: "count: 0",
embeds: [{ title: "the counter" }], embeds: [{ title: "the counter" }],
@@ -64,7 +63,7 @@ test("rendering behavior", async () => {
]) ])
adapter.findButtonByLabel("clicc").click() adapter.findButtonByLabel("clicc").click()
await assertMessages(adapter, [ await assertMessages([
{ {
content: "count: 1", content: "count: 1",
embeds: [ embeds: [
@@ -96,7 +95,7 @@ test("rendering behavior", async () => {
]) ])
adapter.findButtonByLabel("clicc").click() adapter.findButtonByLabel("clicc").click()
await assertMessages(adapter, [ await assertMessages([
{ {
content: "count: 2", content: "count: 2",
embeds: [ embeds: [
@@ -128,7 +127,7 @@ test("rendering behavior", async () => {
]) ])
adapter.findButtonByLabel("hide embed").click() adapter.findButtonByLabel("hide embed").click()
await assertMessages(adapter, [ await assertMessages([
{ {
content: "count: 2", content: "count: 2",
embeds: [], embeds: [],
@@ -155,7 +154,7 @@ test("rendering behavior", async () => {
]) ])
adapter.findButtonByLabel("clicc").click() adapter.findButtonByLabel("clicc").click()
await assertMessages(adapter, [ await assertMessages([
{ {
content: "count: 3", content: "count: 3",
embeds: [], embeds: [],
@@ -182,7 +181,7 @@ test("rendering behavior", async () => {
]) ])
adapter.findButtonByLabel("deactivate").click() adapter.findButtonByLabel("deactivate").click()
await assertMessages(adapter, [ await assertMessages([
{ {
content: "count: 3", content: "count: 3",
embeds: [], embeds: [],
@@ -212,7 +211,7 @@ test("rendering behavior", async () => {
]) ])
adapter.findButtonByLabel("clicc").click() adapter.findButtonByLabel("clicc").click()
await assertMessages(adapter, [ await assertMessages([
{ {
content: "count: 3", content: "count: 3",
embeds: [], embeds: [],

44
test/setup-testing.ts Normal file
View File

@@ -0,0 +1,44 @@
import { nextTick } from "node:process"
import { promisify } from "node:util"
import type { ReactNode } from "react"
import { omit } from "../helpers/omit"
import { Reacord } from "../library/main"
import { TestAdapter, TestCommandInteraction } from "../library/testing"
const nextTickPromise = promisify(nextTick)
export function setupReacordTesting() {
const adapter = new TestAdapter()
const reacord = new Reacord({ adapter })
const reply = reacord.createCommandReply(new TestCommandInteraction(adapter))
async function assertMessages(expected: ReturnType<typeof sampleMessages>) {
await nextTickPromise() // wait for the render to complete
expect(sampleMessages(adapter)).toEqual(expected)
}
async function assertRender(
content: ReactNode,
expected: ReturnType<typeof sampleMessages>,
) {
reply.render(content)
await assertMessages(expected)
}
return {
reacord,
adapter,
reply,
assertMessages,
assertRender,
}
}
function sampleMessages(adapter: TestAdapter) {
return adapter.messages.map((message) => ({
...message.options,
actionRows: message.options.actionRows.map((row) =>
row.map((component) => omit(component, ["customId"])),
),
}))
}