Skip to content

Commit

Permalink
tests: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schmanu committed Aug 3, 2023
1 parent eaaee8e commit 4347d53
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ describe('StatusStep', () => {
/>,
)

expect(useSafeCreationSpy).toHaveBeenCalledWith(
undefined,
expect.anything(),
SafeCreationStatus.PROCESSING,
expect.anything(),
true,
)
expect(useSafeCreationSpy).toHaveBeenCalledWith(SafeCreationStatus.PROCESSING, expect.anything(), true)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as wallet from '@/hooks/wallets/useWallet'
import * as logic from '@/components/new-safe/create/logic'
import * as contracts from '@/services/contracts/safeContracts'
import * as txMonitor from '@/services/tx/txMonitor'
import * as usePendingSafe from '@/components/new-safe/create/steps/StatusStep/usePendingSafe'
import { JsonRpcProvider, Web3Provider } from '@ethersproject/providers'
import type { ConnectedWallet } from '@/hooks/wallets/useOnboard'
import type { ChainInfo } from '@safe-global/safe-gateway-typescript-sdk'
Expand Down Expand Up @@ -34,9 +35,7 @@ describe('useSafeCreation', () => {
saltNonce: 123,
address: '0x10',
}

const mockSetPendingSafe = jest.fn()

const mockStatus = SafeCreationStatus.AWAITING
const mockSetStatus = jest.fn()
const mockProvider: Web3Provider = new Web3Provider(jest.fn())
Expand Down Expand Up @@ -66,7 +65,7 @@ describe('useSafeCreation', () => {

it('should create a safe with gas params if there is no txHash and status is AWAITING', async () => {
const createSafeSpy = jest.spyOn(logic, 'createNewSafe').mockReturnValue(Promise.resolve({} as Safe))

jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([mockPendingSafe, mockSetPendingSafe])
renderHook(() => useSafeCreation(mockStatus, mockSetStatus, false))

await waitFor(() => {
Expand Down Expand Up @@ -97,6 +96,8 @@ describe('useSafeCreation', () => {
features: [FEATURES.EIP1559],
} as unknown as ChainInfo),
)
jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([mockPendingSafe, mockSetPendingSafe])

const createSafeSpy = jest.spyOn(logic, 'createNewSafe').mockReturnValue(Promise.resolve({} as Safe))

renderHook(() => useSafeCreation(mockStatus, mockSetStatus, false))
Expand All @@ -115,6 +116,7 @@ describe('useSafeCreation', () => {

it('should create a safe with no gas params if the gas estimation threw, there is no txHash and status is AWAITING', async () => {
jest.spyOn(gasPrice, 'default').mockReturnValue([undefined, Error('Error for testing'), false])
jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([mockPendingSafe, mockSetPendingSafe])

const createSafeSpy = jest.spyOn(logic, 'createNewSafe').mockReturnValue(Promise.resolve({} as Safe))

Expand All @@ -133,6 +135,7 @@ describe('useSafeCreation', () => {

it('should not create a safe if there is no txHash, status is AWAITING but gas is loading', async () => {
jest.spyOn(gasPrice, 'default').mockReturnValue([undefined, undefined, true])
jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([mockPendingSafe, mockSetPendingSafe])

const createSafeSpy = jest.spyOn(logic, 'createNewSafe').mockReturnValue(Promise.resolve({} as Safe))

Expand All @@ -145,6 +148,7 @@ describe('useSafeCreation', () => {

it('should not create a safe if the status is not AWAITING', async () => {
const createSafeSpy = jest.spyOn(logic, 'createNewSafe')
jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([mockPendingSafe, mockSetPendingSafe])

renderHook(() => useSafeCreation(SafeCreationStatus.WALLET_REJECTED, mockSetStatus, false))

Expand Down Expand Up @@ -197,6 +201,9 @@ describe('useSafeCreation', () => {

it('should not create a safe if there is a txHash', async () => {
const createSafeSpy = jest.spyOn(logic, 'createNewSafe')
jest
.spyOn(usePendingSafe, 'usePendingSafe')
.mockReturnValue([{ ...mockPendingSafe, txHash: '0x123' }, mockSetPendingSafe])

renderHook(() => useSafeCreation(SafeCreationStatus.AWAITING, mockSetStatus, false))

Expand All @@ -207,7 +214,21 @@ describe('useSafeCreation', () => {

it('should watch a tx if there is a txHash and a tx object', async () => {
const watchSafeTxSpy = jest.spyOn(logic, 'checkSafeCreationTx')

jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([
{
...mockPendingSafe,
txHash: '0x123',
tx: {
data: '0x',
from: '0x1234',
nonce: 0,
startBlock: 0,
to: '0x456',
value: BigNumber.from(0),
},
},
mockSetPendingSafe,
])
renderHook(() => useSafeCreation(mockStatus, mockSetStatus, false))

await waitFor(() => {
Expand All @@ -217,6 +238,21 @@ describe('useSafeCreation', () => {

it('should watch a tx even if no wallet is connected', async () => {
jest.spyOn(wallet, 'default').mockReturnValue(null)
jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([
{
...mockPendingSafe,
txHash: '0x123',
tx: {
data: '0x',
from: '0x1234',
nonce: 0,
startBlock: 0,
to: '0x456',
value: BigNumber.from(0),
},
},
mockSetPendingSafe,
])
const watchSafeTxSpy = jest.spyOn(logic, 'checkSafeCreationTx')

renderHook(() => useSafeCreation(mockStatus, mockSetStatus, false))
Expand All @@ -228,7 +264,7 @@ describe('useSafeCreation', () => {

it('should not watch a tx if there is no txHash', async () => {
const watchSafeTxSpy = jest.spyOn(logic, 'checkSafeCreationTx')

jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([mockPendingSafe, mockSetPendingSafe])
renderHook(() => useSafeCreation(mockStatus, mockSetStatus, false))

await waitFor(() => {
Expand All @@ -238,7 +274,20 @@ describe('useSafeCreation', () => {

it('should not watch a tx if there is no tx object', async () => {
const watchSafeTxSpy = jest.spyOn(logic, 'checkSafeCreationTx')

jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([
{
...mockPendingSafe,
tx: {
data: '0x',
from: '0x1234',
nonce: 0,
startBlock: 0,
to: '0x456',
value: BigNumber.from(0),
},
},
mockSetPendingSafe,
])
renderHook(() => useSafeCreation(mockStatus, mockSetStatus, false))

await waitFor(() => {
Expand All @@ -247,6 +296,22 @@ describe('useSafeCreation', () => {
})

it('should set a PROCESSING state when watching a tx', async () => {
jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([
{
...mockPendingSafe,
txHash: '0x123',
tx: {
data: '0x',
from: '0x1234',
nonce: 0,
startBlock: 0,
to: '0x456',
value: BigNumber.from(0),
},
},
mockSetPendingSafe,
])

renderHook(() => useSafeCreation(mockStatus, mockSetStatus, false))

await waitFor(() => {
Expand All @@ -256,7 +321,12 @@ describe('useSafeCreation', () => {

it('should set a PROCESSING state and monitor relay taskId after successfully tx relay', async () => {
jest.spyOn(logic, 'relaySafeCreation').mockResolvedValue('0x456')

jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([
{
...mockPendingSafe,
},
mockSetPendingSafe,
])
const txMonitorSpy = jest.spyOn(txMonitor, 'waitForCreateSafeTx').mockImplementation(jest.fn())

const initialStatus = SafeCreationStatus.PROCESSING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import { SafeCreationStatus } from '@/components/new-safe/create/steps/StatusSte
import { type SafeInfo } from '@safe-global/safe-gateway-typescript-sdk'
import * as web3 from '@/hooks/wallets/web3'
import * as pendingSafe from '@/components/new-safe/create/logic'
import * as usePendingSafe from '@/components/new-safe/create/steps/StatusStep/usePendingSafe'
import * as addressbook from '@/components/new-safe/create/logic/address-book'
import { Web3Provider } from '@ethersproject/providers'
import useSafeCreationEffects from '@/components/new-safe/create/steps/StatusStep/useSafeCreationEffects'
import type { PendingSafeData } from '@/components/new-safe/create/types'
import { hexZeroPad } from 'ethers/lib/utils'

describe('useSafeCreationEffects', () => {
beforeEach(() => {
jest.resetAllMocks()
jest.spyOn(pendingSafe, 'pollSafeInfo').mockImplementation(jest.fn(() => Promise.resolve({} as SafeInfo)))
jest.spyOn(addressbook, 'updateAddressBook').mockReturnValue(() => {})

const mockProvider: Web3Provider = new Web3Provider(jest.fn())
jest.spyOn(web3, 'useWeb3').mockImplementation(() => mockProvider)
Expand All @@ -18,6 +23,9 @@ describe('useSafeCreationEffects', () => {
it('should clear the tx hash if it exists on ERROR or REVERTED', () => {
const setStatusSpy = jest.fn()
const setPendingSafeSpy = jest.fn()
jest
.spyOn(usePendingSafe, 'usePendingSafe')
.mockReturnValue([{ txHash: '0x123' } as PendingSafeData, setPendingSafeSpy])

renderHook(() =>
useSafeCreationEffects({
Expand All @@ -32,7 +40,7 @@ describe('useSafeCreationEffects', () => {
it('should not clear the tx hash if it doesnt exist on ERROR or REVERTED', () => {
const setStatusSpy = jest.fn()
const setPendingSafeSpy = jest.fn()

jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([{} as PendingSafeData, setPendingSafeSpy])
renderHook(() =>
useSafeCreationEffects({
status: SafeCreationStatus.ERROR,
Expand All @@ -47,7 +55,9 @@ describe('useSafeCreationEffects', () => {
const pollSafeInfoSpy = jest.spyOn(pendingSafe, 'pollSafeInfo')
const setStatusSpy = jest.fn()
const setPendingSafeSpy = jest.fn()

jest
.spyOn(usePendingSafe, 'usePendingSafe')
.mockReturnValue([{ safeAddress: hexZeroPad('0x123', 20) } as PendingSafeData, setPendingSafeSpy])
renderHook(() =>
useSafeCreationEffects({
status: SafeCreationStatus.SUCCESS,
Expand All @@ -62,7 +72,7 @@ describe('useSafeCreationEffects', () => {
const pollSafeInfoSpy = jest.spyOn(pendingSafe, 'pollSafeInfo')
const setStatusSpy = jest.fn()
const setPendingSafeSpy = jest.fn()

jest.spyOn(usePendingSafe, 'usePendingSafe').mockReturnValue([{} as PendingSafeData, setPendingSafeSpy])
renderHook(() =>
useSafeCreationEffects({
status: SafeCreationStatus.SUCCESS,
Expand Down

0 comments on commit 4347d53

Please sign in to comment.