Add tests

This commit is contained in:
Ticruz
2025-02-04 13:21:23 +01:00
committed by Jon Staab
parent 917727c86f
commit 8a2b62f693
57 changed files with 9231 additions and 25 deletions
+40
View File
@@ -0,0 +1,40 @@
import {Connection, ConnectionStatus} from "../src/Connection"
import {ConnectionEvent} from "../src/ConnectionEvent"
import {vi, describe, it, expect, beforeEach, afterEach} from "vitest"
describe("Connection", () => {
let connection: Connection
beforeEach(() => {
connection = new Connection("wss://test.relay/")
})
afterEach(() => {
connection.cleanup()
})
it("should initialize with correct state", () => {
expect(connection.status).toBe(ConnectionStatus.Open)
expect(connection.url).toBe("wss://test.relay/")
})
it("should emit events with connection instance", () => {
const spy = vi.fn()
connection.on(ConnectionEvent.Open, spy)
connection.emit(ConnectionEvent.Open)
expect(spy).toHaveBeenCalledWith(connection)
})
it("should throw when sending message on closed connection", async () => {
connection.close()
await expect(connection.send(["EVENT", {}])).rejects.toThrow()
})
it("should cleanup properly", () => {
const spy = vi.fn()
connection.on("test", spy)
connection.cleanup()
connection.emit("test" as any)
expect(spy).not.toHaveBeenCalled()
})
})