-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
128 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
29 changes: 0 additions & 29 deletions
29
app/tests/unit/mutations/ProjectRevision/discardProjectRevision.test.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |