Refactor some helper functions to its own module
This commit is contained in:
parent
0784365285
commit
862d84adf2
3 changed files with 40 additions and 41 deletions
24
src/testing/MockHelper.ts
Normal file
24
src/testing/MockHelper.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import Peer from "peerjs";
|
||||
import { MockPeer } from ".";
|
||||
import { LocalClient, PeerServer, PeerClient, RoomInfo } from "@/network";
|
||||
|
||||
export class MockHelper {
|
||||
private mocks: Record<string, MockPeer> = {};
|
||||
|
||||
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;
|
|
@ -1,2 +1,3 @@
|
|||
export * from "./MockDataConnection";
|
||||
export * from "./MockPeer";
|
||||
export * from "./MockHelper";
|
||||
|
|
|
@ -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<string, MockPeer> {
|
||||
return {};
|
||||
}
|
||||
|
||||
function createServer(
|
||||
mockSource: Record<string, MockPeer>,
|
||||
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<string, MockPeer>,
|
||||
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");
|
||||
|
|
Loading…
Reference in a new issue