Skip to content

Commit e24f4a3

Browse files
authored
Merge pull request #9 from SchoolOfCode/extra-routes-and-testing
Extra routes and testing
2 parents 3077787 + e5fbba8 commit e24f4a3

File tree

6 files changed

+265
-12
lines changed

6 files changed

+265
-12
lines changed

app.test.js

Lines changed: 213 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,219 @@
11
import request from "supertest";
2+
import { jest } from "@jest/globals";
3+
// import supertest from "supertest";
24

35
import app from "./app.js";
46

5-
describe("GET /users", function () {
6-
test("gives us back 200, with a message", async function () {
7-
const expectedBody = {
8-
message: "I wish we had some information to give you ☹️"
9-
};
10-
const actual = await request(app).get("/users");
11-
expect(actual.body).toStrictEqual(expectedBody);
12-
expect(actual.statusCode).toBe(200);
7+
jest.setTimeout(10000);
8+
//JOINCLEAN ROUTES
9+
//Test joinclean routes GET, POST, PUT, DELETE
10+
describe("Tests for /joinclean route", () => {
11+
describe("Tests for GET /joinclean", () => {
12+
test("GET /joinclean returns 200 status code", async () => {
13+
let res = await request(app).get("/joinclean");
14+
expect(res.statusCode).toEqual(200);
15+
});
16+
17+
test("GET /joinclean returns json content", async () => {
18+
let res = await request(app).get("/joinclean");
19+
expect(res.headers["content-type"]).toEqual(
20+
expect.stringContaining("json")
21+
);
22+
});
23+
test("GET /joinclean returns an object", async () => {
24+
let res = await request(app).get("/joinclean");
25+
expect(res._body).toEqual(
26+
expect.objectContaining({
27+
success: true,
28+
payload: expect.any(Array),
29+
})
30+
);
31+
});
32+
});
33+
34+
describe("Tests for POST /joinclean", () => {
35+
test("POST /joinclean returns a 200 status code", async () => {
36+
const res = await request(app).post("/joinclean").send({
37+
name: "Any name",
38+
comments: "A comment",
39+
});
40+
expect(res.statusCode).toEqual(200);
41+
});
42+
test("POST /joinclean method is POST", async () => {
43+
const res = await request(app).post("/joinclean").send({
44+
name: "Any name",
45+
comments: "A comment",
46+
});
47+
expect(res.req.method).toEqual("POST");
48+
});
49+
test("POST /joinclean object is to /joinclean", async () => {
50+
const res = await request(app).post("/joinclean").send({
51+
name: "Any name",
52+
comments: "A comment",
53+
});
54+
expect(res.req.path).toEqual("/joinclean");
55+
});
56+
});
57+
58+
// console.log("this is the raw headers ", res.req);
59+
// expect(res.req.method).toEqual("POST");
60+
// expect(res.req.path).toEqual("/joinclean");
61+
// expect(res.rawHeaders).toEqual(
62+
// expect.arrayContaining(expect.stringContaining("json"))
63+
// );
64+
// expect(res._data).toEqual(
65+
// objectContaining({ name: "Any name", comments: "A comment" })
66+
// );
67+
describe("Tests for PUT /joinclean", () => {
68+
test("PUT /joinclean returns a 200 status code", async () => {
69+
const res = await request(app).put("/joinclean/1").send({
70+
name: "Any name",
71+
comments: "A comment",
72+
});
73+
//console.log(res.body.payload)
74+
expect(res.statusCode).toEqual(200);
75+
});
76+
});
77+
describe("Tests for DELETE /joinclean", () => {
78+
test("DELETE /joinclean returns the correct status code", async () => {
79+
const res = await request(app).delete("/joinclean/1");
80+
expect(res.statusCode).toEqual(200);
81+
});
82+
});
83+
});
84+
85+
86+
//LOGCLEAN ROUTES
87+
// can write tests for GET, POST, PUT
88+
describe("Tests for /logclean route", () => {
89+
describe("Tests for GET /logclean ", () => {
90+
test("GET /logclean returns a 200 status code", async () => {
91+
let res = await request(app).get("/logclean");
92+
expect(res.statusCode).toEqual(200);
93+
});
94+
test("GET /logclean returns json content", async () => {
95+
let res = await request(app).get("/logclean");
96+
expect(res.headers["content-type"]).toEqual(
97+
expect.stringContaining("json")
98+
);
99+
});
100+
test("GET /logclean returns an object", async () => {
101+
let res = await request(app).get("/logclean");
102+
expect(res._body).toEqual(
103+
expect.objectContaining({
104+
success: true,
105+
payload: expect.any(Array),
106+
})
107+
);
108+
});
109+
});
110+
describe("Tests for POST /logclean ", () => {
111+
test("POST /logclean returns a 200 status code", async () => {
112+
const res = await request(app).post("/logclean").send({
113+
cleanname: "Test Clean",
114+
bags: 1,
115+
volunteers: 1,
116+
});
117+
expect(res.statusCode).toEqual(200);
118+
});
119+
test("POST /logclean method is POST", async () => {
120+
const res = await request(app).post("/logclean").send({
121+
cleanname: "Test Clean",
122+
bags: 1,
123+
volunteers: 1,
124+
});
125+
expect(res.req.method).toEqual("POST");
126+
});
127+
test("POST /logclean object is to /logclean", async () => {
128+
const res = await request(app).post("/logclean").send({
129+
cleanname: "Test Clean",
130+
bags: 1,
131+
volunteers: 1,
132+
});
133+
expect(res.req.path).toEqual("/logclean");
134+
});
135+
});
136+
describe("Tests for PUT /logclean ", () => {
137+
test("PUT /logclean returns a 200 status code", async () => {
138+
const res = await request(app).put("/logclean/1").send({
139+
cleanname: "Updated Test Clean",
140+
bags: 2,
141+
volunteers: 2,
142+
});
143+
expect(res.statusCode).toEqual(200);
144+
});
145+
});
146+
});
147+
148+
149+
// STARTCLEAN ROUTES
150+
//can write tests for GET, POST
151+
describe("Tests for /startclean route", () => {
152+
describe("Tests for GET /logclean ", () => {
153+
test("GET /startclean returns 200 status code", async () => {
154+
let res = await request(app).get("/startclean");
155+
156+
expect(res.statusCode).toEqual(200);
157+
});
158+
test("GET /startclean returns json content", async () => {
159+
let res = await request(app).get("/startclean");
160+
expect(res.headers["content-type"]).toEqual(
161+
expect.stringContaining("json")
162+
);
163+
});
164+
test("GET /startclean returns an object", async () => {
165+
let res = await request(app).get("/startclean");
166+
expect(res._body).toEqual(
167+
expect.objectContaining({
168+
success: true,
169+
payload: expect.any(Array),
170+
})
171+
);
172+
});
173+
});
174+
175+
describe("Tests for POST /logclean ", () => {
176+
test("POST /startclean returns a 200 status code and confirmation of an object being posted", async () => {
177+
const res = await request(app).post("/startclean").send({
178+
cleanName: "New Clean Name",
179+
location: "Some Location",
180+
date: "22.09.22",
181+
startTime: "12:00",
182+
endTime: "13:00",
183+
host: "Some Host",
184+
notes: "A Note",
185+
latitude: 74.87603594928817,
186+
longitude: -40.301351746354406,
187+
});
188+
expect(res.statusCode).toEqual(200);
189+
});
190+
test("POST /startclean method is POST", async () => {
191+
const res = await request(app).post("/startclean").send({
192+
cleanName: "New Clean Name",
193+
location: "Some Location",
194+
date: "22.09.22",
195+
startTime: "12:00",
196+
endTime: "13:00",
197+
host: "Some Host",
198+
notes: "A Note",
199+
latitude: 74.87603594928817,
200+
longitude: -40.301351746354406,
201+
});
202+
expect(res.req.method).toEqual("POST");
203+
});
204+
test("POST /startclean object is to /startclean", async () => {
205+
const res = await request(app).post("/startclean").send({
206+
cleanName: "New Clean Name",
207+
location: "Some Location",
208+
date: "22.09.22",
209+
startTime: "12:00",
210+
endTime: "13:00",
211+
host: "Some Host",
212+
notes: "A Note",
213+
latitude: 74.87603594928817,
214+
longitude: -40.301351746354406,
215+
});
216+
expect(res.req.path).toEqual("/startclean");
217+
});
13218
});
14219
});

models/joinClean.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,21 @@ export async function postJoinCleanup(body) {
1919
[name, comments]
2020
);
2121
return (data)
22-
}
22+
}
23+
24+
//PUT
25+
export async function updEvent(id, data){
26+
const sqlString = `UPDATE joinClean SET name = $1 , comments = $2 WHERE id = $3 RETURNING *;`
27+
const result = await query(sqlString,[data.name, data.comments, id]);
28+
return result
29+
}
30+
31+
//Delete
32+
export async function deleteById(id){
33+
const sqlString = `DELETE FROM joinclean WHERE id=$1 RETURNING *;`
34+
const result = await query(sqlString,[id]);
35+
return result
36+
}
37+
38+
39+

models/logClean.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ export async function postLogCleanup(body) {
3535
return (data)
3636
}
3737

38+
//Update
39+
export async function updateLogCleanup(id , data){
40+
const sqlString = `UPDATE log SET cleanname = $1 , bags = $2 ,volunteers =$3 WHERE id = $4 RETURNING *;`
41+
const result = await query(sqlString,[data.cleanname, data.bags, data.volunteers, id]);
42+
return result
43+
}
44+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"db:resetLogTable": "npm run db:dropLogTable && npm run db:createLogTable && npm run db:populateLogTable",
1919
"db:resetJoinTable": "npm run db:dropJoinTable && npm run db:createJoinTable && npm run db:populateJoinTable",
2020
"dev": "nodemon -r dotenv/config ./bin/www.js",
21-
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
21+
"test": "node --experimental-vm-modules -r dotenv/config node_modules/jest/bin/jest.js"
2222
},
2323
"dependencies": {
2424
"cookie-parser": "~1.4.4",

routes/joinClean.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from "express";
22
const router = express.Router();
3-
import {getJoinClean, postJoinCleanup} from "../models/joinClean.js"
3+
import {getJoinClean, postJoinCleanup,updEvent,deleteById} from "../models/joinClean.js"
44

55

66
//Route to GET all entries in Log a clean table
@@ -17,4 +17,20 @@ router.post("/", async function (req, res) {
1717
res.status(200).json({success:true, payload: result.rows});
1818
});
1919

20+
21+
//Put
22+
router.put('/:id',async(req,res)=>{
23+
const id = Number(req.params.id);
24+
const data = req.body;
25+
const result = await updEvent(id, data)
26+
res.json({success: true, payload: result.rows });
27+
});
28+
29+
//Delete
30+
router.delete("/:id", async (req, res) => {
31+
const id = req.params.id
32+
const result = await deleteById(id)
33+
res.json({success: true, payload: result.rows })
34+
});
35+
2036
export default router;

routes/logClean.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from "express";
22
const router = express.Router();
3-
import {getLogClean, postLogCleanup, getAllVolunteers, getAllBags} from "../models/logClean.js"
3+
import {getLogClean, postLogCleanup, getAllVolunteers, getAllBags,updateLogCleanup} from "../models/logClean.js"
44

55

66
//Route to GET all entries in Log a clean table
@@ -29,4 +29,12 @@ router.post("/", async function (req, res) {
2929
res.status(200).json({success:true, payload: result.rows});
3030
});
3131

32+
//Update
33+
router.put('/:id', async function(req,res){
34+
const id= Number(req.params.id);
35+
const data = req.body;
36+
const result = await updateLogCleanup(id, data)
37+
res.json({success: true, payload: result.rows });
38+
})
39+
3240
export default router;

0 commit comments

Comments
 (0)