Skip to content

Commit

Permalink
feat: improve type-safety for events
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a committed Dec 8, 2024
1 parent 31e34e2 commit 5553ced
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions tests/commands/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,43 +136,39 @@ export function connectionTests(
let closeEventFired = false,
endEventFired = false;

const firedEvents: Array<string> = [];
client.addEventListener("close", () => {
closeEventFired = true;
firedEvents.push("close");
});
client.addEventListener("end", () => {
endEventFired = true;
firedEvents.push("end");
});

client.close();

assertEquals(closeEventFired, true);
assertEquals(endEventFired, true);
assertEquals(firedEvents, ["close", "end"]);
});

it("fires events with a lazy client", async () => {
const client = createLazyClient(getOpts());

let connectEventFired = false,
connectEventFiredTimes = 0,
readyEventFired = false,
readyEventFiredTimes = 0,
closeEventFired = false,
endEventFired = false;
const firedEvents: Array<string> = [];

client.addEventListener("connect", () => {
connectEventFired = true;
connectEventFiredTimes++;
firedEvents.push("connect");
});
client.addEventListener("ready", () => {
readyEventFired = true;
readyEventFiredTimes++;
firedEvents.push("ready");
}, { once: true });

client.addEventListener("close", () => {
closeEventFired = true;
firedEvents.push("close");
});
client.addEventListener("end", () => {
endEventFired = true;
firedEvents.push("end");
});

await client.exists("foo");
Expand All @@ -182,13 +178,15 @@ export function connectionTests(
await client.exists("foo");
client.close();

assertEquals(connectEventFired, true);
assertEquals(readyEventFired, true);
assertEquals(closeEventFired, true);
assertEquals(endEventFired, true);

assertEquals(connectEventFiredTimes, 2);
assertEquals(readyEventFiredTimes, 1);
assertEquals(firedEvents, [
"connect",
"ready",
"close",
"end",
"connect",
"close",
"end",
]);
});
});

Expand Down

0 comments on commit 5553ced

Please sign in to comment.