1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-10 04:32:35 +01:00

unit tests expect event

This commit is contained in:
Flam3rboy 2021-09-21 23:13:31 +02:00
parent abdce76df4
commit b3f8df560a
3 changed files with 6628 additions and 5996 deletions

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@ import fs from "fs";
import Ajv from "ajv";
import addFormats from "ajv-formats";
import fetch from "node-fetch";
import { User } from "@fosscord/util";
import { Event, User, events } from "@fosscord/util";
const SchemaPath = join(__dirname, "..", "assets", "schemas.json");
const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" }));
@ -58,6 +58,12 @@ beforeAll(async (done) => {
}
});
const emit = events.emit;
events.emit = (event: string | symbol, ...args: any[]) => {
events.emit("event", args[0]);
return emit(event, ...args);
};
describe("Automatic unit tests with route description middleware", () => {
const routes = getRouteDescriptions();
@ -77,6 +83,23 @@ describe("Automatic unit tests with route description middleware", () => {
}
var body = "";
let eventEmitted = Promise.resolve();
if (route.test.event) {
if (!Array.isArray(route.test.event)) route.test.event = [route.test.event];
eventEmitted = new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject, 1000);
const received = [];
events.on("event", (event: Event) => {
if (!route.test.event.includes(event.event)) return;
received.push(event.event);
if (received.length === route.test.event.length) resolve();
});
});
}
try {
const response = await fetch(`http://localhost:3001/api${urlPath}`, {
@ -101,6 +124,12 @@ describe("Automatic unit tests with route description middleware", () => {
return done(error);
}
try {
await eventEmitted;
} catch (error) {
return done(new Error(`Event ${route.test.event} was not emitted`));
}
return done();
});
});

View File

@ -2,7 +2,7 @@ import { Channel } from "amqplib";
import { RabbitMQ } from "./RabbitMQ";
import EventEmitter from "events";
import { EVENT, Event } from "../interfaces";
const events = new EventEmitter();
export const events = new EventEmitter();
export async function emitEvent(payload: Omit<Event, "created_at">) {
const id = (payload.channel_id || payload.user_id || payload.guild_id) as string;