diff --git a/test/get-operation-id.test.ts b/test/get-operation-id.test.ts new file mode 100644 index 00000000000..73e65228cfe --- /dev/null +++ b/test/get-operation-id.test.ts @@ -0,0 +1,36 @@ +import { getOperationId } from "../app/utils"; + +describe("getOperationId", () => { + test("prefers an explicit operationId when provided", () => { + expect( + getOperationId({ + operationId: "listPets", + method: "get", + path: "/pets", + }), + ).toBe("listPets"); + }); + + test("derives an id from method and path when operationId is missing", () => { + expect( + getOperationId({ + method: "get", + path: "/pets/list", + }), + ).toBe("GET_pets_list"); + }); + + test("uppercases the method and replaces every slash in the path", () => { + expect( + getOperationId({ + method: "post", + path: "/v1/users/create", + }), + ).toBe("POST_v1_users_create"); + }); + + test("produces an id matching the allowed pattern ^[a-zA-Z0-9_-]+$", () => { + const id = getOperationId({ method: "delete", path: "/a/b/c" }); + expect(id).toMatch(/^[a-zA-Z0-9_-]+$/); + }); +});