From 71eb95a27bbc03fdce22d6a7409af4627c1ef3ee Mon Sep 17 00:00:00 2001 From: Vaughn Clayton Date: Sat, 24 Oct 2020 17:09:27 -0400 Subject: [PATCH] Fixes #69 problem with deleteLobby --- server/__tests__/lobbies.test.js | 25 +++++++++++++++++++++++++ server/lobbies.js | 6 +++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/server/__tests__/lobbies.test.js b/server/__tests__/lobbies.test.js index 8520411..61c7e0c 100644 --- a/server/__tests__/lobbies.test.js +++ b/server/__tests__/lobbies.test.js @@ -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(); diff --git a/server/lobbies.js b/server/lobbies.js index 47fbf98..c1b2941 100644 --- a/server/lobbies.js +++ b/server/lobbies.js @@ -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; }