Skip to content

Commit b9631d7

Browse files
authored
Merge pull request #267 from gadget-inc/fix_useAction_auth_fat_triggers
Fix for useAction incorrectly mapping non nested auth action inputs
2 parents 256e169 + 76b998c commit b9631d7

File tree

6 files changed

+84
-35
lines changed

6 files changed

+84
-35
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@gadget-client/app-with-no-user-model": "^1.3.0",
2525
"@gadget-client/bulk-actions-test": "^1.102.0",
2626
"@gadget-client/related-products-example": "^1.848.0",
27-
"@gadget-client/super-auth": "^1.17.0",
27+
"@gadget-client/super-auth": "^1.27.0",
2828
"@gadgetinc/api-client-core": "workspace:*",
2929
"@gadgetinc/eslint-config": "^0.6.1",
3030
"@gadgetinc/prettier-config": "^0.4.0",

packages/react/spec/auth/useSession.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,23 @@ describe("useSession", () => {
2929
expect(query).toMatchInlineSnapshot(`
3030
"query currentSession {
3131
currentSession {
32-
id
3332
__typename
3433
createdAt
35-
updatedAt
34+
id
3635
roles {
3736
key
3837
name
3938
}
39+
updatedAt
4040
user {
41-
id
4241
__typename
4342
createdAt
44-
updatedAt
45-
lastName
46-
firstName
4743
email
44+
firstName
4845
googleImageUrl
46+
id
47+
lastName
48+
updatedAt
4949
}
5050
}
5151
gadgetMeta {
@@ -69,23 +69,23 @@ describe("useSession", () => {
6969
expect(query).toMatchInlineSnapshot(`
7070
"query currentSession {
7171
currentSession {
72-
id
7372
__typename
7473
createdAt
75-
updatedAt
74+
id
7675
roles {
7776
key
7877
name
7978
}
79+
updatedAt
8080
user {
81-
id
8281
__typename
8382
createdAt
84-
updatedAt
85-
lastName
86-
firstName
8783
email
84+
firstName
8885
googleImageUrl
86+
id
87+
lastName
88+
updatedAt
8989
}
9090
}
9191
gadgetMeta {

packages/react/spec/auth/useUser.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ describe("useUser", () => {
2828
expect(query).toMatchInlineSnapshot(`
2929
"query currentSession {
3030
currentSession {
31-
id
3231
__typename
3332
createdAt
34-
updatedAt
33+
id
3534
roles {
3635
key
3736
name
3837
}
38+
updatedAt
3939
user {
40-
id
4140
__typename
4241
createdAt
43-
updatedAt
44-
lastName
45-
firstName
4642
email
43+
firstName
4744
googleImageUrl
45+
id
46+
lastName
47+
updatedAt
4848
}
4949
}
5050
gadgetMeta {
@@ -68,23 +68,23 @@ describe("useUser", () => {
6868
expect(query).toMatchInlineSnapshot(`
6969
"query currentSession {
7070
currentSession {
71-
id
7271
__typename
7372
createdAt
74-
updatedAt
73+
id
7574
roles {
7675
key
7776
name
7877
}
78+
updatedAt
7979
user {
80-
id
8180
__typename
8281
createdAt
83-
updatedAt
84-
lastName
85-
firstName
8682
email
83+
firstName
8784
googleImageUrl
85+
id
86+
lastName
87+
updatedAt
8888
}
8989
}
9090
gadgetMeta {
@@ -110,14 +110,14 @@ describe("useUser", () => {
110110
expect(query).toMatchInlineSnapshot(`
111111
"query currentSession {
112112
currentSession {
113-
id
114113
__typename
115114
createdAt
116-
updatedAt
115+
id
117116
roles {
118117
key
119118
name
120119
}
120+
updatedAt
121121
user {
122122
firstName
123123
}

packages/react/spec/useAction.spec.tsx

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { AnyVariables } from "urql";
88
import { Provider } from "../src/GadgetProvider.js";
99
import { useAction } from "../src/index.js";
1010
import type { ErrorWrapper } from "../src/utils.js";
11-
import { relatedProductsApi } from "./apis.js";
11+
import { relatedProductsApi, superAuthApi } from "./apis.js";
1212
import { MockClientWrapper, createMockUrqlClient, mockUrqlClient } from "./testWrappers.js";
1313

1414
describe("useAction", () => {
@@ -417,6 +417,54 @@ describe("useAction", () => {
417417
`);
418418
});
419419

420+
test("generates correct mutation and variables for trigger that contributes inputs", async () => {
421+
let variables: AnyVariables;
422+
423+
const client = createMockUrqlClient({
424+
mutationAssertions: (request) => {
425+
variables = request.variables;
426+
},
427+
});
428+
429+
const wrapper = (props: { children: React.ReactNode }) => <Provider value={client}>{props.children}</Provider>;
430+
431+
const { result } = renderHook(() => useAction(superAuthApi.user.signUp), {
432+
wrapper,
433+
});
434+
435+
let mutationPromise: any;
436+
act(() => {
437+
mutationPromise = result.current[1]({ email: "[email protected]", password: "password123!" });
438+
});
439+
440+
client.executeMutation.pushResponse("signUpUser", {
441+
data: {
442+
signUpUser: {
443+
result: {
444+
result: "ok",
445+
},
446+
success: true,
447+
},
448+
},
449+
stale: false,
450+
hasNext: false,
451+
});
452+
453+
await act(async () => {
454+
const promiseResult = await mutationPromise;
455+
expect(promiseResult.data).toEqual({ result: "ok" });
456+
expect(promiseResult.fetching).toBe(false);
457+
expect(promiseResult.error).toBeFalsy();
458+
});
459+
460+
expect(variables).toMatchInlineSnapshot(`
461+
{
462+
"email": "[email protected]",
463+
"password": "password123!",
464+
}
465+
`);
466+
});
467+
420468
test("should throw if called without a model api identifier and there is an ambiguous field", async () => {
421469
let caughtError = null;
422470

packages/react/src/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,10 @@ export const disambiguateActionVariables = (
327327

328328
if (action.acceptsModelInput || action.hasCreateOrUpdateEffect) {
329329
if (
330-
action.modelApiIdentifier in variables &&
331-
typeof variables[action.modelApiIdentifier] === "object" &&
332-
variables[action.modelApiIdentifier] !== null
330+
(action.modelApiIdentifier in variables &&
331+
typeof variables[action.modelApiIdentifier] === "object" &&
332+
variables[action.modelApiIdentifier] !== null) ||
333+
!action.variables[action.modelApiIdentifier]
333334
) {
334335
newVariables = variables;
335336
} else {

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)