Skip to content

Commit

Permalink
Added unit tests for context.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
alfetopito committed Jun 7, 2022
1 parent 1609b3e commit 5453256
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/utils/context.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Context } from './context'
import { VoidSigner } from '@ethersproject/abstract-signer'
import { Provider } from '@ethersproject/providers'

test('Context: update chainId', async () => {
const context = new Context(1, {})
let chainId = await context.chainId
expect(chainId).toEqual(1)

// works
chainId = await context.updateChainId(4)
expect(chainId).toEqual(4)
})

test('Context: update chainId fails', () => {
const context = new Context(1, {})

expect(() => {
context.updateChainId(123)
}).toThrow('Invalid chainId: 123')
})

test('Context: get chainId from provider - matches context', async () => {
const mockProvider = jest.fn<Provider, []>()
const provider = new mockProvider()
provider.getNetwork = jest.fn(async () => ({ chainId: 1, name: 'bla' }))

const signer = new VoidSigner('', provider)

const context = new Context(1, { signer })

const chainId = await context.chainId

expect(chainId).toEqual(1)
expect(provider.getNetwork).toHaveBeenCalledTimes(1)
})

test('Context: get chainId from provider - differs from context', async () => {
const mockProvider = jest.fn<Provider, []>()
const provider = new mockProvider()
provider.getNetwork = jest.fn(async () => ({ chainId: 100, name: 'bla' }))

const signer = new VoidSigner('', provider)

const context = new Context(1, { signer })

const chainId = await context.chainId

expect(chainId).toEqual(100)
expect(provider.getNetwork).toHaveBeenCalledTimes(1)
})

0 comments on commit 5453256

Please sign in to comment.