Skip to content

Commit

Permalink
refactor: mutationResult fixture logic for tests
Browse files Browse the repository at this point in the history
This is to fix the linter issues and make the code easier to read.
  • Loading branch information
tmjssz committed Oct 17, 2024
1 parent 65dbf66 commit 0ff2774
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/hooks/useConfirmTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as useWaitForTransaction from '@/hooks/useWaitForTransaction.js'
import * as useSignerClientMutation from '@/hooks/useSignerClientMutation.js'
import { configExistingSafe } from '@test/config.js'
import { ethereumTxHash, safeAddress, safeTxHash, signerPrivateKeys } from '@test/fixtures/index.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult/index.js'
import { renderHookInQueryClientProvider } from '@test/utils.js'
import { MutationKey, QueryKey } from '@/constants.js'
import { queryClient } from '@/queryClient.js'
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as useWaitForTransaction from '@/hooks/useWaitForTransaction.js'
import * as useSignerClientMutation from '@/hooks/useSignerClientMutation.js'
import { configExistingSafe } from '@test/config.js'
import { ethereumTxHash, safeAddress, safeTxHash, signerPrivateKeys } from '@test/fixtures/index.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult/index.js'
import { renderHookInQueryClientProvider } from '@test/utils.js'
import { MutationKey, QueryKey } from '@/constants.js'
import { queryClient } from '@/queryClient.js'
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSignerClientMutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as useSignerClient from '@/hooks/useSignerClient.js'
import * as useConfig from '@/hooks/useConfig.js'
import { configExistingSafe } from '@test/config.js'
import { safeMultisigTransaction, signerPrivateKeys } from '@test/fixtures/index.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult/index.js'
import { renderHookInQueryClientProvider } from '@test/utils.js'

// This is necessary to set a spy on the `useMutation` function without getting the following error:
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useUpdateOwners/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as useSwapOwner from '@/hooks/useUpdateOwners/useSwapOwner.js'
import { configExistingSafe } from '@test/config.js'
import { signerPrivateKeys } from '@test/fixtures/index.js'
import { renderHookInMockedSafeProvider } from '@test/utils.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult/index.js'
import { useUpdateOwners } from './index.js'

describe('useUpdateOwners', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useUpdateOwners/useAddOwner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
signerPrivateKeys
} from '@test/fixtures/index.js'
import { configPredictedSafe } from '@test/config.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult/index.js'
import { renderHookInQueryClientProvider } from '@test/utils.js'
import { MutationKey } from '@/constants.js'

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useUpdateOwners/useRemoveOwner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
signerPrivateKeys
} from '@test/fixtures/index.js'
import { configPredictedSafe } from '@test/config.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult/index.js'
import { renderHookInQueryClientProvider } from '@test/utils.js'
import { MutationKey } from '@/constants.js'

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useUpdateOwners/useSwapOwner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
signerPrivateKeys
} from '@test/fixtures/index.js'
import { configPredictedSafe } from '@test/config.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult/index.js'
import { renderHookInQueryClientProvider } from '@test/utils.js'
import { MutationKey } from '@/constants.js'

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useUpdateThreshold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as useSendTransaction from '@/hooks/useSendTransaction.js'
import * as useSignerClientMutation from '@/hooks/useSignerClientMutation.js'
import { ethereumTxHash, safeMultisigTransaction, signerPrivateKeys } from '@test/fixtures/index.js'
import { configPredictedSafe } from '@test/config.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult.js'
import { createCustomMutationResult } from '@test/fixtures/mutationResult/index.js'
import { renderHookInQueryClientProvider } from '@test/utils.js'
import { MutationKey } from '@/constants.js'

Expand Down
102 changes: 0 additions & 102 deletions test/fixtures/mutationResult.ts

This file was deleted.

46 changes: 46 additions & 0 deletions test/fixtures/mutationResult/error.ts
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
}
}
39 changes: 39 additions & 0 deletions test/fixtures/mutationResult/idle.ts
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>
}
65 changes: 65 additions & 0 deletions test/fixtures/mutationResult/index.ts
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)
}
28 changes: 28 additions & 0 deletions test/fixtures/mutationResult/pending.ts
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'
}
}
40 changes: 40 additions & 0 deletions test/fixtures/mutationResult/success.ts
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)
}
}
Loading

0 comments on commit 0ff2774

Please sign in to comment.