Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #69 problem with deleteLobby #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions server/__tests__/lobbies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ describe("Lobby actions", () => {
expect(getAllLobbies()).toHaveLength(0); // did delete 15min after player addition
});

it("should only delete one lobby", () => {
const lobbyData1 = {
id: "firstLobbyDataInHere",
players: ["2VDBVQ-6MD2F1rWJXdgyBodBp0YjAY88"],
code: "1111",
};
const lobby1 = addLobby(lobbyData1);
expect(getAllLobbies()).toHaveLength(1);

jest.advanceTimersByTime(LOBBY_LIFESPAN / 2);
const lobbyData2 = {
id: "secondLobbyDataInHere",
players: ["2VDBVQ-6MD2F1rWJXdgyBodBp0YjAY88"],
code: "2222",
};
const lobby2 = addLobby(lobbyData2);
expect(getAllLobbies()).toHaveLength(2);

jest.advanceTimersByTime(LOBBY_LIFESPAN / 2);

expect(getAllLobbies()).toHaveLength(1);
expect(getAllLobbies()).not.toContain(lobby1);
expect(getAllLobbies()).toContain(lobby2); // deleted lobby1 but not lobby2
});

afterEach(() => {
jest.runOnlyPendingTimers();
jest.clearAllMocks();
Expand Down
6 changes: 3 additions & 3 deletions server/lobbies.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export const addLobby = (lobbyData) => {

export const deleteLobby = (id) => {
console.log("Deleting Lobby", id);
const lobby = lobbies.find((item) => item.id === id);
if (lobby) {
lobbies.splice(lobby);
const lobby = lobbies.findIndex((item) => item.id === id);
if (lobby > -1) {
lobbies.splice(lobby, 1);
save(lobbies);
return true;
}
Expand Down