Skip to content

Commit

Permalink
chore: add eslint action and activate CLA (#20)
Browse files Browse the repository at this point in the history
* chore: add eslint action and activate CLA

* chore: remove @ts-ignore comments

* chore: skip @typescript lint warning

* fix: Linter errors + refactor mutationResult fixtures (#22)

* refactor: mutationResult fixture logic for tests

---------

Co-authored-by: Tim <[email protected]>
  • Loading branch information
dasanra and tmjssz authored Oct 18, 2024
1 parent a05d55b commit 4beab17
Show file tree
Hide file tree
Showing 25 changed files with 816 additions and 130 deletions.
15 changes: 15 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
ignorePatterns: ['dist'],
extends: [
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'plugin:prettier/recommended' // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
],
parserOptions: {
ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
sourceType: 'module' // Allows for the use of imports
},
rules: {
'@typescript-eslint/no-explicit-any': ['warn'] // any returns linter error by default.
}
}
2 changes: 1 addition & 1 deletion .github/cla.yml → .github/workflows/cla.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
# branch should not be protected
branch: 'main'
# user names of users allowed to contribute without CLA
allowlist: dasanra,DaniSomoza,yagopv,bot*
allowlist: dasanra,DaniSomoza,yagopv,tmjssz,bot*

# the followings are the optional inputs - If the optional inputs are not given, then default values will be taken
# enter the remote organization name where the signatures should be stored (Default is storing the signatures in the same repository)
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: 'ESLint check'
on: [pull_request]

jobs:
eslint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20
cache: yarn

- name: Install Dependencies
run: yarn install --frozen-lockfile

- name: Run eslint
run: yarn lint:check
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"coverage": "jest --coverage",
"format:check": "prettier --check \"*/**/*.{js,json,md,ts}\"",
"format": "prettier --write \"*/**/*.{js,json,md,ts}\"",
"lint:check": "eslint './{src,test}/**/*.{js,jsx,ts,tsx}'",
"unbuild": "rm -rf dist cache *.tsbuildinfo",
"build": "yarn unbuild && tsc -p tsconfig.build.json && tsc-alias"
},
Expand Down Expand Up @@ -50,8 +51,14 @@
"@types/jest": "^29.5.13",
"@types/react": ">=18.3.11",
"@types/react-dom": ">=18.3.1",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"prettier": "^3.3.3",
"react": ">=18.3.1",
"react-dom": ">=18.3.1",
"ts-jest": "^29.2.5",
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/helpers/useCompare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { usePrevious } from './usePrevious.js'
* @param object Object to compare.
* @returns Whether the object has changed.
*/
export function useCompareObject(object?: Object) {
export function useCompareObject(object?: object) {
const prevObject = usePrevious(object)
return JSON.stringify(prevObject) !== JSON.stringify(object)
}
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
6 changes: 4 additions & 2 deletions src/hooks/usePublicClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ describe('usePublicClient', () => {

it('should create a new SafeClient client if the passed config changes', async () => {
const { result, rerender } = renderHookInSafeProvider(
({ config = configPredictedSafe }: UsePubicClientParams = {}) => usePublicClient({ config }),
({ config = configPredictedSafe }: UsePubicClientParams = {}) =>
usePublicClient({ config }),
{ config: configExistingSafe }
)

Expand All @@ -73,7 +74,8 @@ describe('usePublicClient', () => {

it('should NOT create a new SafeClient client if the config param matches the one from previous render', async () => {
const { result, rerender } = renderHookInSafeProvider(
({ config = configPredictedSafe }: UsePubicClientParams = {}) => usePublicClient({ config }),
({ config = configPredictedSafe }: UsePubicClientParams = {}) =>
usePublicClient({ config }),
{ config: configExistingSafe }
)

Expand Down
1 change: 0 additions & 1 deletion src/hooks/usePublicClientQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { renderHookInQueryClientProvider } from '@test/utils.js'
// "TypeError: Cannot redefine property: useQuery"
jest.mock('@tanstack/react-query', () => ({
__esModule: true,
// @ts-ignore
...jest.requireActual('@tanstack/react-query')
}))

Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useSafeInfo/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('useSafeInfo', () => {
})

it('should return fetch and return Safe infos using individual hooks', () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { refetch, ...expectedResult } = createCustomQueryResult({
status: 'success',
data: safeInfo
Expand Down Expand Up @@ -98,6 +99,7 @@ describe('useSafeInfo', () => {
it('should return with loading state + partial data if any individual hook returns with loading state', async () => {
useThresholdSpy.mockReturnValueOnce(createCustomQueryResult({ status: 'pending' }))

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { refetch, ...expectedResult } = {
...queryPendingResult,
data: { ...safeInfo, threshold: undefined }
Expand Down Expand Up @@ -127,6 +129,7 @@ describe('useSafeInfo', () => {
useAddressSpy.mockReturnValueOnce(queryLoadingErrorResult)
useOwnersSpy.mockReturnValueOnce(queryLoadingErrorResult)

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { refetch, ...expectedResult } = {
...queryLoadingErrorResult,
errorUpdateCount: 2,
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
3 changes: 1 addition & 2 deletions src/hooks/useSignerClientMutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ 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:
// "TypeError: Cannot redefine property: useMutation"
jest.mock('@tanstack/react-query', () => ({
__esModule: true,
// @ts-ignore
...jest.requireActual('@tanstack/react-query')
}))

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
5 changes: 2 additions & 3 deletions src/hooks/useUpdateThreshold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ export function useUpdateThreshold(
...params,
mutationKey: [MutationKey.UpdateThreshold],
mutationSafeClientFn: async (signerClient, updateThresholdParams) => {
const updateThresholdTx = await signerClient.createChangeThresholdTransaction(
updateThresholdParams
)
const updateThresholdTx =
await signerClient.createChangeThresholdTransaction(updateThresholdParams)
return sendTransactionAsync({ transactions: [updateThresholdTx] })
}
})
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
}
}
Loading

0 comments on commit 4beab17

Please sign in to comment.