react.spec.tsx 2.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
import matchers from '@testing-library/jest-dom/matchers'
import { cleanup, waitFor } from '@testing-library/react'
import { renderHook } from '@testing-library/react-hooks'
import { afterEach, expect, test } from 'vitest'
import { useMintManagerOwner } from './react'
import { configureChains, createConfig, WagmiConfig } from 'wagmi'
import * as React from 'react'
import { optimism } from 'viem/chains'
import { jsonRpcProvider } from 'wagmi/providers/jsonRpc'

expect.extend(matchers)

afterEach(() => {
  cleanup()
})

const { publicClient } = configureChains(
  [optimism],
  [
    jsonRpcProvider({
      rpc: () => ({
        http:
          import.meta.env.VITE_RPC_URL_L2_MAINNET ??
          'https://mainnet.optimism.io',
      }),
    }),
  ]
)

const config = createConfig({
  publicClient: ({ chainId }) => publicClient({ chainId }),
})

const blockNumber = BigInt(106806163)

test('react hooks should work', async () => {
  const hook = renderHook(
    () => useMintManagerOwner({ chainId: 10, blockNumber }),
    {
      wrapper: ({ children }) => (
        <WagmiConfig config={config}>{children}</WagmiConfig>
      ),
    }
  )
  await waitFor(() => {
    hook.rerender()
    if (hook.result.current.error) throw hook.result.current.error
    expect(hook.result.current?.data).toBeDefined()
  })
  const normalizedResult = {
    ...hook.result.current,
    internal: {
      ...hook.result.current.internal,
      dataUpdatedAt: 'SNAPSHOT_TEST_REMOVED!!!',
    },
  }
  expect(normalizedResult).toMatchInlineSnapshot(`
    {
      "data": "0x2A82Ae142b2e62Cb7D10b55E323ACB1Cab663a26",
      "error": null,
      "fetchStatus": "idle",
      "internal": {
        "dataUpdatedAt": "SNAPSHOT_TEST_REMOVED!!!",
        "errorUpdatedAt": 0,
        "failureCount": 0,
        "isFetchedAfterMount": true,
        "isLoadingError": false,
        "isPaused": false,
        "isPlaceholderData": false,
        "isPreviousData": false,
        "isRefetchError": false,
        "isStale": true,
        "remove": [Function],
      },
      "isError": false,
      "isFetched": true,
      "isFetchedAfterMount": true,
      "isFetching": false,
      "isIdle": false,
      "isLoading": false,
      "isRefetching": false,
      "isSuccess": true,
      "refetch": [Function],
      "status": "success",
    }
  `)
})