Skip to content

Commit

Permalink
fix learningpath invalidation (#635)
Browse files Browse the repository at this point in the history
* fix learningpath invalidation

* invalidate on removal, too
  • Loading branch information
ChristopherChudzicki authored Mar 26, 2024
1 parent eef4c7f commit fb5de02
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
17 changes: 9 additions & 8 deletions frontends/api/src/hooks/learningResources/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,21 +273,22 @@ describe("LearningPath CRUD", () => {
setMockResponse.post(url, relationship)

const { wrapper, queryClient } = setupReactQueryTest()
jest.spyOn(queryClient, "invalidateQueries")
const { result } = renderHook(useLearningpathRelationshipCreate, {
wrapper,
})
result.current.mutate(requestData)

await waitFor(() => expect(result.current.isSuccess).toBe(true))
expect(makeRequest).toHaveBeenCalledWith("post", url, requestData)
expect(queryClient.invalidateQueries).toHaveBeenCalledWith(
keys.relationshipListing,
)

expect(invalidateResourceQueries).toHaveBeenCalledWith(
queryClient,
relationship.child,
)
expect(invalidateResourceQueries).toHaveBeenCalledWith(
queryClient,
relationship.parent,
)

// Check patches cached result
expect(queryClient.getQueryData(keys.childResource)).toEqual(
Expand All @@ -302,7 +303,6 @@ describe("LearningPath CRUD", () => {
setMockResponse.delete(url, null)
const { wrapper, queryClient } = setupReactQueryTest()
queryClient.setQueryData(keys.childResource, relationship.resource)
jest.spyOn(queryClient, "invalidateQueries")
const { result } = renderHook(useLearningpathRelationshipDestroy, {
wrapper,
})
Expand All @@ -311,13 +311,14 @@ describe("LearningPath CRUD", () => {
await waitFor(() => expect(result.current.isSuccess).toBe(true))

expect(makeRequest).toHaveBeenCalledWith("delete", url, undefined)
expect(queryClient.invalidateQueries).toHaveBeenCalledWith(
keys.relationshipListing,
)
expect(invalidateResourceQueries).toHaveBeenCalledWith(
queryClient,
relationship.child,
)
expect(invalidateResourceQueries).toHaveBeenCalledWith(
queryClient,
relationship.parent,
)

// Patched existing resource
expect(queryClient.getQueryData(keys.childResource)).toEqual({
Expand Down
12 changes: 3 additions & 9 deletions frontends/api/src/hooks/learningResources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,7 @@ const useLearningpathRelationshipCreate = () => {
},
onSettled: (response, _err, vars) => {
invalidateResourceQueries(queryClient, vars.child)
queryClient.invalidateQueries(
learningResources.learningpaths._ctx.detail(vars.parent)._ctx
.infiniteItems._def,
)
invalidateResourceQueries(queryClient, vars.parent)
},
})
}
Expand All @@ -188,12 +185,9 @@ const useLearningpathRelationshipDestroy = () => {
},
)
},
onSettled: (response, _err, vars) => {
onSettled: (_response, _err, vars) => {
invalidateResourceQueries(queryClient, vars.child)
queryClient.invalidateQueries(
learningResources.learningpaths._ctx.detail(vars.parent)._ctx
.infiniteItems._def,
)
invalidateResourceQueries(queryClient, vars.parent)
},
})
}
Expand Down

0 comments on commit fb5de02

Please sign in to comment.