-
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.
refactor: mutationResult fixture logic for tests
This is to fix the linter issues and make the code easier to read.
- Loading branch information
Showing
15 changed files
with
230 additions
and
110 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
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
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
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
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
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
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
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
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,46 @@ | ||
import { MutationObserverErrorResult } from '@tanstack/react-query' | ||
import { RenameProp } from '@test/types.js' | ||
import { createMutationIdleResult } from './idle.js' | ||
|
||
export type MutationErrorResult< | ||
TMutateFnName extends string, | ||
TError extends Error, | ||
TVariables, | ||
TResult extends MutationObserverErrorResult< | ||
unknown, | ||
TError, | ||
TVariables | ||
> = MutationObserverErrorResult<unknown, TError, TVariables> | ||
> = RenameProp<TResult, 'mutate', TMutateFnName> & { | ||
[key in `${TMutateFnName}Async`]: TResult['mutate'] | ||
} | ||
|
||
export type CreateErrorMutationResultParam = { | ||
status: 'error' | ||
mutateFnName: string | ||
error: Error | ||
variables: unknown | ||
data?: undefined | ||
} | ||
|
||
export function createMutationErrorResult< | ||
TMutateFnName extends string, | ||
TError extends Error, | ||
TVariables | ||
>( | ||
mutateFnName: TMutateFnName, | ||
error: TError, | ||
variables: TVariables | ||
): MutationErrorResult<TMutateFnName, TError, TVariables> { | ||
return { | ||
...createMutationIdleResult(mutateFnName), | ||
isIdle: false, | ||
isError: true, | ||
status: 'error', | ||
failureCount: 1, | ||
submittedAt: expect.any(Number), | ||
error, | ||
failureReason: error, | ||
variables | ||
} | ||
} |
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,39 @@ | ||
import { MutationObserverIdleResult } from '@tanstack/react-query' | ||
import { RenameProp } from '@test/types.js' | ||
|
||
export type MutationIdleResult<TMutateFnName extends string> = RenameProp< | ||
MutationObserverIdleResult, | ||
'mutate', | ||
TMutateFnName | ||
> & { [key in `${TMutateFnName}Async`]: MutationObserverIdleResult['mutate'] } | ||
|
||
export type CreateIdleMutationResultParam = { | ||
status: 'idle' | ||
mutateFnName: string | ||
data?: undefined | ||
variables?: undefined | ||
error?: undefined | ||
} | ||
|
||
export function createMutationIdleResult<TMutateFnName extends string>( | ||
mutateFnName: TMutateFnName | ||
) { | ||
return { | ||
[mutateFnName]: expect.any(Function), | ||
[`${mutateFnName}Async`]: expect.any(Function), | ||
isIdle: true, | ||
isPaused: false, | ||
isPending: false, | ||
isSuccess: false, | ||
reset: expect.any(Function), | ||
status: 'idle', | ||
submittedAt: 0, | ||
variables: undefined, | ||
context: undefined, | ||
data: undefined, | ||
error: null, | ||
failureCount: 0, | ||
failureReason: null, | ||
isError: false | ||
} as MutationIdleResult<TMutateFnName> | ||
} |
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,65 @@ | ||
import { | ||
CreateIdleMutationResultParam, | ||
createMutationIdleResult, | ||
MutationIdleResult | ||
} from './idle.js' | ||
import { | ||
createMutationPendingResult, | ||
CreatePendingMutationResultParam, | ||
MutationPendingResult | ||
} from './pending.js' | ||
import { | ||
createMutationSuccessResult, | ||
CreateSuccessMutationResultParam, | ||
MutationSuccessResult | ||
} from './success.js' | ||
import { | ||
CreateErrorMutationResultParam, | ||
createMutationErrorResult, | ||
MutationErrorResult | ||
} from './error.js' | ||
|
||
type CreateCustomMutationResultParam = | ||
| CreateIdleMutationResultParam | ||
| CreatePendingMutationResultParam | ||
| CreateSuccessMutationResultParam | ||
| CreateErrorMutationResultParam | ||
|
||
export function createCustomMutationResult<TMutateFnName extends string>( | ||
param: CreateIdleMutationResultParam | ||
): MutationIdleResult<TMutateFnName> | ||
|
||
export function createCustomMutationResult<TMutateFnName extends string>( | ||
param: CreatePendingMutationResultParam | ||
): MutationPendingResult<TMutateFnName> | ||
|
||
export function createCustomMutationResult< | ||
TMutateFnName extends string, | ||
TData = unknown, | ||
TVariables = unknown | ||
>(param: CreateSuccessMutationResultParam): MutationSuccessResult<TMutateFnName, TData, TVariables> | ||
|
||
export function createCustomMutationResult< | ||
TMutateFnName extends string, | ||
TError extends Error = Error, | ||
TVariables = unknown | ||
>(param: CreateErrorMutationResultParam): MutationErrorResult<TMutateFnName, TError, TVariables> | ||
|
||
export function createCustomMutationResult({ | ||
status, | ||
mutateFnName, | ||
data, | ||
error, | ||
variables | ||
}: CreateCustomMutationResultParam) { | ||
if (status === 'idle') { | ||
return createMutationIdleResult(mutateFnName) | ||
} | ||
if (status === 'pending') { | ||
return createMutationPendingResult(mutateFnName) | ||
} | ||
if (status === 'success') { | ||
return createMutationSuccessResult(mutateFnName, data, variables) | ||
} | ||
return createMutationErrorResult(mutateFnName, error, variables) | ||
} |
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,28 @@ | ||
import { MutationObserverLoadingResult } from '@tanstack/react-query' | ||
import { RenameProp } from '@test/types.js' | ||
import { createMutationIdleResult } from './idle.js' | ||
|
||
export type MutationPendingResult<TMutateFnName extends string> = RenameProp< | ||
MutationObserverLoadingResult, | ||
'mutate', | ||
TMutateFnName | ||
> & { [key in `${TMutateFnName}Async`]: MutationObserverLoadingResult['mutate'] } | ||
|
||
export type CreatePendingMutationResultParam = { | ||
status: 'pending' | ||
mutateFnName: string | ||
data?: undefined | ||
variables?: undefined | ||
error?: undefined | ||
} | ||
|
||
export function createMutationPendingResult<TMutateFnName extends string>( | ||
mutateFnName: TMutateFnName | ||
): MutationPendingResult<TMutateFnName> { | ||
return { | ||
...createMutationIdleResult(mutateFnName), | ||
isIdle: false, | ||
isPending: true, | ||
status: 'pending' | ||
} | ||
} |
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,40 @@ | ||
import { MutationObserverSuccessResult } from '@tanstack/react-query' | ||
import { RenameProp } from '@test/types.js' | ||
import { createMutationIdleResult } from './idle.js' | ||
|
||
export type MutationSuccessResult< | ||
TMutateFnName extends string, | ||
TData, | ||
TVariables, | ||
TResult extends MutationObserverSuccessResult< | ||
TData, | ||
Error, | ||
TVariables | ||
> = MutationObserverSuccessResult<TData, Error, TVariables> | ||
> = RenameProp<TResult, 'mutate', TMutateFnName> & { | ||
[key in `${TMutateFnName}Async`]: TResult['mutate'] | ||
} | ||
|
||
export type CreateSuccessMutationResultParam = { | ||
status: 'success' | ||
mutateFnName: string | ||
data: unknown | ||
variables: unknown | ||
error?: undefined | ||
} | ||
|
||
export function createMutationSuccessResult<TMutateFnName extends string, TData, TVariables>( | ||
mutateFnName: TMutateFnName, | ||
data: TData, | ||
variables: TVariables | ||
): MutationSuccessResult<TMutateFnName, TData, TVariables> { | ||
return { | ||
...createMutationIdleResult(mutateFnName), | ||
data, | ||
variables, | ||
isIdle: false, | ||
isSuccess: true, | ||
status: 'success', | ||
submittedAt: expect.any(Number) | ||
} | ||
} |
Oops, something went wrong.