Files
discordeno/tests/messages/delete_message.ts
TriForMine 3fc62b9572 tests: add create/delete/edit message tests (#787)
* tests: add create/delete/edit message tests

* Use sleep instead of delay

* Deno fmt

* Add getters to message tests

* Change back to delay

* Deno fmt

* Use function in tests

* Deno fmt
2021-04-09 21:06:12 +01:00

67 lines
1.7 KiB
TypeScript

import { cache, delay, deleteMessage, sendMessage } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertExists } from "../deps.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
const message = await sendMessage(tempData.channelId, "Hello World!");
// Assertions
assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(5000);
// Make sure the message was created.
if (!cache.messages.has(message.id)) {
throw new Error(
"The message seemed to be sent but it was not cached.",
);
}
// Delete the message now
if (type === "raw") {
await deleteMessage(tempData.channelId, message.id, reason);
} else {
await message.delete(reason);
}
// Wait 5 seconds to give it time for MESSAGE_DELETE event
await delay(5000);
// Make sure it is gone from cache
if (cache.messages.has(message.id)) {
throw new Error(
"The message should have been deleted but it is still in cache.",
);
}
}
Deno.test({
name: "[message] delete a message without a reason.",
async fn() {
await ifItFailsBlameWolf("raw");
},
...defaultTestOptions,
});
Deno.test({
name: "[message] delete a message with a reason.",
async fn() {
await ifItFailsBlameWolf("raw", "with a reason");
},
...defaultTestOptions,
});
Deno.test({
name: "[message] message.delete() without a reason.",
async fn() {
await ifItFailsBlameWolf("getter");
},
...defaultTestOptions,
});
Deno.test({
name: "[message] message.delete() with a reason.",
async fn() {
await ifItFailsBlameWolf("getter", "with a reason");
},
...defaultTestOptions,
});