Skip to content

Commit

Permalink
chores(__tests__): enhance folder structure for tests & harden error …
Browse files Browse the repository at this point in the history
…state tests
  • Loading branch information
D3Portillo committed Jun 15, 2022
1 parent 7cc4e35 commit 9e6f3c3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/useMetamask.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`useMetamask should match empty state & no metamask 1`] = `<div />`;
exports[`useMetamask should match empty state & no metamask installed 1`] = `<div />`;
File renamed without changes.
8 changes: 6 additions & 2 deletions __utils__/index.ts → __tests__/__utils__/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { act } from "@testing-library/react"
import { Metamask } from "../lib/shared"
import { Metamask } from "../../lib/shared"

import { ACCOUNTS } from "./constans"

export const waitForUseEffect = async (node) => {
return act(async () => {
Expand All @@ -10,9 +12,11 @@ export const waitForUseEffect = async (node) => {
export function exposeMetamask(metamaskEth: {
request?: Metamask["request"]
on?: Metamask["on"]
selectedAddress?: string
isMetaMask?: boolean
}) {
const metamask = {
selectedAddress: "ADDRESS",
selectedAddress: null,
isMetaMask: true,
removeListener: jest.fn(),
on: jest.fn(),
Expand Down
11 changes: 7 additions & 4 deletions __tests__/useMetamask.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as React from "react"
import { act, render, RenderResult } from "@testing-library/react"

import { ERRORS } from "../lib/utils/constants"
import { UseMatamaskAPI } from "../lib/shared"
import { useMetamask } from "../lib/index"

import { ACCOUNTS } from "../__utils__/constans"
import { exposeMetamask, waitForUseEffect } from "../__utils__"
import { ACCOUNTS } from "./__utils__/constans"
import { exposeMetamask, waitForUseEffect } from "./__utils__"

function ExposeHook({
onRender = (_: UseMatamaskAPI) => {},
Expand All @@ -24,18 +25,20 @@ describe("useMetamask", () => {
jest.clearAllMocks()
})

it("should match empty state & no metamask", () => {
it("should match empty state & no metamask installed", async () => {
const expectedState = {
chainIdDecimal: 0,
chainId: "0x0",
metamask: {},
error: null,
formattedBalance: "0.00",
balance: 0,
error: ERRORS.METAMASK_NOT_INSTALLED,
}
const onRender = jest.fn()
node = render(<ExposeHook onRender={onRender} />)
await waitForUseEffect(node)
expect(node.container).toMatchSnapshot()
await waitForUseEffect(node)
expect(onRender).toBeCalledWith(expect.objectContaining(expectedState))
})

Expand Down

1 comment on commit 9e6f3c3

@vercel
Copy link

@vercel vercel bot commented on 9e6f3c3 Jun 15, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

forta – ./

forta-git-master-d3portillo.vercel.app
forta.vercel.app
forta-d3portillo.vercel.app

Please sign in to comment.