Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasJ committed Apr 20, 2022
1 parent 003433e commit 12c48af
Show file tree
Hide file tree
Showing 3 changed files with 410 additions and 6 deletions.
19 changes: 14 additions & 5 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ export function buildThunks<
api.util.patchQueryData(endpointName, args, ret.inversePatches)
),
}
if (currentState.status === QueryStatus.uninitialized) {
return ret
}
if ('data' in currentState) {
if (isDraftable(currentState.data)) {
const [, patches, inversePatches] = produceWithPatches(
Expand All @@ -305,10 +302,22 @@ export function buildThunks<
path: [],
value: undefined,
})
dispatch(api.endpoints[endpointName].initiate(args, {subscribe: false, forceRefetch: true, }))
dispatch(
(
api.endpoints[endpointName] as ApiEndpointQuery<
QueryDefinition<any, any, any, any, any>,
Definitions
>
).initiate(args, {
subscribe: false,
forceRefetch: true,
forceQueryFn: () => ({
data: upsertRecipe(undefined),
}),
})
)
}


return ret
}

Expand Down
12 changes: 11 additions & 1 deletion packages/toolkit/src/query/core/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/**
* Note: this file should import all other files for type discovery and declaration merging
*/
import type { PatchQueryDataThunk, UpdateQueryDataThunk } from './buildThunks'
import type {
PatchQueryDataThunk,
UpdateQueryDataThunk,
UpsertQueryDataThunk,
} from './buildThunks'
import { buildThunks } from './buildThunks'
import type {
ActionCreatorWithPayload,
Expand Down Expand Up @@ -210,6 +214,10 @@ declare module '../apiTypes' {
Definitions,
RootState<Definitions, string, ReducerPath>
>
upsertQueryData: UpsertQueryDataThunk<
Definitions,
RootState<Definitions, string, ReducerPath>
>
/**
* A Redux thunk that applies a JSON diff/patch array to the cached data for a given query result. This immediately updates the Redux state with those changes.
*
Expand Down Expand Up @@ -406,6 +414,7 @@ export const coreModule = (): Module<CoreModule> => ({
mutationThunk,
patchQueryData,
updateQueryData,
upsertQueryData,
prefetch,
buildMatchThunkActions,
} = buildThunks({
Expand Down Expand Up @@ -434,6 +443,7 @@ export const coreModule = (): Module<CoreModule> => ({
safeAssign(api.util, {
patchQueryData,
updateQueryData,
upsertQueryData,
prefetch,
resetApiState: sliceActions.resetApiState,
})
Expand Down
Loading

0 comments on commit 12c48af

Please sign in to comment.