Skip to content

Commit

Permalink
feat: generic discard mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastia committed Jan 13, 2022
1 parent 90ab8ca commit 895b043
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 63 deletions.
9 changes: 9 additions & 0 deletions app/mutations/FormChange/discardFormChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { graphql } from "react-relay";

export const mutation = graphql`
mutation discardFormChangeMutation($input: UpdateFormChangeInput!) {
updateFormChange(input: $input) {
__typename
}
}
`;
36 changes: 2 additions & 34 deletions app/mutations/ProjectRevision/discardProjectRevision.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
import { graphql, useMutation, UseMutationConfig } from "react-relay";
import { Disposable, MutationParameters } from "relay-runtime";
import { graphql } from "react-relay";

const mutation = graphql`
export const mutation = graphql`
mutation discardProjectRevisionMutation($input: UpdateProjectRevisionInput!) {
updateProjectRevision(input: $input) {
__typename
}
}
`;

function useDiscardProjectMutation<TMutation extends MutationParameters>(): [
(
projectRevisionId: string,
config: Partial<UseMutationConfig<TMutation>>
) => Disposable,
boolean
] {
const [updateMutation, isInFlight] = useMutation(mutation);

const discardProjectMutation = (
projectRevisionId: string,
config: Partial<UseMutationConfig<TMutation>>
) => {
return updateMutation({
variables: {
input: {
id: projectRevisionId,
projectRevisionPatch: {
deletedAt: new Date().toISOString(),
},
},
},
...config,
});
};

return [discardProjectMutation, isInFlight];
}

export default useDiscardProjectMutation;
62 changes: 62 additions & 0 deletions app/mutations/useDiscardMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useMutation, UseMutationConfig } from "react-relay";
import {
Disposable,
GraphQLTaggedNode,
MutationParameters,
} from "relay-runtime";

/**
*
* This hook creates a discard mutation by wrapping the relay `useMutation` hook and
* setting the mutation variables with a `deletedAt` value.
*
* @param relayNodeName the name of the relay node the mutation is applied to, e.g. "projectRevision", "formChange", ...
* @param mutation the GraphQL 'update' mutation on the relay node
* @returns a tuple with the discard mutation and a boolean indicating if the mutation is in flight
*
* example usage:
*
* const mutation = graphql`
* mutation discardMyEntityMutation($input: UpdateMyEntityInput!) {
* updateMyEntity(input: $input) {
* __typename
* }
* }`;
*
* const [discardMyEntityMutation, isInFlight] = useDiscardMutation("myEntity", myTableMutation);
*
*/

export default function useDiscardMutation<
TMutation extends MutationParameters
>(
relayNodeName: string,
mutation: GraphQLTaggedNode
): [
(
relayId: string,
config: Partial<UseMutationConfig<TMutation>>
) => Disposable,
boolean
] {
const [updateMutation, isInFlight] = useMutation(mutation);

const discardMutation = (
formChangeId: string,
config: Partial<UseMutationConfig<TMutation>>
) => {
return updateMutation({
variables: {
input: {
id: formChangeId,
[`${relayNodeName}Patch`]: {
deletedAt: new Date().toISOString(),
},
},
},
...config,
});
};

return [discardMutation, isInFlight];
}

This file was deleted.

55 changes: 55 additions & 0 deletions app/tests/unit/mutations/useDiscardMutation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { renderHook } from "@testing-library/react-hooks";
import useDiscardMutation from "mutations/useDiscardMutation";

describe("The useDiscardMutation hook", () => {
it("should call the relay hook with the passed id and the current date, with a properly named patch field", () => {
const mockMutationFunction = jest.fn();

jest
.spyOn(require("react-relay"), "useMutation")
.mockImplementation(() => [mockMutationFunction, jest.fn()]);

const {
result: {
current: [mutationUnderTest],
},
} = renderHook(() =>
useDiscardMutation(
"testTableName",
"some_irrelevant_graphql_for_this_test" as any
)
);

mutationUnderTest("test_project_revision_id", {});

expect(mockMutationFunction).toHaveBeenCalledWith({
variables: {
input: {
id: "test_project_revision_id",
testTableNamePatch: { deletedAt: expect.anything() },
},
},
});
});

it("should pass the inFlight value from the internal relay hook", () => {
const mockInFlight = jest.fn();

jest
.spyOn(require("react-relay"), "useMutation")
.mockImplementation(() => [jest.fn(), mockInFlight]);

const {
result: {
current: [, inFlightUnderTest],
},
} = renderHook(() =>
useDiscardMutation(
"testTableName",
"some_irrelevant_graphql_for_this_test" as any
)
);

expect(inFlightUnderTest).toEqual(mockInFlight);
});
});

0 comments on commit 895b043

Please sign in to comment.