Open
Conversation
listguy
reviewed
Mar 9, 2021
| @@ -0,0 +1,62 @@ | |||
| const fs = require('fs').promises; | |||
| const Item = require('./item.js'); | |||
| const databaseFile = process.env.NODE_ENV === 'test' ? './backend/testdata.json':'./backend/data.json'; | |||
| await fs.writeFile("./backend/testdata.json", "[]"); | ||
| }); | ||
|
|
||
| describe("Sending URL to the server", () => { |
There was a problem hiding this comment.
Missing a test for posting an existing url
Comment on lines
+11
to
+68
| describe("Sending URL to the server", () => { | ||
| it("The URL is legal", async () => { | ||
| const response = await request.post('/api/shorturl/new').type('form').send({url:"https://www.youtube.com/"}); | ||
| expect(response.status).toBe(201); | ||
| }); | ||
| it("The URL is illegal", async () => { | ||
| const response = await request.post('/api/shorturl/new').type('form').send({url:"utubecom/?hl=iw&gl=IL"}); | ||
| expect(response.status).toBe(400); | ||
| }); | ||
| it("Corrupted file in server", async () => { | ||
| await fs.writeFile("./backend/testdata.json", "["); | ||
| const response = await request.post('/api/shorturl/new').type('form').send({url:"https://www.youtube.com/"}); | ||
| expect(response.status).toBe(500); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Redirect to URL by ID", () => { | ||
| it("Send Legal ID", async () => { | ||
| let newItem = new Item('https://www.youtube.com/'); | ||
| await fs.writeFile("./backend/testdata.json", JSON.stringify([newItem])); | ||
| const response = await request.get(`/api/statistic/${newItem.id}`); | ||
| expect(response.status).toBe(200); | ||
| }); | ||
| it("Send Illegal ID", async () => { | ||
| const response = await request.get(`/api/statistic/abc`); | ||
| expect(response.status).toBe(400); | ||
| }); | ||
| it("Legal ID but ID not found", async () => { | ||
| const response = await request.get(`/api/statistic/abcdefg`); | ||
| expect(response.status).toBe(404); | ||
| }); | ||
| it("Corrupted file in server", async () => { | ||
| await fs.writeFile("./backend/testdata.json", "["); | ||
| const response = await request.get(`/api/statistic/abcdefg`); | ||
| expect(response.status).toBe(500); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Get statistics by ID", () => { | ||
| it("Send Legal ID", async () => { | ||
| let newItem = new Item('https://www.youtube.com/'); | ||
| await fs.writeFile("./backend/testdata.json", JSON.stringify([newItem])); | ||
| const response = await request.get(`/${newItem.id}`); | ||
| expect(response.status).toBe(302); | ||
| }); | ||
| it("Send Illegal ID", async () => { | ||
| const response = await request.get(`/abc`); | ||
| expect(response.status).toBe(400); | ||
| }); | ||
| it("Legal ID but ID not found", async () => { | ||
| const response = await request.get(`/abcdefg`); | ||
| expect(response.status).toBe(404); | ||
| }); | ||
| it("Corrupted file in server", async () => { | ||
| await fs.writeFile("./backend/testdata.json", "["); | ||
| const response = await request.get(`/abcdefg`); | ||
| expect(response.status).toBe(500); | ||
| }); |
There was a problem hiding this comment.
Good job and good tests. Covering almost every case.
However naming is a bit confusing. We usually write what we expect the server to do in each case, what we are about to test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.