|
| 1 | +import type { CanonicalSpec } from "@patchlogr/types"; |
| 2 | +import { describe, expect, test } from "vitest"; |
| 3 | +import { partitionByMethod } from "../partitionByMethod"; |
| 4 | + |
| 5 | +describe("partitionByMethod", () => { |
| 6 | + test("should group by first tag", () => { |
| 7 | + const spec: CanonicalSpec = { |
| 8 | + operations: { |
| 9 | + "GET /user": { |
| 10 | + key: "GET /user", |
| 11 | + doc: { tags: ["user"] }, |
| 12 | + method: "GET", |
| 13 | + path: "/user", |
| 14 | + request: { params: [] }, |
| 15 | + responses: {}, |
| 16 | + }, |
| 17 | + "GET /user/{userId}": { |
| 18 | + key: "GET /user/{userId}", |
| 19 | + doc: { tags: ["user"] }, |
| 20 | + method: "GET", |
| 21 | + path: "/user/{userId}", |
| 22 | + request: { params: [] }, |
| 23 | + responses: {}, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }; |
| 27 | + |
| 28 | + const partitions = partitionByMethod(spec).partitions; |
| 29 | + expect(partitions).toHaveLength(1); |
| 30 | + expect(partitions.get("GET")).toHaveLength(2); |
| 31 | + expect(partitions.get("GET")?.[0]?.operationKey).toBe("GET /user"); |
| 32 | + expect(partitions.get("GET")?.[1]?.operationKey).toBe( |
| 33 | + "GET /user/{userId}", |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + test("should group by multiple tags", () => { |
| 38 | + const spec: CanonicalSpec = { |
| 39 | + operations: { |
| 40 | + "GET /user": { |
| 41 | + key: "GET /user", |
| 42 | + doc: { tags: ["user"] }, |
| 43 | + method: "GET", |
| 44 | + path: "/user", |
| 45 | + request: { params: [] }, |
| 46 | + responses: {}, |
| 47 | + }, |
| 48 | + "POST /auth/login": { |
| 49 | + key: "POST /auth/login", |
| 50 | + doc: { tags: ["auth"] }, |
| 51 | + method: "POST", |
| 52 | + path: "/auth/login", |
| 53 | + request: { params: [] }, |
| 54 | + responses: {}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + const partitions = partitionByMethod(spec).partitions; |
| 60 | + |
| 61 | + expect(partitions).toHaveLength(2); |
| 62 | + expect(partitions.get("GET")).toHaveLength(1); |
| 63 | + expect(partitions.get("POST")).toHaveLength(1); |
| 64 | + expect(partitions.get("GET")?.[0]?.operationKey).toBe("GET /user"); |
| 65 | + expect(partitions.get("POST")?.[0]?.operationKey).toBe( |
| 66 | + "POST /auth/login", |
| 67 | + ); |
| 68 | + }); |
| 69 | +}); |
0 commit comments