diff --git a/src/testing/MockHelper.ts b/src/testing/MockHelper.ts new file mode 100644 index 0000000..8b63404 --- /dev/null +++ b/src/testing/MockHelper.ts @@ -0,0 +1,24 @@ +import Peer from "peerjs"; +import { MockPeer } from "."; +import { LocalClient, PeerServer, PeerClient, RoomInfo } from "@/network"; + +export class MockHelper { + private mocks: Record = {}; + + createServer( + room: RoomInfo, + id: string = "test-server", + name: string = "server-client" + ) { + const serverPeer = new MockPeer(this.mocks, id, {}); + const serverPlayer = new LocalClient({ name: name }); + return new PeerServer(room, serverPlayer, serverPeer as Peer); + } + + createClient(id: string = "test-client", name: string = "client-peer") { + const clientPeer = new MockPeer(this.mocks, id, {}); + return new PeerClient({ name }, clientPeer as Peer); + } +} + +export default MockHelper; diff --git a/src/testing/index.ts b/src/testing/index.ts index 46190f0..647d674 100644 --- a/src/testing/index.ts +++ b/src/testing/index.ts @@ -1,2 +1,3 @@ export * from "./MockDataConnection"; export * from "./MockPeer"; +export * from "./MockHelper"; diff --git a/src/tests/unit/network.spec.ts b/src/tests/unit/network.spec.ts index 3c6ab1f..a4425f0 100644 --- a/src/tests/unit/network.spec.ts +++ b/src/tests/unit/network.spec.ts @@ -1,6 +1,5 @@ -import Peer from "peerjs"; -import { MockPeer } from "@/testing"; -import { PeerServer, LocalClient, PeerClient, NetworkMessage } from "@/network"; +import { MockHelper } from "@/testing"; +import { NetworkMessage } from "@/network"; import { EventHook } from "@/testing/EventHook"; const sampleRoom = () => ({ @@ -8,31 +7,6 @@ const sampleRoom = () => ({ password: "" }); -function mockSource(): Record { - return {}; -} - -function createServer( - mockSource: Record, - id: string = "test-server", - name: string = "server-client" -) { - const serverPeer = new MockPeer(mockSource, id, {}); - const serverPlayer = new LocalClient({ - name: name - }); - return new PeerServer(sampleRoom(), serverPlayer, serverPeer as Peer); -} - -function createClient( - mockSource: Record, - id: string = "test-client", - name: string = "client-peer" -) { - const clientPeer = new MockPeer(mockSource, id, {}); - return new PeerClient({ name }, clientPeer as Peer); -} - function messageKind(kind: string): (msg: NetworkMessage) => void { return msg => { expect(msg.kind).toEqual(kind); @@ -41,15 +15,15 @@ function messageKind(kind: string): (msg: NetworkMessage) => void { describe("network/PeerServer", () => { test("Create server", () => { - const mox = mockSource(); - createServer(mox); + const mox = new MockHelper(); + mox.createServer(sampleRoom()); }); test("Test client join", async () => { - const mox = mockSource(); + const mox = new MockHelper(); const hook = new EventHook(); - createServer(mox); - const client = createClient(mox); + mox.createServer(sampleRoom()); + const client = mox.createClient(); hook.hookEmitter(client, "data", "client-data"); client.connect("test-server"); await hook.expect("client-data", 1000, messageKind("room-info")); @@ -57,11 +31,11 @@ describe("network/PeerServer", () => { }); test("Test multiple clients (w/ name collision)", async () => { - const mox = mockSource(); + const mox = new MockHelper(); const hook = new EventHook(); - createServer(mox); - const client1 = createClient(mox); - const client2 = createClient(mox); + mox.createServer(sampleRoom()); + const client1 = mox.createClient(); + const client2 = mox.createClient(); hook.hookEmitter(client1, "data", "client1-data"); hook.hookEmitter(client2, "data", "client2-data"); client1.connect("test-server"); @@ -77,11 +51,11 @@ describe("network/PeerServer", () => { describe("network/PeerClient", () => { test("Client handles forced name change", async () => { - const mox = mockSource(); + const mox = new MockHelper(); const hook = new EventHook(); - createServer(mox); - const client1 = createClient(mox); - const client2 = createClient(mox); + mox.createServer(sampleRoom()); + const client1 = mox.createClient(); + const client2 = mox.createClient(); hook.hookEmitter(client2, "rename", "client-rename"); client1.connect("test-server"); client2.connect("test-server");