Step 21: Test GET with query parameters
Update tests/routes/bookmarks.js
by updating beforeEach
operation as follows:
beforeEach(() => {
bookmarkDao.deleteAll();
const cutoff = 2;
for (let index = 0; index < cutoff; index++) {
bookmarkDao.create({
title: "Fake title",
url: `url-${index}`,
});
}
for (let index = cutoff; index < numBookmarks; index++) {
bookmarkDao.create({
title: faker.lorem.sentence(),
url: faker.internet.url(),
});
}
});
Next add these tests:
describe("GET with query parameters", async () => {
it("GET all bookmarks given title", async () => {
const title = "Fake title";
const response = await request.get(`/bookmarks?title=${title}`);
expect(response.status).toBe(200);
expect(response.body.data.length).toBe(2);
});
it("GET all bookmarks given URL", async () => {
const url = "url-0";
const response = await request.get(`/bookmarks?url=${url}`);
expect(response.status).toBe(200);
expect(response.body.data.length).toBe(1);
});
});
Run the tests in this file.
Save and commit all changes.