Skip to content

Commit

Permalink
📄 Add misssing tests (#789)
Browse files Browse the repository at this point in the history
Add misssing tests

Co-authored-by: mateusz <[email protected]>
  • Loading branch information
mj426382 and mateusz authored Jun 1, 2022
1 parent 4619282 commit 06bedac
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- run: pnpm install --frozen-lockfile --strict-peer-dependencies
- run: pnpm run lint
- run: pnpm run build
- run: pnpm run test
- run: pnpm run test -- --timeout 0
test-example:
runs-on: ubuntu-latest
timeout-minutes: 25
Expand Down
27 changes: 25 additions & 2 deletions packages/core/src/hooks/useBlockMeta.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { expect } from 'chai'
import { useBlockMeta } from '..'
import { renderWeb3Hook, sleep } from '../testing'
import { Wallet } from 'ethers'
import { Config, useBlockMeta } from '..'
import { renderDAppHook, renderWeb3Hook, sleep, setupTestingConfig, TestingNetwork } from '../testing'

describe('useBlockMeta', () => {
let network1: TestingNetwork
let config: Config
const receiver = Wallet.createRandom().address

beforeEach(async () => {
;({ config, network1 } = await setupTestingConfig())
await network1.wallets[0].sendTransaction({ to: receiver, value: 100 })
})
it('retrieves block timestamp and difficulty', async () => {
const { result, waitForCurrent } = await renderWeb3Hook(useBlockMeta)
await waitForCurrent((val) => val?.timestamp !== undefined && val?.difficulty !== undefined)
Expand All @@ -24,4 +33,18 @@ describe('useBlockMeta', () => {
await waitForCurrent((val) => val.timestamp?.getTime() !== firstTimestamp?.getTime())
expect(result.current.timestamp).to.be.greaterThan(firstTimestamp)
})

it('updates the block timestamp when a transaction gets mined qith dappHook', async () => {
const { result, waitForCurrent } = await renderDAppHook(useBlockMeta, { config })
await waitForCurrent((val) => val.timestamp !== undefined && val.difficulty !== undefined)

expect(result.error).to.be.undefined
const firstTimestamp = result.current.timestamp

await sleep(1000)
await network1.wallets[0].sendTransaction({ to: receiver, value: 100 })
await sleep(1000)
await waitForCurrent((val) => val.timestamp?.getTime() !== firstTimestamp?.getTime())
expect(result.current.timestamp).to.be.greaterThan(firstTimestamp)
})
})
19 changes: 17 additions & 2 deletions packages/core/src/hooks/useEtherBalance.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Wallet } from 'ethers'
import { useEffect } from 'react'
import { Config } from '../constants'
import { Mainnet } from '../model'
import { TestingNetwork, renderDAppHook, setupTestingConfig, SECOND_TEST_CHAIN_ID } from '../testing'
import { TestingNetwork, renderDAppHook, setupTestingConfig, SECOND_TEST_CHAIN_ID, sleep } from '../testing'
import { useEtherBalance } from './useEtherBalance'
import { useEthers } from './useEthers'

Expand Down Expand Up @@ -34,7 +34,7 @@ describe('useEtherBalance', () => {
expect(result.current).to.eq(100)
})

it('do not change static value when changing ether value', async () => {
it('does not change static value when changing ether value', async () => {
const { result, waitForCurrent } = await renderDAppHook(() => useEtherBalance(receiver, { isStatic: true }), {
config,
})
Expand All @@ -46,6 +46,21 @@ describe('useEtherBalance', () => {
expect(result.current).to.eq(100)
})

it('does change non-static value when changing ether value', async () => {
const { result, waitForCurrent } = await renderDAppHook(() => useEtherBalance(receiver, { isStatic: false }), {
config,
})
await waitForCurrent((val) => val !== undefined)
expect(result.error).to.be.undefined
expect(result.current).to.eq(100)
await sleep(1000)
await network1.wallets[0].sendTransaction({ to: receiver, value: 100 })
await sleep(1000)
await waitForCurrent((val) => val?.toString() === '200')
expect(result.error).to.be.undefined
expect(result.current).to.eq(200)
})

it('defaults to active read-write provider chain id', async () => {
const { result, waitForCurrent } = await renderDAppHook(
() => {
Expand Down

3 comments on commit 06bedac

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.