Skip to content

Commit

Permalink
fix: update test to validate random quote selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor-Palha committed Oct 28, 2024
1 parent 2abc2e3 commit 6fc6c76
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
62 changes: 47 additions & 15 deletions backend/controllers/quote.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,51 @@ describe('quote controller', () => {
});

it("/quotes/random should return a random quote", async () => {
const fakeQuote = {
_id: '5cd96e05de30eff6ebccedfc',
dialog: 'Tomatoes, sausages, nice crispy bacon',
movie: '5cd95395de30eff6ebccde5c',
character: '5cd99d4bde30eff6ebccfc7c',
id: '5cd96e05de30eff6ebccedfc'
};

mockingoose(QuoteModel).toReturn(fakeQuote, 'findOne');

const response = await request(app).get('/v2/quotes/random');

expect(response.statusCode).toEqual(HttpCode.OK);
expect(response.body).toEqual(fakeQuote);
});
const fakeQuotes = [
{
_id: '5cd96e05de30eff6ebccedfc',
dialog: 'Tomatoes, sausages, nice crispy bacon',
movie: '5cd95395de30eff6ebccde5c',
character: '5cd99d4bde30eff6ebccfc7c',
id: '5cd96e05de30eff6ebccedfc'
},
{
_id: '5cd96e05de30eff6ebcce99c',
dialog: 'Sam, no!',
movie: '5cd95395de30eff6ebccde5d',
character: '5cd99d4bde30eff6ebccfc15',
id: '5cd96e05de30eff6ebcce99c'
},
{
_id: "5cd96e05de30eff6ebcce89a",
dialog: "DEATH!",
movie: "5cd95395de30eff6ebccde5d",
character: "5cdbe49b7ed9587226e794a0",
id: "5cd96e05de30eff6ebcce89a"
},
{
_id: "5cd96e05de30eff6ebcce8cd",
dialog: "You'll see. Oh yes, you will see.",
movie: "5cd95395de30eff6ebccde5d",
character: "5cd99d4bde30eff6ebccfe9e",
id: "5cd96e05de30eff6ebcce8cd"
},
{
_id: "5cd96e05de30eff6ebcced9d",
dialog: "What business does an Elf, a Man and a Dwarf have in the Riddermark? Speak quickly!",
movie: "5cd95395de30eff6ebccde5b",
character: "5cdbdecb6dc0baeae48cfa5a",
id: "5cd96e05de30eff6ebcced9d"
}
];

mockingoose(QuoteModel).toReturn(fakeQuotes.length, 'estimatedDocumentCount');
mockingoose(QuoteModel).toReturn(fakeQuotes, 'find');

const response = await request(app).get('/v2/quotes/random');

expect(response.statusCode).toEqual(HttpCode.OK);
expect(fakeQuotes).toContainEqual(response.body);
});

});
7 changes: 4 additions & 3 deletions backend/controllers/quote.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ export const quoteController = {
}

const randomIndex = Math.floor(Math.random() * count);
console.log(randomIndex);
const quote = await QuoteModel.findOne().skip(randomIndex);


const quotes = await QuoteModel.find();
const quote = quotes[randomIndex];

return res.json(quote);
} catch (error) {
return next(error);
Expand Down

0 comments on commit 6fc6c76

Please sign in to comment.