Skip to content

Commit

Permalink
added more tests for the coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
uo289029 committed Apr 27, 2024
1 parent 95a30eb commit 32996e0
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 11 deletions.
4 changes: 2 additions & 2 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ describe('Gateway Service', () => {
{
_id: "6626bd80de07476e84fe74da",
username: "maria2",
password: "$2b$10$GnDbxj5LffsFTh1JGUCm.OYmZwe8P.KklXEd38EA5liukCUeDpIPa",
password: "$2b$10$GnDbxj5LffsFTh1JGUCm.OYmZwe8P.KklXEd38EA5liukCUeDpIPa", //NOSONAR
createdAt: "2024-04-22T19:41:52.245+00:00",
__v: 0
},
{
_id: "6626bazf0df55711esdfe74da",
username: "maria",
password: "$2b$10$Gnawesrthyj4uyfhgdd54634Cm.OYmZwe8P.KklXEd38EA5liukCUeDpIPa",
password: "$2b$10$Gnawesrthyj4uyfhgdd54634Cm.OYmZwe8P.KklXEd38EA5liukCUeDpIPa", //NOSONAR
createdAt: "2024-03-19T19:41:52.245+00:00",
__v: 0
}
Expand Down
10 changes: 7 additions & 3 deletions questionsgenerator/questions-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ const QuestionGenerator = require('./questionGenerator.js');

let mongoServer;
let app;
//jest.mock('axios');
jest.mock('axios');

beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();
process.env.MONGODB_URI = mongoUri;
app = require('./questions-service');
//mongoServer = await MongoMemoryServer.create();
//const mongoUri = mongoServer.getUri();
//process.env.MONGODB_URI = mongoUri;
Expand All @@ -32,8 +36,8 @@ beforeAll(async () => {
});

afterAll(async () => {
//app.close();
//await mongoServer.stop();
app.close();
await mongoServer.stop();
});

describe('Questions Service', () => {
Expand Down
7 changes: 6 additions & 1 deletion record/historial-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ app.post('/saveGameRecord', async (req, res) => {

const username = user.username;

console.log(username);
console.log(gameQuestions);
console.log(gameQuestions[username]);

if (!gameQuestions[username]) {
return res.status(400).json({ error: "No game questions found for this user" });
}
Expand All @@ -59,6 +63,8 @@ app.post('/saveGameRecord', async (req, res) => {
correctAnswers: correctAnswers,
questions: gameQuestions[username]
});

console.log(game);

// Guarda el juego en la base de datos
const gameResult = await recordRepo.saveGame(game);
Expand All @@ -77,7 +83,6 @@ app.post('/saveGameRecord', async (req, res) => {
app.get('/getGameRecord', async (req, res) => {
try {
const user = req.query.user;
console.log(user);

const games = await recordRepo.getGameRecord(user);

Expand Down
34 changes: 30 additions & 4 deletions record/historial-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ beforeAll(async () => {
isCorrect: true
}]
}]);

});

afterAll(async () => {
Expand All @@ -64,7 +65,7 @@ afterAll(async () => {
});

describe('Historial Service', () => {
it('should get a new question on POST /saveQuestion', async () => {
it('should save a question on POST /saveQuestion', async () => {
const question = "Example question";
const answersArray = ["Answer 1", "Answer 2", "Answer 3", "Answer 4"];
const correctAnswer = 0;
Expand All @@ -85,11 +86,36 @@ describe('Historial Service', () => {
expect(response.body).toHaveProperty('msg', 'Question saved successfully');
});

it('should get a new question on POST /saveGameRecord', async () => {
it('should give error as there is no record to save on POST /saveGameRecord', async () => {
const username = "Example username";
const response = await request(app).post('/saveGameRecord').send({ user: { username: 'someUsername' } });
//guarda el juego en la base de datos
expect(response.status).toBe(400);
expect(response.body).toHaveProperty('error', 'No game questions found for this user');
});

it('should return error while validating the game on POST /saveGameRecord', async () => {
const question = "Example question";
const answersArray = ["Answer 1", "Answer 2", "Answer 3", "Answer 4"];
const correctAnswer = 0;
const selectedAnswer = 1;
const isCorrect = correctAnswer === selectedAnswer;
const username = "Example username";
const response = await request(app).post('/saveGameRecord').send({ username });

let response = await request(app).post('/saveQuestion').send({
question,
answersArray,
correctAnswer,
selectedAnswer,
isCorrect,
username
});

response = await request(app).post('/saveGameRecord').send({ user: { username: 'Example username' } });
//guarda el juego en la base de datos

expect(response.status).toBe(400);
expect(response.body).toHaveProperty('error', 'Error validating the game');

});

it('should get a new question on GET /getGameRecord', async () => {
Expand Down
37 changes: 37 additions & 0 deletions webapp/src/components/Home.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ describe('Home component', () => {
);

// Make sure all the buttons are rendered
expect(screen.getByText('New Full Random Game')).toBeInTheDocument();
expect(screen.getByText('New Art Gallery Game')).toBeInTheDocument();
expect(screen.getByText('New Human Calculator Game')).toBeInTheDocument();
expect(screen.getByText('Play by Category')).toBeInTheDocument();
});

Expand All @@ -83,5 +86,39 @@ describe('Home component', () => {
expect(await screen.findByText('New Geography Game')).toBeInTheDocument();
expect(await screen.findByText('New Science Game')).toBeInTheDocument();
});

it('should render the category buttons when clicking the play by category', async () => {
const history = createMemoryHistory();
render(
<Router history={history}>
<Home />
</Router>
);

// Click the 'Play by Category' button
fireEvent.click(screen.getByText('New Art Gallery Game'));

// Make sure the information is rendered
expect(await screen.findByText('How to Play')).toBeInTheDocument();
expect(await screen.findByText('Guess the artwork of the artist.')).toBeInTheDocument();
expect(await screen.findByText('Start Game')).toBeInTheDocument();
});

it('should render the category buttons when clicking the play by category', async () => {
const history = createMemoryHistory();
render(
<Router history={history}>
<Home />
</Router>
);

// Click the 'Play by Category' button
fireEvent.click(screen.getByText('New Human Calculator Game'));

// Make sure the information is rendered
expect(await screen.findByText('How to Play')).toBeInTheDocument();
expect(await screen.findByText('Solve the math problems as fast as you can.')).toBeInTheDocument();
expect(await screen.findByText('Start Game')).toBeInTheDocument();
});

});
2 changes: 1 addition & 1 deletion webapp/src/components/UserProfile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('UserProfile component', () => {
mockAxios.onGet(`http://localhost:8000/getUserByUsername`, { params: { username: 'expectedUsername' } }).reply(200, {
user: {
createdAt: "2024-04-22T20:11:53.053Z",
password: "$2b$10$m6RxpAY0yd23plXLXWn0cOUNTObjrpbsoPlLvFBwJk3VTdbwzxg92",
password: "$2b$10$m6RxpAY0yd23plXLXWn0cOUNTObjrpbsoPlLvFBwJk3VTdbwzxg92", //NOSONAR
username: "mery2",
__v: 0,
_id: "6626c489de07476e84fe74f2"
Expand Down

0 comments on commit 32996e0

Please sign in to comment.