-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Oleksandr week 2 Node JS #18
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import app from "../app.js"; | ||
import supertest from "supertest"; | ||
import { API_KEY } from "../sources/keys.js"; | ||
import nock from "nock"; | ||
const request = supertest(app); | ||
|
||
describe("POST /", (done) => { | ||
afterEach(() => { | ||
nock.cleanAll(); | ||
}); | ||
|
||
it("App returns 200 status on the main page.", async () => { | ||
await request | ||
.get("/") | ||
.expect(200) | ||
.then((res) => { | ||
expect(res.text).toBe("hello from backend to frontend!"); | ||
}); | ||
}); | ||
|
||
it("App returns 200 status when the city name is correct.", async () => { | ||
const mockCity = "Amsterdam"; | ||
const mockTemp = 15; | ||
|
||
nock("https://api.openweathermap.org") | ||
.get("/data/2.5/weather") | ||
.query({ q: mockCity, APPID: API_KEY, units: "metric" }) | ||
.reply(200, { name: "Amsterdam", main: { temp: 15 } }); | ||
|
||
const res = await request | ||
.post("/weather") | ||
.send({ cityName: "Amsterdam" }) | ||
.expect(200); | ||
|
||
expect(res.body).toEqual({ | ||
Amsterdam: 15, | ||
}); | ||
}); | ||
|
||
it("App returns 400 status when the city name is missing.", async () => { | ||
await request | ||
.post("/weather") | ||
.send() | ||
.set("Content-type", "application/json") | ||
.set("Accept", "application/json") | ||
.expect(400, { | ||
weatherText: "City name is required!", | ||
}); | ||
}); | ||
|
||
it("App returns 404 status when the city name is gibberish.", async () => { | ||
nock("https://api.openweathermap.org") | ||
.get("/data/2.5/weather") | ||
.query({ q: "abcdefg", APPID: API_KEY, units: "metric" }) | ||
.reply(404, { message: "city not found" }); | ||
|
||
await request | ||
.post("/weather") | ||
.send({ cityName: "abcdefg" }) | ||
.set("Content-type", "application/json") | ||
.set("Accept", "application/json") | ||
.expect(404, { | ||
weatherText: "City is not found!", | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import express from "express"; | ||
import fetch from "node-fetch"; | ||
import { API_KEY } from "./sources/keys.js"; | ||
|
||
const app = express(); | ||
|
||
app.use(express.json()); | ||
const baseUrl = "https://api.openweathermap.org/data/2.5/weather"; | ||
|
||
app.get("/", (req, res) => { | ||
res.send("hello from backend to frontend!"); | ||
}); | ||
|
||
app.post("/weather", async (req, res) => { | ||
if (!req.body.cityName) { | ||
return res.status(400).send({ weatherText: "City name is required!" }); | ||
} | ||
|
||
const cityName = req.body.cityName; | ||
const url = `${baseUrl}?q=${cityName}&APPID=${API_KEY}&units=metric`; | ||
|
||
const response = await fetch(url); | ||
|
||
if (!response.ok) { | ||
return res.status(404).send({ weatherText: "City is not found!" }); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
res.status(200).send({ [data.name]: data.main.temp }); | ||
}); | ||
|
||
export default app; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = { | ||
presets: [ | ||
[ | ||
// This is a configuration, here we are telling babel what configuration to use | ||
"@babel/preset-env", | ||
{ | ||
targets: { | ||
node: "current", | ||
}, | ||
}, | ||
], | ||
], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
// Tells jest that any file that has 2 .'s in it and ends with either js or jsx should be run through the babel-jest transformer | ||
transform: { | ||
"^.+\\.jsx?$": "babel-jest", | ||
}, | ||
// By default our `node_modules` folder is ignored by jest, this tells jest to transform those as well | ||
transformIgnorePatterns: [], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "node", | ||
"version": "1.0.0", | ||
"description": "> If you are following the HackYourFuture curriculum we recommend you to start with module 1: [HTML/CSS/GIT](https://github.com/HackYourFuture/HTML-CSS). To get a complete overview of the HackYourFuture curriculum first, click [here](https://github.com/HackYourFuture/curriculum).", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "jest", | ||
"start": "nodemon server.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.21.0", | ||
"express-handlebars": "^8.0.1", | ||
"nock": "^13.5.5", | ||
"node-fetch": "^3.3.2", | ||
"nodemon": "^3.1.7" | ||
}, | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.25.4", | ||
"babel-jest": "^29.7.0", | ||
"jest": "^29.7.0", | ||
"supertest": "^7.0.0" | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note the section 3.2.1. in the project instructions. When running the Jest tests, you import the Can you think of a way of breaking out only the code necessary to start the server? You should end up with two files:
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import app from "./app.js"; | ||
|
||
const PORT = 3004; | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}`); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice that you are using a mocking library!