Skip to content

Commit

Permalink
Merge pull request #10 from Firebird1029/backend-tests
Browse files Browse the repository at this point in the history
Add jest tests for backend
  • Loading branch information
rcheng11 authored Feb 26, 2024
2 parents 70623fc + 88c5224 commit f0bd393
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions __tests__/backend.t.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const request = require("supertest");
const axios = require("axios");
const app = require("../server/server");

jest.mock("axios");

describe("Express App Tests", () => {
afterEach(() => {
jest.resetAllMocks();
});

describe("GET /api", () => {
it("should respond with JSON message", async () => {
const response = await request(app).get("/api");

expect(response.statusCode).toBe(200);
expect(response.body.message).toBe("Hello from server!");
});
});

describe("GET /getUserProfile", () => {
it("should respond with user profile when valid token is provided", async () => {
axios.get.mockResolvedValue({ data: { uri: "some-uri" } });

const response = await request(app).get(
"/getUserProfile?token=valid-token",
);

expect(response.statusCode).toBe(200);
expect(response.body.profile).toBeDefined();
});

it("should respond with 400 when no token is provided", async () => {
const response = await request(app).get("/getUserProfile");

expect(response.statusCode).toBe(400);
});

it("should respond with 500 when error occurs", async () => {
const response = await request(app).get(
"/getTopItems?token=invalid-token",
);
expect(response.statusCode).toBe(500);
});
});

describe("GET /getTopItems", () => {
it("should respond with top items when valid token and type are provided", async () => {
axios.get.mockResolvedValue({ data: { items: ["item1", "item2"] } });

const response = await request(app).get(
"/getTopItems?token=valid-token&type=artists",
);

expect(response.statusCode).toBe(200);
expect(response.body.topItems).toBeDefined();
});

it("should respond with 400 when no token is provided", async () => {
const response = await request(app).get("/getTopItems?type=artists");

expect(response.statusCode).toBe(400);
});

it("should respond with 500 when error occurs", async () => {
const response = await request(app).get(
"/getTopItems?token=invalid-token&type=artists",
);

expect(response.statusCode).toBe(500);
});
});
});

0 comments on commit f0bd393

Please sign in to comment.