Skip to content

Instantly share code, notes, and snippets.

@sharad-s
Created August 24, 2018 19:02
Show Gist options
  • Save sharad-s/d015c77d83d3929dabd4f9312666998b to your computer and use it in GitHub Desktop.
Save sharad-s/d015c77d83d3929dabd4f9312666998b to your computer and use it in GitHub Desktop.
const request = require("supertest");
const { Tag } = require("../../models/tag");
const { Track } = require("../../models/track");
let server;
describe("/api/tracks", () => {
beforeEach(() => {
// Run the express server
server = require("../../index");
});
afterEach(async () => {
await server.close();
await Tag.remove({});
await Track.remove({});
});
describe("GET /", () => {
it("should return all tracks", async () => {
await Track.collection.insertMany([
{
name: "Track01",
ipfsHash: "IPFS_HASH01",
imageHash: "IMAGE_HASH01",
tags: ["tag01"]
},
{
name: "Track02",
ipfsHash: "IPFS_HASH02",
imageHash: "IMAGE_HASH02",
tags: ["tag02"]
},
{
name: "Track03",
ipfsHash: "IPFS_HASH03",
imageHash: "IMAGE_HASH03",
tags: ["tag03"]
}
]);
const res = await request(server).get("/api/tracks");
console.log(res.body);
expect(res.status).toBe(200);
expect(res.body.length).toBe(3);
expect(res.body.some(t => t.name === "Track01")).toBeTruthy();
expect(res.body.some(t => t.name === "Track02")).toBeTruthy();
expect(res.body.some(t => t.name === "Track03")).toBeTruthy();
});
});
});
/*
describe("GET /:id", async () => {
it("should return a tag if a valid id is passed", async () => {
const tag = new Tag({ name: "tag1" });
await tag.save();
const res = await request(server).get("/api/tags/" + tag._id);
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("name", tag.name); //expect name to have tag.name
});
it("should return 404 if an invalid id is passed", async () => {
const res = await request(server).get("/api/tags/2");
expect(res.status).toBe(404);
});
});
*/
/*
describe("POST /", async () => {
// let token;
let name;
// Standard code for making a POST request
const exec = async () => {
return await request(server)
.post("/api/tags/")
.send({ name });
};
// Happy Path
beforeEach(() => {
// token = new User().generateAuthToken();
name = "tag1";
});
it("should return 400 if tag is less than 3 characters", async () => {
name = "12";
const res = await exec();
expect(res.status).toBe(400);
});
it("should return 400 if tag is more than 15 characters", async () => {
name = new Array(20).join("a");
const res = await exec();
expect(res.status).toBe(400);
});
// Happy Path
it("should save the tag if it is valid", async () => {
await exec();
const tag = await Tag.find({ name: "tag1" });
expect(tag).not.toBeNull();
});
// Happy Path
it("should return the tag in the response if valid", async () => {
const res = await exec();
expect(res.body).toHaveProperty("_id");
expect(res.body).toHaveProperty("name", "tag1");
});
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment