rendering to channel + simplified adapter interface

This commit is contained in:
MapleLeaf
2021-12-27 20:57:04 -06:00
parent 3682f67bfe
commit ef26b66cb8
17 changed files with 408 additions and 425 deletions

View File

@@ -1,11 +1,11 @@
import React from "react"
import { ReacordTester } from "../library/core/reacord-tester"
import { ActionRow, Button, Select } from "../library/main"
import { setupReacordTesting } from "./setup-testing"
const { assertRender } = setupReacordTesting()
const testing = new ReacordTester()
test("action row", async () => {
await assertRender(
await testing.assertRender(
<>
<Button label="outside button" onClick={() => {}} />
<ActionRow>

2
test/discord-js.test.tsx Normal file
View File

@@ -0,0 +1,2 @@
test.todo("discord js integration")
export {}

View File

@@ -1,4 +1,5 @@
import React from "react"
import { ReacordTester } from "../library/core/reacord-tester"
import {
Embed,
EmbedAuthor,
@@ -8,14 +9,13 @@ import {
EmbedThumbnail,
EmbedTitle,
} from "../library/main"
import { setupReacordTesting } from "./setup-testing"
const { assertRender } = setupReacordTesting()
const testing = new ReacordTester()
test("kitchen sink", async () => {
const now = new Date()
await assertRender(
await testing.assertRender(
<>
<Embed color={0xfe_ee_ef}>
<EmbedAuthor name="author" iconUrl="https://example.com/author.png" />
@@ -75,7 +75,7 @@ test("kitchen sink", async () => {
})
test("author variants", async () => {
await assertRender(
await testing.assertRender(
<>
<Embed>
<EmbedAuthor iconUrl="https://example.com/author.png">
@@ -110,7 +110,7 @@ test("author variants", async () => {
})
test("field variants", async () => {
await assertRender(
await testing.assertRender(
<>
<Embed>
<EmbedField name="field name" value="field value" />
@@ -157,7 +157,7 @@ test("field variants", async () => {
test("footer variants", async () => {
const now = new Date()
await assertRender(
await testing.assertRender(
<>
<Embed>
<EmbedFooter text="footer text" />
@@ -213,7 +213,7 @@ test("footer variants", async () => {
test("embed props", async () => {
const now = new Date()
await assertRender(
await testing.assertRender(
<Embed
title="title text"
description="description text"

View File

@@ -1,11 +1,11 @@
import React from "react"
import { ReacordTester } from "../library/core/reacord-tester"
import { Link } from "../library/main"
import { setupReacordTesting } from "./setup-testing"
const { assertRender } = setupReacordTesting()
const tester = new ReacordTester()
test("link", async () => {
await assertRender(
await tester.assertRender(
<>
<Link url="https://example.com/">link text</Link>
<Link label="link text" url="https://example.com/" />

View File

@@ -1,15 +1,19 @@
import * as React from "react"
import { Button, Embed, EmbedField, EmbedTitle } from "../library/main"
import { TestCommandInteraction } from "../library/testing"
import { setupReacordTesting } from "./setup-testing"
import {
Button,
Embed,
EmbedField,
EmbedTitle,
ReacordTester,
} from "../library/main"
test("rendering behavior", async () => {
const { reacord, adapter, assertMessages } = setupReacordTesting()
const tester = new ReacordTester()
const reply = reacord.reply(new TestCommandInteraction(adapter))
const reply = tester.reply()
reply.render(<KitchenSinkCounter onDeactivate={() => reply.deactivate()} />)
await assertMessages([
await tester.assertMessages([
{
content: "count: 0",
embeds: [],
@@ -35,8 +39,8 @@ test("rendering behavior", async () => {
},
])
adapter.findButtonByLabel("show embed").click()
await assertMessages([
tester.findButtonByLabel("show embed").click()
await tester.assertMessages([
{
content: "count: 0",
embeds: [{ title: "the counter" }],
@@ -62,8 +66,8 @@ test("rendering behavior", async () => {
},
])
adapter.findButtonByLabel("clicc").click()
await assertMessages([
tester.findButtonByLabel("clicc").click()
await tester.assertMessages([
{
content: "count: 1",
embeds: [
@@ -94,8 +98,8 @@ test("rendering behavior", async () => {
},
])
adapter.findButtonByLabel("clicc").click()
await assertMessages([
tester.findButtonByLabel("clicc").click()
await tester.assertMessages([
{
content: "count: 2",
embeds: [
@@ -126,8 +130,8 @@ test("rendering behavior", async () => {
},
])
adapter.findButtonByLabel("hide embed").click()
await assertMessages([
tester.findButtonByLabel("hide embed").click()
await tester.assertMessages([
{
content: "count: 2",
embeds: [],
@@ -153,8 +157,8 @@ test("rendering behavior", async () => {
},
])
adapter.findButtonByLabel("clicc").click()
await assertMessages([
tester.findButtonByLabel("clicc").click()
await tester.assertMessages([
{
content: "count: 3",
embeds: [],
@@ -180,8 +184,8 @@ test("rendering behavior", async () => {
},
])
adapter.findButtonByLabel("deactivate").click()
await assertMessages([
tester.findButtonByLabel("deactivate").click()
await tester.assertMessages([
{
content: "count: 3",
embeds: [],
@@ -210,8 +214,8 @@ test("rendering behavior", async () => {
},
])
adapter.findButtonByLabel("clicc").click()
await assertMessages([
tester.findButtonByLabel("clicc").click()
await tester.assertMessages([
{
content: "count: 3",
embeds: [],
@@ -242,9 +246,9 @@ test("rendering behavior", async () => {
})
test("delete", async () => {
const { reacord, adapter, assertMessages } = setupReacordTesting()
const tester = new ReacordTester()
const reply = reacord.reply(new TestCommandInteraction(adapter))
const reply = tester.reply()
reply.render(
<>
some text
@@ -253,7 +257,7 @@ test("delete", async () => {
</>,
)
await assertMessages([
await tester.assertMessages([
{
content: "some text",
embeds: [{ description: "some embed" }],
@@ -264,7 +268,7 @@ test("delete", async () => {
])
reply.destroy()
await assertMessages([])
await tester.assertMessages([])
})
// test multiple instances that can be updated independently,

View File

@@ -1,11 +1,9 @@
import { jest } from "@jest/globals"
import React, { useState } from "react"
import { Button, Option, Select } from "../library/main"
import { setupReacordTesting } from "./setup-testing"
const { adapter, reply, assertRender, assertMessages } = setupReacordTesting()
import { Button, Option, ReacordTester, Select } from "../library/main"
test("single select", async () => {
const tester = new ReacordTester()
const onSelect = jest.fn()
function TestSelect() {
@@ -30,7 +28,7 @@ test("single select", async () => {
}
async function assertSelect(values: string[], disabled = false) {
await assertMessages([
await tester.assertMessages([
{
content: "",
embeds: [],
@@ -54,23 +52,26 @@ test("single select", async () => {
])
}
const reply = tester.reply()
reply.render(<TestSelect />)
await assertSelect([])
expect(onSelect).toHaveBeenCalledTimes(0)
adapter.findSelectByPlaceholder("choose one").select("2")
tester.findSelectByPlaceholder("choose one").select("2")
await assertSelect(["2"])
expect(onSelect).toHaveBeenCalledWith({ values: ["2"] })
adapter.findButtonByLabel("disable").click()
tester.findButtonByLabel("disable").click()
await assertSelect(["2"], true)
adapter.findSelectByPlaceholder("choose one").select("1")
tester.findSelectByPlaceholder("choose one").select("1")
await assertSelect(["2"], true)
expect(onSelect).toHaveBeenCalledTimes(1)
})
test("multiple select", async () => {
const tester = new ReacordTester()
const onSelect = jest.fn()
function TestSelect() {
@@ -91,7 +92,7 @@ test("multiple select", async () => {
}
async function assertSelect(values: string[]) {
await assertMessages([
await tester.assertMessages([
{
content: "",
embeds: [],
@@ -115,31 +116,34 @@ test("multiple select", async () => {
])
}
const reply = tester.reply()
reply.render(<TestSelect />)
await assertSelect([])
expect(onSelect).toHaveBeenCalledTimes(0)
adapter.findSelectByPlaceholder("select").select("1", "3")
tester.findSelectByPlaceholder("select").select("1", "3")
await assertSelect(expect.arrayContaining(["1", "3"]))
expect(onSelect).toHaveBeenCalledWith({
values: expect.arrayContaining(["1", "3"]),
})
adapter.findSelectByPlaceholder("select").select("2")
tester.findSelectByPlaceholder("select").select("2")
await assertSelect(expect.arrayContaining(["2"]))
expect(onSelect).toHaveBeenCalledWith({
values: expect.arrayContaining(["2"]),
})
adapter.findSelectByPlaceholder("select").select()
tester.findSelectByPlaceholder("select").select()
await assertSelect([])
expect(onSelect).toHaveBeenCalledWith({ values: [] })
})
test("optional onSelect + unknown value", async () => {
reply.render(<Select placeholder="select" />)
adapter.findSelectByPlaceholder("select").select("something")
await assertMessages([
const tester = new ReacordTester()
tester.reply().render(<Select placeholder="select" />)
tester.findSelectByPlaceholder("select").select("something")
await tester.assertMessages([
{
content: "",
embeds: [],

View File

@@ -1,52 +0,0 @@
import { nextTick } from "node:process"
import { promisify } from "node:util"
import type { ReactNode } from "react"
import { logPretty } from "../helpers/log-pretty"
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 = TestAdapter.create()
const reacord = new Reacord({ adapter })
const reply = reacord.reply(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)
}
function logMessages() {
logPretty(sampleMessages(adapter))
}
return {
reacord,
adapter,
reply,
assertMessages,
assertRender,
logMessages,
}
}
function sampleMessages(adapter: TestAdapter) {
return adapter.messages.map((message) => ({
...message.options,
actionRows: message.options.actionRows.map((row) =>
row.map((component) =>
omit(component, ["customId", "onClick", "onSelect", "onSelectValue"]),
),
),
}))
}