Skip to content

Commit

Permalink
Insert new participant in sorted order (front-end)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasnaphas committed Mar 29, 2022
1 parent 78fca98 commit d3bcd1c
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 24 deletions.
57 changes: 34 additions & 23 deletions frontend/src/components/RosterPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,39 @@ class RosterPage extends Component {
}
});
};
messageHandler = (event) => {
if (!event) {
return;
}
if (!event.data) {
return;
}
const eventData = JSON.parse(event.data);
if (!eventData) {
return;
}
if (!eventData.newParticipant) {
return;
}
this.setState((state, props) => {
if (state.participants.includes(eventData.newParticipant)) {
return;
}
return {
participants: state.participants
.concat([eventData.newParticipant])
.sort((a, b) => {
if (new String(a).toLowerCase() < new String(b).toLowerCase())
return -1;
if (new String(a).toLowerCase() > new String(b).toLowerCase())
return 1;
if (a < b) return -1;
if (a > b) return 1;
return 0;
}),
};
});
};
componentDidMount() {
this._isMounted = true;
const {
Expand Down Expand Up @@ -114,29 +147,7 @@ class RosterPage extends Component {
`roomcode=${roomCode}&` +
`gamename=${encodeURIComponent(gameName)}`
);
webSocket.addEventListener("message", (event) => {
if (!event) {
return;
}
if (!event.data) {
return;
}
const eventData = JSON.parse(event.data);
if (!eventData) {
return;
}
if (!eventData.newParticipant) {
return;
}
this.setState((state, props) => {
if (state.participants.includes(eventData.newParticipant)) {
return;
}
return {
participants: state.participants.concat([eventData.newParticipant]),
};
});
});
webSocket.addEventListener("message", this.messageHandler);
}
componentWillUnmount() {
if (webSocket && webSocket.close) {
Expand Down
82 changes: 81 additions & 1 deletion frontend/src/components/RosterPage.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import RosterPage from "./RosterPage";
import React from "react";
import { MemoryRouter } from "react-router-dom";
import { findByText, render, screen, waitFor } from "@testing-library/react";
import {
findByText,
getAllByRole,
render,
screen,
waitFor,
} from "@testing-library/react";
import "@testing-library/jest-dom";
import userEvent from "@testing-library/user-event";
import { ThemeProvider, createTheme } from "@mui/material/styles";
Expand Down Expand Up @@ -292,4 +298,78 @@ describe("RosterPage", () => {
expect(mockWebSocket.addEventListener).toHaveBeenCalled();
await findByText(table, "Je Teste");
});
// new participant shows up in the right order
test("New participant shows up in the right order", async () => {
let mockWebSocketConstructorCalls = 0;
let messageEventHandler;
const mockWebSocket = {
addEventListener: jest.fn((eventType, cb) => {
messageEventHandler = cb;
}),
};

class MockWebSocket {
constructor(server, protocol) {
mockWebSocketConstructorCalls++;
// expect server to be correct
expect(server).toEqual(
`wss://${window.location.hostname}/ws/?` +
`roomcode=${confirmedRoomCode}&` +
`gamename=${encodeURIComponent(confirmedGameName)}`
);
return mockWebSocket;
}
}
global.WebSocket = MockWebSocket;

const confirmedRoomCode = "TESTIN";
const confirmedGameName = "car";
function* participantsGenerator() {
yield ["ABx", "car", "castle", "Kastle", "Lill", "munoria"];
}
const pGen = participantsGenerator();
const roster = jest.fn(async () => {
return { status: 200, data: { participants: pGen.next().value } };
});
const closeSeder = jest.fn();
const setConfirmedRoomCode = jest.fn();
const setConfirmedGameName = jest.fn();
const setChosenPath = jest.fn();
render(
<ThemeProvider theme={theme}>
<MemoryRouter>
<RosterPage
confirmedRoomCode={confirmedRoomCode}
confirmedGameName={confirmedGameName}
roster={roster}
closeSeder={closeSeder}
setConfirmedGameName={setConfirmedGameName}
setConfirmedRoomCode={setConfirmedRoomCode}
setChosenPath={setChosenPath}
></RosterPage>
</MemoryRouter>
</ThemeProvider>
);
expect(mockWebSocketConstructorCalls).toEqual(1);
const testMessage = new MessageEvent("message", {
data: `{"newParticipant":"Draw"}`,
});
let table = await screen.findByRole("table");
await findByText(table, "ABx");
expect(roster).toHaveBeenCalledTimes(1);
messageEventHandler(testMessage);
expect(mockWebSocket.addEventListener).toHaveBeenCalled();
await findByText(table, "car");
const cells = screen.getAllByRole("cell");
expect(cells.map((cell) => cell.innerHTML)).toEqual([
"ABx",
"car",
"castle",
"Draw",
"Kastle",
"Lill",
"munoria",
]);
});
// removeEventListener is called
});

0 comments on commit d3bcd1c

Please sign in to comment.