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,81 +0,0 @@
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 { testBotToken, testChannelId } from "./test-environment.js"
const client = new Client({
intents: ["GUILDS"],
})
let channel: TextChannel
test.before(async () => {
await client.login(testBotToken)
const result =
client.channels.cache.get(testChannelId) ??
(await client.channels.fetch(testChannelId)) ??
raise("Channel not found")
if (!(result instanceof TextChannel)) {
throw new TypeError("Channel must be a text channel")
}
channel = result
})
test.after(() => {
client.destroy()
})
test("rendering text", async (t) => {
const container = new ReacordContainer(channel)
const content = nanoid()
await container.render([content])
{
const messages = await channel.messages.fetch()
t.true(messages.some((m) => m.content === content))
}
const newContent = nanoid()
await container.render([newContent])
{
const messages = await channel.messages.fetch()
t.true(messages.some((m) => m.content === newContent))
}
await container.render([])
{
const messages = await channel.messages.fetch()
t.false(messages.some((m) => m.content === newContent))
}
})
test("rapid updates", async (t) => {
const container = new ReacordContainer(channel)
const content = nanoid()
const newContent = nanoid()
void container.render([content])
await container.render([newContent])
{
const messages = await channel.messages.fetch()
t.true(messages.some((m) => m.content === newContent))
}
void container.render([content])
await container.render([])
{
const messages = await channel.messages.fetch()
t.false(messages.some((m) => m.content === newContent))
}
})

View File

@@ -8,7 +8,7 @@ export class ReacordContainer {
channel: TextBasedChannels
message?: Message
actions: Action[] = []
runningPromise?: PromiseLike<void>
runningPromise?: Promise<void>
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()
}
}

View File

@@ -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()
},
}
}

View File

@@ -1,10 +0,0 @@
import "dotenv/config.js"
import { raise } from "./helpers/raise.js"
function getEnvironmentValue(name: string) {
return process.env[name] ?? raise(`Missing environment variable: ${name}`)
}
export const testBotToken = getEnvironmentValue("TEST_BOT_TOKEN")
// export const testGuildId = getEnvironmentValue("TEST_GUILD_ID")
export const testChannelId = getEnvironmentValue("TEST_CHANNEL_ID")