Skip to content

Commit

Permalink
test: add problemDetails test code
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Jul 30, 2024
1 parent 94c042f commit 202c0ca
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
71 changes: 71 additions & 0 deletions __tests__/unit/models/problemDetails.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { expect, test } from "vitest";
import {
isProblemDetails,
ProblemDetails
} from "../../../src/models/problemDetails";

test("should return true when value is a ProblemDetails object", () => {
const value: ProblemDetails = {
title: "Internal Server Error",
status: 500,
detail: "An unexpected error occurred.",
instance: "/api/users",
errors: ["500", "Internal Server Error"]
};
const result = isProblemDetails(value);

expect(result).toBe(true);
});

test("should return false when value is not an object", () => {
const value = "Invalid input";
const result = isProblemDetails(value);

expect(result).toBe(false);
});

test("should return false when value is null", () => {
const value = null;
const result = isProblemDetails(value);

expect(result).toBe(false);
});

test("should return false when status is not a number", () => {
const value = {
title: "Bad Request",
status: "400",
detail: "Invalid request.",
instance: "/api/users",
errors: ["400", "Bad Request"]
};
const result = isProblemDetails(value);

expect(result).toBe(false);
});

test("should return true when errors is an array", () => {
const value: ProblemDetails = {
title: "Not Found",
status: 404,
detail: "Resource not found.",
instance: "/api/users",
errors: ["", ""]
};
const result = isProblemDetails(value);

expect(result).toBe(true);
});

test("should return false when errors is not an array", () => {
const value = {
title: "Unauthorized",
status: 401,
detail: "Access denied.",
instance: "/api/users",
errors: "401"
};
const result = isProblemDetails(value);

expect(result).toBe(false);
});
1 change: 0 additions & 1 deletion src/models/problemDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface ProblemDetails {
errors: [code: string, message: string];
}

// TODO: Write test code
export function isProblemDetails(value: unknown): value is ProblemDetails {
if (typeof value !== "object" || value === null) {
return false;
Expand Down

0 comments on commit 202c0ca

Please sign in to comment.