Skip to content

solution#2

Open
davidbinneun wants to merge 1 commit intomainfrom
solution
Open

solution#2
davidbinneun wants to merge 1 commit intomainfrom
solution

Conversation

@davidbinneun
Copy link
Copy Markdown
Owner

No description provided.

@@ -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';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

अति उत्कृष्ट

await fs.writeFile("./backend/testdata.json", "[]");
});

describe("Sending URL to the server", () => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants