|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { sanitizeNameForGraphQL } from "../../src/lib/type-utils.js"; |
| 2 | +import { |
| 3 | + getSingleNameWithNamespace, |
| 4 | + sanitizeNameForGraphQL, |
| 5 | + toEnumMemberName, |
| 6 | + toFieldName, |
| 7 | + toTypeName, |
| 8 | +} from "../../src/lib/type-utils.js"; |
3 | 9 |
|
4 | | -describe("sanitizeNameForGraphQL", () => { |
5 | | - it("replaces special characters with underscores", () => { |
6 | | - expect(sanitizeNameForGraphQL("$Money$")).toBe("_Money_"); |
7 | | - expect(sanitizeNameForGraphQL("My-Name")).toBe("My_Name"); |
8 | | - expect(sanitizeNameForGraphQL("Hello.World")).toBe("Hello_World"); |
9 | | - }); |
| 10 | +describe("type-utils", () => { |
| 11 | + describe("sanitizeNameForGraphQL", () => { |
| 12 | + it("replaces special characters with underscores", () => { |
| 13 | + expect(sanitizeNameForGraphQL("$Money$")).toBe("_Money_"); |
| 14 | + expect(sanitizeNameForGraphQL("My-Name")).toBe("My_Name"); |
| 15 | + expect(sanitizeNameForGraphQL("Hello.World")).toBe("Hello_World"); |
| 16 | + }); |
10 | 17 |
|
11 | | - it("replaces [] with Array", () => { |
12 | | - expect(sanitizeNameForGraphQL("Item[]")).toBe("ItemArray"); |
13 | | - }); |
| 18 | + it("replaces [] with Array", () => { |
| 19 | + expect(sanitizeNameForGraphQL("Item[]")).toBe("ItemArray"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("leaves valid names unchanged", () => { |
| 23 | + expect(sanitizeNameForGraphQL("ValidName")).toBe("ValidName"); |
| 24 | + expect(sanitizeNameForGraphQL("_underscore")).toBe("_underscore"); |
| 25 | + expect(sanitizeNameForGraphQL("name123")).toBe("name123"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("adds prefix for names starting with numbers", () => { |
| 29 | + expect(sanitizeNameForGraphQL("123Name")).toBe("_123Name"); |
| 30 | + expect(sanitizeNameForGraphQL("1")).toBe("_1"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("handles multiple special characters", () => { |
| 34 | + expect(sanitizeNameForGraphQL("$My-Special.Name$")).toBe("_My_Special_Name_"); |
| 35 | + }); |
14 | 36 |
|
15 | | - it("leaves valid names unchanged", () => { |
16 | | - expect(sanitizeNameForGraphQL("ValidName")).toBe("ValidName"); |
17 | | - expect(sanitizeNameForGraphQL("_underscore")).toBe("_underscore"); |
18 | | - expect(sanitizeNameForGraphQL("name123")).toBe("name123"); |
| 37 | + it("handles empty prefix parameter", () => { |
| 38 | + expect(sanitizeNameForGraphQL("123Name", "")).toBe("_123Name"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("uses custom prefix for invalid starting character", () => { |
| 42 | + expect(sanitizeNameForGraphQL("123Name", "Num")).toBe("Num_123Name"); |
| 43 | + }); |
19 | 44 | }); |
20 | 45 |
|
21 | | - it("adds prefix for names starting with numbers", () => { |
22 | | - expect(sanitizeNameForGraphQL("123Name")).toBe("_123Name"); |
23 | | - expect(sanitizeNameForGraphQL("1")).toBe("_1"); |
| 46 | + describe("toTypeName", () => { |
| 47 | + it("converts to PascalCase", () => { |
| 48 | + expect(toTypeName("my_name")).toBe("MyName"); |
| 49 | + expect(toTypeName("some-value")).toBe("SomeValue"); |
| 50 | + expect(toTypeName("hello_world")).toBe("HelloWorld"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("preserves all-caps acronyms", () => { |
| 54 | + expect(toTypeName("API")).toBe("API"); |
| 55 | + expect(toTypeName("APIResponse")).toBe("APIResponse"); |
| 56 | + expect(toTypeName("myAPIKey")).toBe("MyAPIKey"); |
| 57 | + expect(toTypeName("HTTPResponse")).toBe("HTTPResponse"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("handles namespaced names by using only the last part", () => { |
| 61 | + expect(toTypeName("MyNamespace.MyType")).toBe("MyType"); |
| 62 | + expect(toTypeName("A.B.C.MyType")).toBe("MyType"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("sanitizes and converts special characters", () => { |
| 66 | + // Special chars become underscores, then PascalCase removes them |
| 67 | + expect(toTypeName("my-special$name")).toBe("MySpecialName"); |
| 68 | + expect(toTypeName("$invalid")).toBe("Invalid"); |
| 69 | + }); |
24 | 70 | }); |
25 | 71 |
|
26 | | - it("handles multiple special characters", () => { |
27 | | - expect(sanitizeNameForGraphQL("$My-Special.Name$")).toBe("_My_Special_Name_"); |
| 72 | + describe("toEnumMemberName", () => { |
| 73 | + it("converts to CONSTANT_CASE", () => { |
| 74 | + expect(toEnumMemberName("MyEnum", "myValue")).toBe("MY_VALUE"); |
| 75 | + expect(toEnumMemberName("Status", "inProgress")).toBe("IN_PROGRESS"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("handles already uppercase names", () => { |
| 79 | + expect(toEnumMemberName("MyEnum", "ACTIVE")).toBe("ACTIVE"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("uses enum name as prefix for invalid starting characters", () => { |
| 83 | + expect(toEnumMemberName("Priority", "1High")).toBe("PRIORITY_1_HIGH"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("handles special characters", () => { |
| 87 | + expect(toEnumMemberName("MyEnum", "value-with-dashes")).toBe("VALUE_WITH_DASHES"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("separates numbers", () => { |
| 91 | + expect(toEnumMemberName("MyEnum", "value123")).toBe("VALUE_123"); |
| 92 | + }); |
28 | 93 | }); |
29 | 94 |
|
30 | | - it("handles empty prefix parameter", () => { |
31 | | - expect(sanitizeNameForGraphQL("123Name", "")).toBe("_123Name"); |
| 95 | + describe("toFieldName", () => { |
| 96 | + it("converts to camelCase", () => { |
| 97 | + expect(toFieldName("MyField")).toBe("myField"); |
| 98 | + expect(toFieldName("SOME_VALUE")).toBe("someValue"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("handles snake_case", () => { |
| 102 | + expect(toFieldName("my_field_name")).toBe("myFieldName"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("handles special characters", () => { |
| 106 | + expect(toFieldName("my-field")).toBe("myField"); |
| 107 | + expect(toFieldName("$special")).toBe("_special"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("preserves leading underscores", () => { |
| 111 | + expect(toFieldName("_private")).toBe("_private"); |
| 112 | + expect(toFieldName("__internal")).toBe("__internal"); |
| 113 | + }); |
32 | 114 | }); |
33 | 115 |
|
34 | | - it("uses custom prefix for invalid starting character", () => { |
35 | | - expect(sanitizeNameForGraphQL("123Name", "Num")).toBe("Num_123Name"); |
| 116 | + describe("getSingleNameWithNamespace", () => { |
| 117 | + it("replaces dots with underscores", () => { |
| 118 | + expect(getSingleNameWithNamespace("My.Namespace.Type")).toBe("My_Namespace_Type"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("trims whitespace", () => { |
| 122 | + expect(getSingleNameWithNamespace(" My.Type ")).toBe("My_Type"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("handles names without namespace", () => { |
| 126 | + expect(getSingleNameWithNamespace("MyType")).toBe("MyType"); |
| 127 | + }); |
36 | 128 | }); |
37 | 129 | }); |
0 commit comments