From bb9721fee1996152e231631a04d0a2ad4f9e2bd4 Mon Sep 17 00:00:00 2001 From: uo283182 Date: Mon, 22 Apr 2024 14:45:01 +0200 Subject: [PATCH 1/5] tests de homescreem y test de app cambiado nombre --- webapp/src/App.test.js | 2 +- webapp/src/components/HomeScreen.js | 6 +-- webapp/src/components/HomeScreen.test.js | 48 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 webapp/src/components/HomeScreen.test.js diff --git a/webapp/src/App.test.js b/webapp/src/App.test.js index 384c5148..5024eb7e 100644 --- a/webapp/src/App.test.js +++ b/webapp/src/App.test.js @@ -4,7 +4,7 @@ import App from './App'; describe('App component', () => { - it('renders learn react link',async () => { + it('should show the nav var and the logout button',async () => { render( diff --git a/webapp/src/components/HomeScreen.js b/webapp/src/components/HomeScreen.js index 9d94754b..025df265 100644 --- a/webapp/src/components/HomeScreen.js +++ b/webapp/src/components/HomeScreen.js @@ -22,10 +22,7 @@ const HomeScreen = () => { } } - useEffect(() => { - checkUserLogin(); - document.getElementById('btLogout').style.display = 'inline-block'; - }, []) + const handleChange = (event, newValue) => { @@ -45,6 +42,7 @@ const HomeScreen = () => { color="info" inputProps={{ + "data-testid": "name-input", min: 1, max: 30, type: 'number', diff --git a/webapp/src/components/HomeScreen.test.js b/webapp/src/components/HomeScreen.test.js new file mode 100644 index 00000000..4e8011bf --- /dev/null +++ b/webapp/src/components/HomeScreen.test.js @@ -0,0 +1,48 @@ +import React from 'react'; +import { render, fireEvent, screen , waitFor, cleanup } from '@testing-library/react'; +import { BrowserRouter} from 'react-router-dom'; +import HomeScreen from './HomeScreen'; + + +describe('HomeScreen component', () => { + beforeEach(() => { + localStorage.clear(); + cleanup(); + }); + +//TEST 1 - Test introduciendo un número de preguntas y dar al boton jugar, se muestra game + it('should show the components to start the game', async () => { + render( + + + + ); + + const input = await screen.findByTestId("name-input"); + const slider = screen.getByRole('slider') + fireEvent.change(slider, {target: {value: '20'}}) + expect(slider.value).toBe("20") + expect(input.value).toEqual("20"); + + const jugarButton = screen.getByText('Jugar'); + fireEvent.click(jugarButton); + + expect(localStorage.getItem('numQuestions')).toBe("20"); + + expect(window.location.pathname).toBe('/game'); + + }); + + //TEST 2 - Test dar al botón ver historial, y te lleva al historial + it('should handle error when logging in', async () => { + render( + + + + ); + + const historialButton = screen.getByText('Ver historial'); + fireEvent.click(historialButton); + + }); +}); \ No newline at end of file From 5c9782fb54febe17a5d605e5e9f60bbdf3fa8a0a Mon Sep 17 00:00:00 2001 From: uo283182 Date: Mon, 22 Apr 2024 14:55:25 +0200 Subject: [PATCH 2/5] =?UTF-8?q?A=C3=B1adidos=20test=20de=20AddUser.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webapp/src/components/AddUser.test.js | 80 +++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/webapp/src/components/AddUser.test.js b/webapp/src/components/AddUser.test.js index 827d8df5..d97b78ce 100644 --- a/webapp/src/components/AddUser.test.js +++ b/webapp/src/components/AddUser.test.js @@ -65,4 +65,84 @@ describe('AddUser component', () => { expect(screen.getByText(/Error: Internal Server Error/i)).toBeInTheDocument(); }); }); + + it('should handle error when adding user with empty username', async () => { + render( + + + + ); + + const usernameInput = screen.getByLabelText(/Username/i); + const passwordInput = screen.getByLabelText(/Password/i); + const addUserButton = screen.getByRole('button', { name: /Add User/i }); + + // Mock the axios.post request to simulate an error response + mockAxios.onPost('http://localhost:8000/adduser').reply(400, { error: 'El nombre de usuario no puede estar vacío' }); + + // Simulate user input + fireEvent.change(usernameInput, { target: { value: ' ' } }); + fireEvent.change(passwordInput, { target: { value: 'testPassword' } }); + + // Trigger the add user button click + fireEvent.click(addUserButton); + + // Wait for the error Snackbar to be open + await waitFor(() => { + expect(screen.getByText(/Error: El nombre de usuario no puede estar vacío/i)).toBeInTheDocument(); + }); + }); + it('should handle error when adding user with empty password', async () => { + render( + + + + ); + + const usernameInput = screen.getByLabelText(/Username/i); + const passwordInput = screen.getByLabelText(/Password/i); + const addUserButton = screen.getByRole('button', { name: /Add User/i }); + + // Mock the axios.post request to simulate an error response + mockAxios.onPost('http://localhost:8000/adduser').reply(400, { error: 'La contraseña no puede estar vacía' }); + + // Simulate user input + fireEvent.change(usernameInput, { target: { value: 'testUser' } }); + fireEvent.change(passwordInput, { target: { value: ' ' } }); + + // Trigger the add user button click + fireEvent.click(addUserButton); + + // Wait for the error Snackbar to be open + await waitFor(() => { + expect(screen.getByText(/Error: La contraseña no puede estar vacía/i)).toBeInTheDocument(); + }); + }); + + it('try to add user with username existing', async () => { + render( + + + + ); + + const usernameInput = screen.getByLabelText(/Username/i); + const passwordInput = screen.getByLabelText(/Password/i); + const addUserButton = screen.getByRole('button', { name: /Add User/i }); + + // Mock the axios.post request to simulate an error response + mockAxios.onPost('http://localhost:8000/adduser').reply(400, { error: 'Este nombre de usuario está en uso' }); + + // Simulate user input + fireEvent.change(usernameInput, { target: { value: 'testUser' } }); + fireEvent.change(passwordInput, { target: { value: 'testPass' } }); + + // Trigger the add user button click + fireEvent.click(addUserButton); + + // Wait for the Snackbar to be open + await waitFor(() => { + expect(screen.getByText(/Error: Este nombre de usuario está en uso/i)).toBeInTheDocument(); + }); + }); }); From 37d117a4d60509c2b52e0b8027596533ed22f9d1 Mon Sep 17 00:00:00 2001 From: uo283182 Date: Mon, 22 Apr 2024 20:38:43 +0200 Subject: [PATCH 3/5] test arreglado --- webapp/src/components/HomeScreen.js | 11 +++++++++-- webapp/src/components/HomeScreen.test.js | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/webapp/src/components/HomeScreen.js b/webapp/src/components/HomeScreen.js index afea03dd..52d6e07c 100644 --- a/webapp/src/components/HomeScreen.js +++ b/webapp/src/components/HomeScreen.js @@ -6,7 +6,7 @@ import { useNavigate } from "react-router-dom"; const HomeScreen = () => { const [value, setValue] = useState(15); const navigate = useNavigate(); - const token = localStorage.getItem('token'); + const handleStartButtonClick = () => { localStorage.setItem('numQuestions', value); @@ -17,12 +17,19 @@ const HomeScreen = () => { navigate("/history"); } const checkUserLogin = () => { + const token = localStorage.getItem('token'); if (token==null) { navigate("/"); } } - +useEffect(() => { + checkUserLogin(); + const logoutButton = document.getElementById('btLogout'); + if (logoutButton) { + logoutButton.style.display = 'inline-block'; + } + }, []) const handleChange = (event, newValue) => { diff --git a/webapp/src/components/HomeScreen.test.js b/webapp/src/components/HomeScreen.test.js index 4e8011bf..9d585d63 100644 --- a/webapp/src/components/HomeScreen.test.js +++ b/webapp/src/components/HomeScreen.test.js @@ -12,12 +12,13 @@ describe('HomeScreen component', () => { //TEST 1 - Test introduciendo un número de preguntas y dar al boton jugar, se muestra game it('should show the components to start the game', async () => { + localStorage.setItem('token', "t"); render( ); - + const input = await screen.findByTestId("name-input"); const slider = screen.getByRole('slider') fireEvent.change(slider, {target: {value: '20'}}) From 6186be34e1b2c9ccb766322380d8d6917e6262c3 Mon Sep 17 00:00:00 2001 From: uo283182 Date: Mon, 22 Apr 2024 21:17:58 +0200 Subject: [PATCH 4/5] Tests de Welcome.js --- webapp/src/components/Welcome.test.js | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 webapp/src/components/Welcome.test.js diff --git a/webapp/src/components/Welcome.test.js b/webapp/src/components/Welcome.test.js new file mode 100644 index 00000000..4c80e7c7 --- /dev/null +++ b/webapp/src/components/Welcome.test.js @@ -0,0 +1,42 @@ +import { render, screen, fireEvent } from '@testing-library/react'; +import { BrowserRouter} from 'react-router-dom'; +import Welcome from './Welcome'; + +describe('Welcome Component', () => { + + it('should render the welcome message', () => { + render( + + + + ); + const welcomeMessage = screen.getByText(/Tu juego favorito de televisión, ¡ahora en tu ordenador!/i); + expect(welcomeMessage).toBeInTheDocument(); + const welcomeMessage2 = screen.getByText(/Accede a tu cuenta o regístrate para comenzar/i); + expect(welcomeMessage2).toBeInTheDocument(); + + }); + + it('should navigate to login page when login button is clicked', () => { + render( + + + + ); + const loginButton = screen.getByRole('button', { name: /login/i }); + fireEvent.click(loginButton); + expect(window.location.pathname).toBe('/login'); + }); + + it('should navigate to signup page when signup button is clicked', () => { + render( + + + + ); + const signupButton = screen.getByRole('button', { name: /signup/i }); + fireEvent.click(signupButton); + expect(window.location.pathname).toBe('/adduser'); + }); + +}); \ No newline at end of file From 40a9005f135a32dfc01226d1a74f7424f22072f3 Mon Sep 17 00:00:00 2001 From: uo283182 Date: Tue, 23 Apr 2024 00:25:46 +0200 Subject: [PATCH 5/5] Tests de History.js --- webapp/package-lock.json | 173 ++++++++++++++++++++++++++ webapp/package.json | 3 +- webapp/src/components/History.test.js | 78 ++++++++++++ 3 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 webapp/src/components/History.test.js diff --git a/webapp/package-lock.json b/webapp/package-lock.json index 94d91b5f..6b4fc2b8 100644 --- a/webapp/package-lock.json +++ b/webapp/package-lock.json @@ -17,6 +17,7 @@ "axios": "^1.6.5", "bootstrap": "^5.3.3", "history": "^5.3.0", + "jsonwebtoken": "^9.0.2", "jwt-decode": "^4.0.0", "react": "^18.2.0", "react-dom": "^18.2.0", @@ -9542,6 +9543,11 @@ "node": "*" } }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==" + }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -11567,6 +11573,14 @@ "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", "integrity": "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==" }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -20721,6 +20735,27 @@ "node": ">=0.10.0" } }, + "node_modules/jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "dependencies": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + }, + "engines": { + "node": ">=12", + "npm": ">=6" + } + }, "node_modules/jsx-ast-utils": { "version": "3.3.5", "resolved": "https://registry.npmjs.org/jsx-ast-utils/-/jsx-ast-utils-3.3.5.tgz", @@ -20735,6 +20770,25 @@ "node": ">=4.0" } }, + "node_modules/jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "dependencies": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "node_modules/jwt-decode": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz", @@ -20909,6 +20963,36 @@ "resolved": "https://registry.npmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz", "integrity": "sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==" }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==" + }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==" + }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==" + }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==" + }, + "node_modules/lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==" + }, "node_modules/lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -20919,6 +21003,11 @@ "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==" }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "node_modules/lodash.sortby": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", @@ -38474,6 +38563,11 @@ "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", "dev": true }, + "buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==" + }, "buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -39938,6 +40032,14 @@ "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", "integrity": "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==" }, + "ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "requires": { + "safe-buffer": "^5.0.1" + } + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -46828,6 +46930,23 @@ "resolved": "https://registry.npmjs.org/jsonpointer/-/jsonpointer-5.0.1.tgz", "integrity": "sha512-p/nXbhSEcu3pZRdkW1OfJhpsVtW1gd4Wa1fnQc9YLiTfAjn0312eMKimbdIQzuZl9aa9xUGaRlP9T/CJE/ditQ==" }, + "jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "requires": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + } + }, "jsx-ast-utils": { "version": "3.3.5", "resolved": "https://registry.npmjs.org/jsx-ast-utils/-/jsx-ast-utils-3.3.5.tgz", @@ -46839,6 +46958,25 @@ "object.values": "^1.1.6" } }, + "jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "requires": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "jwt-decode": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz", @@ -46976,6 +47114,36 @@ "resolved": "https://registry.npmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz", "integrity": "sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==" }, + "lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==" + }, + "lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==" + }, + "lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==" + }, + "lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==" + }, + "lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==" + }, + "lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==" + }, "lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -46986,6 +47154,11 @@ "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==" }, + "lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "lodash.sortby": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", diff --git a/webapp/package.json b/webapp/package.json index a6faac33..6777b5eb 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -12,6 +12,7 @@ "axios": "^1.6.5", "bootstrap": "^5.3.3", "history": "^5.3.0", + "jsonwebtoken": "^9.0.2", "jwt-decode": "^4.0.0", "react": "^18.2.0", "react-dom": "^18.2.0", @@ -58,4 +59,4 @@ "serve": "^14.2.1", "start-server-and-test": "^2.0.3" } -} \ No newline at end of file +} diff --git a/webapp/src/components/History.test.js b/webapp/src/components/History.test.js new file mode 100644 index 00000000..81b128f1 --- /dev/null +++ b/webapp/src/components/History.test.js @@ -0,0 +1,78 @@ +import React from 'react'; +import { render,fireEvent, screen,cleanup, waitFor } from '@testing-library/react'; +import History from './History'; +import { BrowserRouter } from 'react-router-dom'; +import axios from 'axios'; +import MockAdapter from 'axios-mock-adapter'; +const mockAxios = new MockAdapter(axios); +const jwt = require('jsonwebtoken'); + +describe('History Component', () => { + beforeEach(() => { + localStorage.clear(); + mockAxios.reset(); + cleanup(); + }); + + it('renders history table correctly', async () => { + + const token = jwt.sign({username: "testUsername" }, 'secret-key'); + localStorage.setItem('token', token); + const mockHistoryData = [ + { + _id: '1', + correctQuestions: 8, + totalQuestions: 10, + totalTime: 120, + doneAt: '2022-01-01T00:00:00Z', + }, + { + _id: '2', + correctQuestions: 6, + totalQuestions: 12, + totalTime: 90, + doneAt: '2022-01-02T00:00:00Z', + }, + ]; + + mockAxios.onPost('http://localhost:8000/getRecords').reply(200, mockHistoryData); + + render( + + + + ); + + await waitFor(() => { + expect(screen.getByText('Historial de partidas')).toBeInTheDocument(); + expect(screen.getByText('Preguntas acertadas')).toBeInTheDocument(); + expect(screen.getByText('Nº preguntas')).toBeInTheDocument(); + expect(screen.getByText('Tiempo total')).toBeInTheDocument(); + expect(screen.getByText('Fecha')).toBeInTheDocument(); + + expect(screen.getByText('8')).toBeInTheDocument(); + expect(screen.getByText('10')).toBeInTheDocument(); + expect(screen.getByText('120 segundos')).toBeInTheDocument(); + expect(screen.getByText('1/1/2022, 1:00:00')).toBeInTheDocument(); + + expect(screen.getByText('6')).toBeInTheDocument(); + expect(screen.getByText('12')).toBeInTheDocument(); + expect(screen.getByText('90 segundos')).toBeInTheDocument(); + expect(screen.getByText('2/1/2022, 1:00:00')).toBeInTheDocument(); + }); + }); + + it('button back goes to home', async () => { + render( + + + + ); + + const backButton = screen.getByRole('button', { name: /Volver/i }); + fireEvent.click(backButton); + await waitFor(() => { + expect(window.location.pathname).toBe('/home'); + }); + }); +});