move integration tests to their own folder

This commit is contained in:
MapleLeaf
2021-12-09 18:48:26 -06:00
parent ffe069eb31
commit 02e938ea56
5 changed files with 24 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
import "dotenv/config.js" import "dotenv/config.js"
import { raise } from "./helpers/raise.js" import { raise } from "../src/helpers/raise.js"
function getEnvironmentValue(name: string) { function getEnvironmentValue(name: string) {
return process.env[name] ?? raise(`Missing environment variable: ${name}`) return process.env[name] ?? raise(`Missing environment variable: ${name}`)

View File

@@ -1,8 +1,8 @@
import test from "ava" import test from "ava"
import { Client, TextChannel } from "discord.js" import { Client, TextChannel } from "discord.js"
import { nanoid } from "nanoid" import { nanoid } from "nanoid"
import { ReacordContainer } from "./container.js" import { raise } from "../src/helpers/raise.js"
import { raise } from "./helpers/raise.js" import { createRoot } from "../src/render.js"
import { testBotToken, testChannelId } from "./test-environment.js" import { testBotToken, testChannelId } from "./test-environment.js"
const client = new Client({ const client = new Client({
@@ -31,10 +31,10 @@ test.after(() => {
}) })
test("rendering text", async (t) => { test("rendering text", async (t) => {
const container = new ReacordContainer(channel) const root = createRoot(channel)
const content = nanoid() const content = nanoid()
await container.render([content]) await root.render(content)
{ {
const messages = await channel.messages.fetch() const messages = await channel.messages.fetch()
@@ -42,14 +42,14 @@ test("rendering text", async (t) => {
} }
const newContent = nanoid() const newContent = nanoid()
await container.render([newContent]) await root.render(newContent)
{ {
const messages = await channel.messages.fetch() const messages = await channel.messages.fetch()
t.true(messages.some((m) => m.content === newContent)) t.true(messages.some((m) => m.content === newContent))
} }
await container.render([]) await root.render(false)
{ {
const messages = await channel.messages.fetch() const messages = await channel.messages.fetch()
@@ -58,21 +58,21 @@ test("rendering text", async (t) => {
}) })
test("rapid updates", async (t) => { test("rapid updates", async (t) => {
const container = new ReacordContainer(channel) const root = createRoot(channel)
const content = nanoid() const content = nanoid()
const newContent = nanoid() const newContent = nanoid()
void container.render([content]) void root.render(content)
await container.render([newContent]) await root.render(newContent)
{ {
const messages = await channel.messages.fetch() const messages = await channel.messages.fetch()
t.true(messages.some((m) => m.content === newContent)) t.true(messages.some((m) => m.content === newContent))
} }
void container.render([content]) void root.render(content)
await container.render([]) await root.render(false)
{ {
const messages = await channel.messages.fetch() const messages = await channel.messages.fetch()

View File

@@ -86,7 +86,7 @@
"--no-warnings" "--no-warnings"
], ],
"files": [ "files": [
"src/**/*.test.ts" "{src,integration}/**/*.test.ts"
] ]
} }
} }

View File

@@ -8,7 +8,7 @@ export class ReacordContainer {
channel: TextBasedChannels channel: TextBasedChannels
message?: Message message?: Message
actions: Action[] = [] actions: Action[] = []
runningPromise?: PromiseLike<void> runningPromise?: Promise<void>
constructor(channel: TextBasedChannels) { constructor(channel: TextBasedChannels) {
this.channel = channel this.channel = channel
@@ -75,4 +75,8 @@ export class ReacordContainer {
await this.runningPromise await this.runningPromise
this.runningPromise = undefined this.runningPromise = undefined
} }
awaitActions() {
return this.runningPromise ?? Promise.resolve()
}
} }

View File

@@ -1,20 +1,22 @@
/* eslint-disable unicorn/no-null */ /* eslint-disable unicorn/no-null */
import type { TextBasedChannels } from "discord.js" import type { TextBasedChannels } from "discord.js"
import type { ReactNode } from "react"
import { ReacordContainer } from "./container" import { ReacordContainer } from "./container"
import { reconciler } from "./reconciler" import { reconciler } from "./reconciler"
export type ReacordRenderTarget = TextBasedChannels export type ReacordRenderTarget = TextBasedChannels
export function render(content: string, target: ReacordRenderTarget) { export function createRoot(target: ReacordRenderTarget) {
const container = new ReacordContainer(target) const container = new ReacordContainer(target)
const containerId = reconciler.createContainer(container, 0, false, null) const containerId = reconciler.createContainer(container, 0, false, null)
reconciler.updateContainer(content, containerId)
return { return {
rerender: (newContent: string) => { render: (content: ReactNode) => {
reconciler.updateContainer(newContent, containerId) reconciler.updateContainer(content, containerId)
return container.awaitActions()
}, },
destroy: () => { destroy: () => {
reconciler.updateContainer(null, containerId) reconciler.updateContainer(null, containerId)
return container.awaitActions()
}, },
} }
} }