From e6922660eef213b32ee0f3e02d453efe7deccaaa Mon Sep 17 00:00:00 2001 From: Mister-Mario Date: Sun, 28 Apr 2024 11:15:46 +0200 Subject: [PATCH] Added tests for url construction --- .../ForgetPassword/ForgetPassword.test.js | 12 +++++ .../questionView/QuestionGenerator.test.js | 10 +++++ .../ranking/RankingRetriever.test.js | 44 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 webapp/src/components/ranking/RankingRetriever.test.js diff --git a/webapp/src/components/ForgetPassword/ForgetPassword.test.js b/webapp/src/components/ForgetPassword/ForgetPassword.test.js index 1a5b12d..7f7eef5 100644 --- a/webapp/src/components/ForgetPassword/ForgetPassword.test.js +++ b/webapp/src/components/ForgetPassword/ForgetPassword.test.js @@ -130,6 +130,18 @@ describe('ForgetPassword Component', () => { await expect(forgetPasswordFunctions.changePassword('test@example.com', 'testuser', 'newpassword', 'newpassword')).rejects.toThrow('Failed to change password'); }); }); + + describe('checkUrl', () => { + it('should have base url', async () => { + expect(forgetPasswordFunctions.apiUrl).toEqual('http://localhost:8000'); + }); + + it('should have env variable url', async () => { + process.env.REACT_APP_API_ENDPOINT = 'test'; + forgetPasswordFunctions = new ForgetPasswordFunctions(); + expect(forgetPasswordFunctions.apiUrl).toEqual('test'); + }); + }); }); }); diff --git a/webapp/src/components/questionView/QuestionGenerator.test.js b/webapp/src/components/questionView/QuestionGenerator.test.js index 9764f98..d08fb0e 100644 --- a/webapp/src/components/questionView/QuestionGenerator.test.js +++ b/webapp/src/components/questionView/QuestionGenerator.test.js @@ -66,4 +66,14 @@ describe('QuestionGenerator', () => { expect(questions.length).toBe(1); expect(questions[0].question).toBe("What is the population of Oviedo?"); }); + + it('should have the base url', () => { + expect(questionGenerator.apiUrl).toEqual('http://localhost:8000/questions') + }) + + it('should not have the base url', () => { + process.env.REACT_APP_API_ENDPOINT = 'test'; + questionGenerator = new QuestionGenerator(); + expect(questionGenerator.apiUrl).toEqual('test/questions') + }) }); diff --git a/webapp/src/components/ranking/RankingRetriever.test.js b/webapp/src/components/ranking/RankingRetriever.test.js new file mode 100644 index 0000000..b4c7857 --- /dev/null +++ b/webapp/src/components/ranking/RankingRetriever.test.js @@ -0,0 +1,44 @@ +import axios from 'axios'; +import RankingRetriever from './RankingRetriever'; // Ruta a tu archivo RankingRetriever + +jest.mock('axios'); // Simula las llamadas a Axios + +describe('RankingRetriever Tests', () => { + let rankingRetriever; + + beforeEach(() => { + rankingRetriever = new RankingRetriever(); + }); + + test('Throws error if endpoint is not set for getTopTen', async () => { + axios.get.mockRejectedValue(new Error('Endpoint not found')); // Simula error + + try { + await rankingRetriever.getTopTen('dummy-token'); + } catch (error) { + expect(error).toBeInstanceOf(Error); // Verifica que se lanzó un error + expect(error.message).toContain('Endpoint not found'); // Verifica el mensaje del error + } + }); + + test('Throws error if endpoint is not set for getUser', async () => { + axios.get.mockRejectedValue(new Error('Endpoint not found')); // Simula error + + try { + await rankingRetriever.getUser('dummy-user', 'dummy-token'); + } catch (error) { + expect(error).toBeInstanceOf(Error); // Verifica que se lanzó un error + expect(error.message).toContain('Endpoint not found'); // Verifica el mensaje del error + } + }); + + test('should have the base url', () => { + expect(rankingRetriever.apiUrl).toEqual('http://localhost:8000/record/ranking') + }) + + test('should not have the base url', () => { + process.env.REACT_APP_API_ENDPOINT = 'test'; + rankingRetriever = new RankingRetriever(); + expect(rankingRetriever.apiUrl).toEqual('test/record/ranking') + }) +});