Skip to content

Commit

Permalink
Added testing but 404 failing
Browse files Browse the repository at this point in the history
  • Loading branch information
7emansell committed Oct 23, 2023
1 parent de0defd commit d195d51
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
33 changes: 27 additions & 6 deletions __tests__/[slug].test.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
import slugHandler from "@/pages/api/lanes";
import { NextApiRequest } from 'next';
import { NextApiRequest } from "next";

it('should respond with 200 status and lanes data for a GET request', () => {
it("should respond with 200 status and lanes data for a GET request", () => {
const request: NextApiRequest = {
method: 'GET',
method: "GET",
query: {
slug: "maps",
}
},
} as unknown as NextApiRequest;

const mockResponse: any = {
status: jest.fn(function () {
return this;
return this;
}),
json: jest.fn(),
};

slugHandler(request, mockResponse);

expect(mockResponse.status).toHaveBeenCalledWith(200);
})
});

it("should respond with an error for nonexistent slugs", () => {
const request: NextApiRequest = {
method: "GET",
query: {
slug: "not-a-slug",
},
} as unknown as NextApiRequest;

const mockResponse: any = {
status: jest.fn(function () {
return this;
}),
json: jest.fn(),
};

slugHandler(request, mockResponse);

expect(mockResponse.status).toHaveBeenCalledWith(404);
expect(mockResponse.json).toHaveBeenCalledWith({ error: "Lane not found" });
});
2 changes: 0 additions & 2 deletions src/pages/api/lanes/[slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ export default async function slugHandler(
response: NextApiResponse
) {
const { method, query } = request;

const slug = query.slug as string;

const slugData = lanesData.lanes.find((lane) => lane[slug] !== undefined);

if (method === "GET") {
Expand Down

0 comments on commit d195d51

Please sign in to comment.