Skip to content

Commit 916c54c

Browse files
committed
Add test coverage for @operationFields, model refs, and empty types
- Add @operationFields test to ObjectType (verifies operation field rendering) - Add nested model reference test to ObjectType (verifies type references) - Add empty model tests to ObjectType, InputType, InterfaceType (documents that GraphQL requires at least one field per type)
1 parent 11b0f8d commit 916c54c

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

packages/graphql/test/components/input-type.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,12 @@ describe("InputType component", () => {
8484

8585
expect(sdl).toContain("values: [String!]!");
8686
});
87+
88+
it("throws error for empty model (GraphQL requires at least one field)", async () => {
89+
const { Empty } = await tester.compile(t.code`model ${t.model("Empty")} {}`);
90+
91+
expect(() => {
92+
renderComponentToSDL(tester.program, <InputType type={Empty} />);
93+
}).toThrow(/must define fields/);
94+
});
8795
});

packages/graphql/test/components/interface-type.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,17 @@ describe("InterfaceType component", () => {
7272
expect(sdl).toContain("description: String");
7373
expect(sdl).not.toContain("description: String!");
7474
});
75+
76+
it("throws error for empty interface (GraphQL requires at least one field)", async () => {
77+
const { Empty } = await tester.compile(
78+
t.code`
79+
@Interface
80+
model ${t.model("Empty")} {}
81+
`,
82+
);
83+
84+
expect(() => {
85+
renderComponentToSDL(tester.program, <InterfaceType type={Empty} />);
86+
}).toThrow(/must define fields/);
87+
});
7588
});

packages/graphql/test/components/object-type.test.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,52 @@ describe("ObjectType component", () => {
119119
expect(sdl).toContain("id: String!");
120120
expect(sdl).toContain("name: String!");
121121
});
122+
123+
it("renders operation fields via @operationFields", async () => {
124+
const { Book } = await tester.compile(
125+
t.code`
126+
@operationFields(getRelated)
127+
model ${t.model("Book")} { title: string; }
128+
129+
op getRelated(limit: int32): Book[];
130+
`,
131+
);
132+
133+
const sdl = renderComponentToSDL(tester.program, <ObjectType type={Book} />);
134+
135+
expect(sdl).toContain("type Book {");
136+
expect(sdl).toContain("title: String!");
137+
expect(sdl).toContain("getRelated(limit: Int!): [Book!]!");
138+
});
139+
140+
it("renders fields that reference other models", async () => {
141+
const { Author } = await tester.compile(
142+
t.code`
143+
model ${t.model("Book")} { title: string; }
144+
model ${t.model("Author")} { name: string; favoriteBook: Book; }
145+
`,
146+
);
147+
148+
const sdl = renderComponentToSDL(
149+
tester.program,
150+
<>
151+
<gql.ObjectType name="Book">
152+
<gql.Field name="title" type={gql.String} nonNull />
153+
</gql.ObjectType>
154+
<ObjectType type={Author} />
155+
</>,
156+
);
157+
158+
expect(sdl).toContain("type Author {");
159+
expect(sdl).toContain("name: String!");
160+
expect(sdl).toContain("favoriteBook: Book!");
161+
});
162+
163+
it("throws error for empty model (GraphQL requires at least one field)", async () => {
164+
const { Empty } = await tester.compile(t.code`model ${t.model("Empty")} {}`);
165+
166+
expect(() => {
167+
renderComponentToSDL(tester.program, <ObjectType type={Empty} />);
168+
}).toThrow(/must define fields/);
169+
});
122170
});

0 commit comments

Comments
 (0)