Skip to content

2023 02 28 開發日誌

Hong, Jian-Ching edited this page Mar 21, 2023 · 2 revisions

概要

ATDD

試著實作 WebSocket 的 ATDD,但是寫起來很不順

需要針對每個命令第幾次的回應來進行測試

  it('when cell is unopened then open should be opened', (done) => {
    ws.on('open', () => {
      const data = JSON.stringify({ event: 'ping', data: {} });
      ws.send(data);
    });

    let gameInfoCount = 0;
    ws.on('message', (message) => {
      const event = JSON.parse(message.toString());
      // expect(event.event).toBe("gameInfo");

      switch (event.event) {
        case 'pong':
          gameInfo();
          break;
        case 'gameInfo':
          gameInfoCount++;
          switch (gameInfoCount) {
            case 1:
              // console.log(`gameInfo: ${gameInfoCount}`);
              // console.log(event.data);
              // console.log(event.data.cells[0][0].state === CellState.UNOPENED);
              expect(event.data.cells[0][0].state).toBe(CellState.UNOPENED);
              open(0, 0);
              break;
            case 2:
              expect(event.data.cells[0][0].state).toBe(CellState.OPENED);
              done();
              break;
            default:
              throw new Error(`unhandled case`);
          }
          break;
        default:
          throw new Error(`unhandled case`);
      }
    });
  });