Skip to content
Snippets Groups Projects
Commit 2bef3ee1 authored by Jonny Ngo Luong's avatar Jonny Ngo Luong
Browse files

test: add test for category filtering on post (#6)

parent f0e8b3dc
No related branches found
No related tags found
1 merge request!9Resolve "Organiser/Filtrere funksjon etter kategori"
......@@ -41,13 +41,11 @@ router.route("/").post(async (request: Request, response: Response) => {
// Get all posts `/api/post/?categoryid=`
router.route("/").get(async (request: Request, response: Response) => {
const { categoryid } = request.query as { [key: string]: string };
console.log(categoryid);
try {
let input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.name, p.imageUrl
FROM post as p
INNER JOIN category ON category.categoryid = p.categoryid`;
if (categoryid) input += ` WHERE p.categoryid=${categoryid}`;
console.log(input);
response.status(200).json(await query(input, ""));
} catch (error) {
response.status(400).send("Bad Request");
......
import request from 'supertest';
import app from '../../../app';
import request from "supertest";
import app from "../../../app";
describe('Test postController', () => {
beforeAll(async () => { // kjører før testing
console.log("Post controller test starting...");
});
describe("Test postController", () => {
beforeAll(async () => {
// kjører før testing
console.log("Post controller test starting...");
});
afterAll(async () => { // kjører når all testing er gått gjennom
console.log("...Test ending");
});
afterAll(async () => {
// kjører når all testing er gått gjennom
console.log("...Test ending");
});
it('Request /api/post should return request of 200!', async () => {
const result = await request(app)
.get('/api/post')
.send()
it("Request /api/post should return request of 200!", async () => {
const result = await request(app).get("/api/post").send();
expect(result.status).toBe(200);
});
expect(result.status).toBe(200);
});
it('Request /api/post/1 should return data with name "test"!', async () => {
const result = await request(app).get('/api/post/1').send();
it('Request /api/post/1 should return data with name "test"!', async () => {
const result = await request(app).get("/api/post/1").send();
expect(result.status).toBe(200);
expect(result.body.data[0]?.title).toBe('test');
});
expect(result.status).toBe(200);
expect(result.body.data[0]?.title).toBe("test");
});
it('Request /api/post/?categoryid=1 should return datas with categoryname = "Antikviteter og Kunst"', async () => {
const result = await request(app).get("/api/post/1").send();
expect(result.status).toBe(200);
expect(result.body.data[0]?.name).toBe("Antikviteter og Kunst");
});
});
});
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment