Skip to content

Commit bfc4c90

Browse files
committed
introduces useActionForm
1 parent 2dff659 commit bfc4c90

9 files changed

+2349
-1
lines changed

packages/api-client-core/spec/mockUrqlClient.ts

+3
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ const newMockOperationFn = (assertions?: (request: GraphQLRequest) => void) => {
7474

7575
const fn = jest.fn((request: GraphQLRequest, options?: Partial<OperationContext>) => {
7676
const { query } = request;
77+
7778
const fetchOptions = options?.fetchOptions;
7879
const key = graphqlDocumentName(query) ?? "unknown";
80+
7981
subjects[key] ??= makeSubject<OperationResult>();
8082

8183
if (fetchOptions && typeof fetchOptions != "function") {
@@ -99,6 +101,7 @@ const newMockOperationFn = (assertions?: (request: GraphQLRequest) => void) => {
99101
if (!subjects[key]) {
100102
throw new Error(`No mock client subject started for key ${key}, options are ${Object.keys(subjects).join(", ")}`);
101103
}
104+
102105
act(() => {
103106
subjects[key].next({
104107
operation: null as any,

packages/react/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"dependencies": {
3131
"@gadgetinc/api-client-core": "^0.15.11",
3232
"react-fast-compare": "^3.2.2",
33+
"react-hook-form": "^7.46.1",
3334
"urql": "^4.0.4"
3435
},
3536
"resolutions": {

packages/react/spec/useAction.spec.tsx

+61
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,67 @@ describe("useAction", () => {
463463
});
464464
});
465465

466+
test("generates correct mutation and variables if there is an ambiguous field", async () => {
467+
let variables: AnyVariables;
468+
469+
const client = createMockUrqlClient({
470+
mutationAssertions: (request) => {
471+
variables = request.variables;
472+
},
473+
});
474+
475+
const wrapper = (props: { children: React.ReactNode }) => <Provider value={client}>{props.children}</Provider>;
476+
477+
const { result } = renderHook(() => useAction(relatedProductsApi.ambiguous.update), {
478+
wrapper,
479+
});
480+
481+
let mutationPromise: any;
482+
act(() => {
483+
mutationPromise = result.current[1]({ id: "123", ambiguous: { ambiguous: "foo", booleanField: true } });
484+
});
485+
486+
expect(client.executeMutation).toBeCalledTimes(1);
487+
488+
client.executeMutation.pushResponse("updateAmbiguous", {
489+
data: {
490+
updateAmbiguous: {
491+
success: true,
492+
ambiguous: {
493+
id: "123",
494+
ambiguous: "foo",
495+
booleanField: true,
496+
},
497+
},
498+
},
499+
stale: false,
500+
hasNext: false,
501+
});
502+
503+
await act(async () => {
504+
const promiseResult = await mutationPromise;
505+
expect(promiseResult.data).toMatchInlineSnapshot(`
506+
{
507+
"ambiguous": "foo",
508+
"booleanField": true,
509+
"id": "123",
510+
}
511+
`);
512+
expect(promiseResult.fetching).toBe(false);
513+
expect(promiseResult.error).toBeFalsy();
514+
});
515+
516+
expect(variables).toMatchInlineSnapshot(`
517+
{
518+
"ambiguous": {
519+
"ambiguous": "foo",
520+
"booleanField": true,
521+
},
522+
"id": "123",
523+
}
524+
`);
525+
});
526+
466527
test("should throw if called without a model api identifier and there is an ambiguous field", async () => {
467528
let caughtError = null;
468529

0 commit comments

Comments
 (0)