leaveRoom.test.ts 2.07 KB
import { expect } from "chai";
import { RoomManager } from "../room/RoomManager";
import { User } from "../user/User";
import { DummySocket } from "./util/DummySocket";
import { SocketTester } from "./util/SocketTester";

describe("방 퇴장", () => {
  const roomManager = new RoomManager();

  it("로그인하지 않은 유저는 요청할 수 없습니다", () => {
    const socket = new SocketTester(roomManager);
    const response = socket.test("leaveRoom", {});
    expect(response.ok).eq(false);
  });
  it("방에 입장하지 않은 유저는 요청할 수 없습니다", () => {
    const socket = new SocketTester(roomManager);
    socket.login("guest");

    const response = socket.test("leaveRoom", {});
    expect(response.ok).eq(false);
  });
  it("방에서 퇴장합니다", () => {
    const socket = new SocketTester(roomManager);
    socket.login("guest");

    const room = roomManager.create("테스트", 2);
    expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
    expect(socket.test("leaveRoom", {}).ok).eq(true);
  });
  it("방에서 퇴장한 뒤 다시 요청을 보낼 수 없습니다", () => {
    const socket = new SocketTester(roomManager);
    socket.login("guest");

    const room = roomManager.create("테스트", 2);
    expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
    expect(socket.test("leaveRoom", {}).ok).eq(true);
    expect(socket.test("leaveRoom", {}).ok).eq(false);
  });
  it("방에서 퇴장하면 유저 목록이 업데이트 됩니다", () => {
    const socket1 = new SocketTester(roomManager);
    const socket2 = new SocketTester(roomManager);

    socket1.login("guest1");
    socket2.login("guest2");

    const room = roomManager.create("테스트", 2);

    expect(socket1.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
    expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
    expect(socket2.test("leaveRoom", {}).ok).eq(true);
    const updated = socket1.socket.received("updateRoomUser");
    expect(updated.state).eq("removed");
    expect(updated.user.username).eq("guest2");
  });
});