Skip to content

Commit

Permalink
Fix account abstraction kit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv committed May 6, 2024
1 parent e087a6b commit b6ec4c8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
62 changes: 32 additions & 30 deletions packages/account-abstraction-kit/src/AccountAbstraction.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Safe, { predictSafeAddress } from '@safe-global/protocol-kit'
import Safe, { predictSafeAddress, SafeProvider } from '@safe-global/protocol-kit'
import { GelatoRelayPack } from '@safe-global/relay-kit'
import { SafeTransaction } from '@safe-global/safe-core-sdk-types'
import AccountAbstraction from './AccountAbstraction'
Expand All @@ -9,70 +9,72 @@ jest.mock('@safe-global/relay-kit')
const GelatoRelayPackMock = GelatoRelayPack as jest.MockedClass<typeof GelatoRelayPack>
const predictSafeAddressMock = predictSafeAddress as jest.MockedFunction<typeof predictSafeAddress>
const SafeMock = Safe as jest.MockedClass<typeof Safe>
const SafeProviderMock = SafeProvider as jest.MockedClass<typeof SafeProvider>

describe('AccountAbstraction', () => {
const provider = {
request: jest.fn()
}
const safeProvider = {
getSignerAddress: jest.fn(),
isContractDeployed: jest.fn(),
getChainId: jest.fn()
}

const signerAddress = '0xSignerAddress'
const predictSafeAddress = '0xPredictSafeAddressMock'

beforeEach(() => {
jest.clearAllMocks()
safeProvider.getSignerAddress.mockResolvedValue(signerAddress)
predictSafeAddressMock.mockResolvedValue(predictSafeAddress)
SafeProviderMock.prototype.getSignerAddress.mockResolvedValue(signerAddress)
})

describe('init', () => {
const accountAbstraction = new AccountAbstraction({ provider })
const accountAbstraction = new AccountAbstraction({ provider, signer: signerAddress })

it('should initialize a Safe instance with its address if contract is deployed already', async () => {
safeProvider.isContractDeployed.mockResolvedValueOnce(true)
SafeProviderMock.prototype.isContractDeployed.mockResolvedValueOnce(true)

await accountAbstraction.init()

expect(safeProvider.getSignerAddress).toHaveBeenCalledTimes(1)
expect(SafeProviderMock.prototype.getSignerAddress).toHaveBeenCalledTimes(1)
expect(predictSafeAddressMock).toHaveBeenCalledTimes(1)
expect(predictSafeAddressMock).toHaveBeenCalledWith({
safeProvider,
safeAccountConfig: { owners: ['0xSignerAddress'], threshold: 1 }
})
expect(predictSafeAddressMock).toHaveBeenCalledWith(
expect.objectContaining({
safeAccountConfig: { owners: ['0xSignerAddress'], threshold: 1 }
})
)
expect(SafeMock.create).toHaveBeenCalledTimes(1)
expect(SafeMock.create).toHaveBeenCalledWith({
safeProvider,
safeAddress: predictSafeAddress
})
expect(SafeMock.create).toHaveBeenCalledWith(
expect.objectContaining({
safeAddress: predictSafeAddress
})
)
})

it('should initialize a Safe instance with a config if contract is NOT deployed yet', async () => {
safeProvider.isContractDeployed.mockResolvedValueOnce(false)
SafeProviderMock.prototype.isContractDeployed.mockResolvedValueOnce(false)

await accountAbstraction.init()

expect(safeProvider.getSignerAddress).toHaveBeenCalledTimes(1)
expect(SafeProviderMock.prototype.getSignerAddress).toHaveBeenCalledTimes(1)
expect(predictSafeAddressMock).toHaveBeenCalledTimes(1)
expect(predictSafeAddressMock).toHaveBeenCalledWith({
safeProvider,
safeAccountConfig: { owners: ['0xSignerAddress'], threshold: 1 }
})
expect(predictSafeAddressMock).toHaveBeenCalledWith(
expect.objectContaining({
safeAccountConfig: { owners: ['0xSignerAddress'], threshold: 1 }
})
)
expect(SafeMock.create).toHaveBeenCalledTimes(1)
expect(SafeMock.create).toHaveBeenCalledWith({
safeProvider,
predictedSafe: { safeAccountConfig: { owners: ['0xSignerAddress'], threshold: 1 } }
})
expect(SafeMock.create).toHaveBeenCalledWith(
expect.objectContaining({
predictedSafe: { safeAccountConfig: { owners: ['0xSignerAddress'], threshold: 1 } }
})
)
})

it('should throw an error if the provider has not a signer', async () => {
safeProvider.getSignerAddress.mockResolvedValueOnce(undefined)
SafeProviderMock.prototype.getSignerAddress.mockResolvedValueOnce(undefined)

expect(accountAbstraction.init()).rejects.toThrow(
`There's no signer in the provided SafeProvider`
`There's no signer available with the provided config (provider, signer)`
)

expect(SafeMock.create).not.toHaveBeenCalled()
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/account-abstraction-kit/src/AccountAbstraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AccountAbstraction {
const signer = await safeProvider.getSignerAddress()

if (!signer) {
throw new Error("There's no signer available in the provided config")
throw new Error("There's no signer available with the provided config (provider, signer)")
}

const owners = [signer]
Expand Down

0 comments on commit b6ec4c8

Please sign in to comment.