joinRoom.test.ts 2.01 KB
import { expect } from "chai";
import { RoomManager } from "../room/RoomManager";
import { DummySocket } from "./util/DummySocket";
import { prepareJoinedRoom, prepareUsersEmptyRooms } from "./util/prepare";
import { SocketTester } from "./util/SocketTester";

describe("방 입장", () => {
  it("로그인하지 않은 유저는 요청할 수 없습니다", () => {
    const roomManager = new RoomManager();
    const socket = new SocketTester(roomManager);
    const room = roomManager.create("테스트", 2);
    socket.testNotOk("joinRoom", { uuid: room.uuid });
  });
  it("방에 입장합니다", () => {
    const {
      sockets: [socket],
      users: [user],
      rooms: [room],
    } = prepareUsersEmptyRooms(1, 1);

    const response = socket.test("joinRoom", { uuid: room.uuid });
    expect(response.ok).eq(true);
    expect(response.result?.uuid).eq(room.uuid);
    expect(response.result?.name).eq(room.name);
    expect(response.result?.maxUsers).eq(room.maxUsers);
    expect(response.result?.users?.length).eq(1);
    expect(response.result?.users[0]?.nickname).eq(user.nickname);
  });
  it("방에 입장하면 유저 목록이 업데이트 됩니다", () => {
    const {
      sockets: [socket1, socket2],
      users: [user1, user2],
    } = prepareJoinedRoom(2);

    const updated = socket1.socket.received("updateRoomUser");
    expect(updated.state).eq("added");
    expect(updated.user.nickname).eq(user2.nickname);
  });
  it("방에 이미 입장한 상태에서 다른 방에 입장할 수 없습니다", () => {
    const {
      sockets: [socket],
      rooms: [room1, room2],
    } = prepareUsersEmptyRooms(1, 2);

    socket.testOk("joinRoom", { uuid: room1.uuid });
    socket.testNotOk("joinRoom", { uuid: room2.uuid });
  });
  it("가득 찬 방에는 입장할 수 없습니다", () => {
    const {
      sockets: [socket1, socket2],
      rooms: [room],
    } = prepareUsersEmptyRooms(2, 1, 1);

    socket1.testOk("joinRoom", { uuid: room.uuid });
    socket2.testNotOk("joinRoom", { uuid: room.uuid });
  });
});