From 5fa0a89d8b26904b626104a1bd06280dd6ca433b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20M=C3=A9ndez=20Murias?= Date: Sun, 7 Apr 2024 23:42:54 +0200 Subject: [PATCH] Post game tests updated --- webapp/src/test/PostGame.test.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/webapp/src/test/PostGame.test.js b/webapp/src/test/PostGame.test.js index 71381ed0..450bd230 100644 --- a/webapp/src/test/PostGame.test.js +++ b/webapp/src/test/PostGame.test.js @@ -119,4 +119,36 @@ describe('PostGame component', () => { // Check if the snackbar is closed expect(screen.queryByText('Game saved successfully')).not.toBeInTheDocument(); }); + test('displays error snackbar correctly', async () => { + // Mock the SessionContext value + const sessionData = { + userId: 'mockedUserId', + token: 'mockedToken' + }; + // Mock the axios post function to throw an error + axios.post.mockRejectedValue(new Error('Mock error')); + render( + + + + ); + // Check if saveGame function is called correctly + expect(axios.post).toHaveBeenCalledWith( + 'http://localhost:8000/addgame', + { + user: sessionData.userId, + pAcertadas: localStorage.getItem('pAcertadas'), + pFalladas: localStorage.getItem('pFalladas'), + totalTime: localStorage.getItem('tiempoUsado'), + gameMode: undefined, + }, + { + headers: { + Authorization: `Bearer ${sessionData.token}` + } + } + ); + // Check if the error snackbar is displayed + expect(await screen.findByText('Error adding game')).toBeInTheDocument(); + }); }); \ No newline at end of file