From 02e938ea5622e1063354598e6df63f35ecae692a Mon Sep 17 00:00:00 2001 From: MapleLeaf <19603573+itsMapleLeaf@users.noreply.github.com> Date: Thu, 9 Dec 2021 18:48:26 -0600 Subject: [PATCH] move integration tests to their own folder --- {src => integration}/test-environment.ts | 2 +- .../text.test.ts | 22 +++++++++---------- package.json | 2 +- src/container.ts | 6 ++++- src/render.ts | 10 +++++---- 5 files changed, 24 insertions(+), 18 deletions(-) rename {src => integration}/test-environment.ts (88%) rename src/container.test.ts => integration/text.test.ts (76%) diff --git a/src/test-environment.ts b/integration/test-environment.ts similarity index 88% rename from src/test-environment.ts rename to integration/test-environment.ts index 3ae01c0..ac49a85 100644 --- a/src/test-environment.ts +++ b/integration/test-environment.ts @@ -1,5 +1,5 @@ import "dotenv/config.js" -import { raise } from "./helpers/raise.js" +import { raise } from "../src/helpers/raise.js" function getEnvironmentValue(name: string) { return process.env[name] ?? raise(`Missing environment variable: ${name}`) diff --git a/src/container.test.ts b/integration/text.test.ts similarity index 76% rename from src/container.test.ts rename to integration/text.test.ts index 9557468..c09b723 100644 --- a/src/container.test.ts +++ b/integration/text.test.ts @@ -1,8 +1,8 @@ import test from "ava" import { Client, TextChannel } from "discord.js" import { nanoid } from "nanoid" -import { ReacordContainer } from "./container.js" -import { raise } from "./helpers/raise.js" +import { raise } from "../src/helpers/raise.js" +import { createRoot } from "../src/render.js" import { testBotToken, testChannelId } from "./test-environment.js" const client = new Client({ @@ -31,10 +31,10 @@ test.after(() => { }) test("rendering text", async (t) => { - const container = new ReacordContainer(channel) + const root = createRoot(channel) const content = nanoid() - await container.render([content]) + await root.render(content) { const messages = await channel.messages.fetch() @@ -42,14 +42,14 @@ test("rendering text", async (t) => { } const newContent = nanoid() - await container.render([newContent]) + await root.render(newContent) { const messages = await channel.messages.fetch() t.true(messages.some((m) => m.content === newContent)) } - await container.render([]) + await root.render(false) { const messages = await channel.messages.fetch() @@ -58,21 +58,21 @@ test("rendering text", async (t) => { }) test("rapid updates", async (t) => { - const container = new ReacordContainer(channel) + const root = createRoot(channel) const content = nanoid() const newContent = nanoid() - void container.render([content]) - await container.render([newContent]) + void root.render(content) + await root.render(newContent) { const messages = await channel.messages.fetch() t.true(messages.some((m) => m.content === newContent)) } - void container.render([content]) - await container.render([]) + void root.render(content) + await root.render(false) { const messages = await channel.messages.fetch() diff --git a/package.json b/package.json index 7ae8080..98f43d0 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "--no-warnings" ], "files": [ - "src/**/*.test.ts" + "{src,integration}/**/*.test.ts" ] } } diff --git a/src/container.ts b/src/container.ts index d0c6166..f712c6a 100644 --- a/src/container.ts +++ b/src/container.ts @@ -8,7 +8,7 @@ export class ReacordContainer { channel: TextBasedChannels message?: Message actions: Action[] = [] - runningPromise?: PromiseLike + runningPromise?: Promise constructor(channel: TextBasedChannels) { this.channel = channel @@ -75,4 +75,8 @@ export class ReacordContainer { await this.runningPromise this.runningPromise = undefined } + + awaitActions() { + return this.runningPromise ?? Promise.resolve() + } } diff --git a/src/render.ts b/src/render.ts index 8ee47b9..7282f91 100644 --- a/src/render.ts +++ b/src/render.ts @@ -1,20 +1,22 @@ /* eslint-disable unicorn/no-null */ import type { TextBasedChannels } from "discord.js" +import type { ReactNode } from "react" import { ReacordContainer } from "./container" import { reconciler } from "./reconciler" export type ReacordRenderTarget = TextBasedChannels -export function render(content: string, target: ReacordRenderTarget) { +export function createRoot(target: ReacordRenderTarget) { const container = new ReacordContainer(target) const containerId = reconciler.createContainer(container, 0, false, null) - reconciler.updateContainer(content, containerId) return { - rerender: (newContent: string) => { - reconciler.updateContainer(newContent, containerId) + render: (content: ReactNode) => { + reconciler.updateContainer(content, containerId) + return container.awaitActions() }, destroy: () => { reconciler.updateContainer(null, containerId) + return container.awaitActions() }, } }