Skip to content

Commit d92420b

Browse files
committed
Extends fake-client to inspect operation options
1 parent 4005ec6 commit d92420b

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

source/zod-graphql-fake-client/fake-client.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ test('throws when inspecting a query that doesn’t exist', () => {
1717
}
1818
});
1919

20+
test('throws when inspecting a operation options that don’t exist', () => {
21+
const client = createFakeGraphqlClient();
22+
23+
try {
24+
client.inspectFirstOperationOptions();
25+
assert.fail('Expected inspectFirstOperationOptions() to throw but it did not');
26+
} catch (error: unknown) {
27+
assert.strictEqual((error as Error).message, 'No operationOption at index 0 recorded');
28+
}
29+
});
30+
2031
test('query() returns the default result when no result is configured', async () => {
2132
const client = createFakeGraphqlClient();
2233

@@ -108,3 +119,41 @@ test('inspectFirstOperationPayload() returns the mutation payload of the first m
108119

109120
assert.deepStrictEqual(payload, { operationName: 'foo', query: 'mutation foo { foo }', variables: {} });
110121
});
122+
123+
test('inspectFirstOperationOptions() returns the operation options of the first query', async () => {
124+
const client = createFakeGraphqlClient();
125+
126+
await client.queryOrThrow(simpleQuery, {
127+
operationName: 'foo',
128+
headers: { foo: 'bar' },
129+
timeout: 42,
130+
variables: {}
131+
});
132+
const options = client.inspectFirstOperationOptions();
133+
134+
assert.deepStrictEqual(options, {
135+
operationName: 'foo',
136+
headers: { foo: 'bar' },
137+
timeout: 42,
138+
variables: {}
139+
});
140+
});
141+
142+
test('inspectFirstOperationOptions() returns the operation options of the first mutation', async () => {
143+
const client = createFakeGraphqlClient();
144+
145+
await client.mutateOrThrow(simpleQuery, {
146+
operationName: 'foo',
147+
headers: { foo: 'bar' },
148+
timeout: 42,
149+
variables: {}
150+
});
151+
const options = client.inspectFirstOperationOptions();
152+
153+
assert.deepStrictEqual(options, {
154+
operationName: 'foo',
155+
headers: { foo: 'bar' },
156+
timeout: 42,
157+
variables: {}
158+
});
159+
});

source/zod-graphql-fake-client/fake-client.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { buildGraphqlMutation, buildGraphqlQuery } from '../zod-graphql-query-bu
1313
export type FakeGraphqlClient = GraphqlClient & {
1414
readonly inspectOperationPayload: (index: number) => GraphqlOverHttpOperationRequestPayload;
1515
readonly inspectFirstOperationPayload: () => GraphqlOverHttpOperationRequestPayload;
16+
readonly inspectOperationOptions: (index: number) => OperationOptions;
17+
readonly inspectFirstOperationOptions: () => OperationOptions;
1618
};
1719

1820
type FakeSuccessData = {
@@ -33,6 +35,7 @@ type FakeClientOptions = {
3335
export function createFakeGraphqlClient(clientOptions: FakeClientOptions = {}): FakeGraphqlClient {
3436
const { results = [] } = clientOptions;
3537
const operationPayloads: GraphqlOverHttpOperationRequestPayload[] = [];
38+
const operationOptions: OperationOptions[] = [];
3639
const defaultResult: FakeResult = { data: {} };
3740

3841
async function collectOperation<Schema extends QuerySchema>(
@@ -61,6 +64,7 @@ export function createFakeGraphqlClient(clientOptions: FakeClientOptions = {}):
6164
query: serializedQuery,
6265
variables: variableValues
6366
});
67+
operationOptions.push(options);
6468

6569
if (result.error !== undefined) {
6670
return { success: false, errorDetails: result.error };
@@ -91,6 +95,15 @@ export function createFakeGraphqlClient(clientOptions: FakeClientOptions = {}):
9195
return payload;
9296
}
9397

98+
function inspectOperationOptions(index: number): OperationOptions {
99+
const operationOption = operationOptions[index];
100+
if (operationOption === undefined) {
101+
throw new Error(`No operationOption at index ${index} recorded`);
102+
}
103+
104+
return operationOption;
105+
}
106+
94107
return {
95108
query,
96109

@@ -118,6 +131,10 @@ export function createFakeGraphqlClient(clientOptions: FakeClientOptions = {}):
118131

119132
inspectFirstOperationPayload() {
120133
return inspectOperationPayload(0);
134+
},
135+
inspectOperationOptions,
136+
inspectFirstOperationOptions() {
137+
return inspectOperationOptions(0);
121138
}
122139
};
123140
}

0 commit comments

Comments
 (0)