Skip to content

Commit fcd186d

Browse files
committed
chores(__tests__): add missing tests for utils
1 parent 3579f4c commit fcd186d

File tree

1 file changed

+109
-13
lines changed

1 file changed

+109
-13
lines changed

__tests__/utils.test.ts

Lines changed: 109 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
/**
2-
* @TODO
3-
* export function connectToMetamask(): Promise<string>
4-
* export function formatEther(balance: number): string
5-
* export function getMetamaskProvider(): Metamask | null
6-
* export function getFIATBalance(tokenBalance, tokenPrice): number
7-
* export const metamaskRequest: OnRequest["request"]
8-
*/
9-
101
import {
112
addEtherNetwork,
123
addEtherToken,
134
sendEther,
145
switchEtherNetwork,
156
switchOrAppendNetwork,
7+
connectToMetamask,
8+
metamaskRequest,
9+
getMetamaskProvider,
10+
getFIATValue,
11+
formatEther,
1612
} from "../lib"
1713
import { ERRORS } from "../lib/utils/constants"
14+
1815
import { defineWindowReload, exposeMetamask } from "./__utils__"
16+
import { ACCOUNTS } from "./__utils__/constans"
17+
18+
const INTERNAL_ERROR = {
19+
code: 0,
20+
message: "INTERNAL_ERROR",
21+
}
1922

2023
describe("utils", () => {
2124
beforeEach(() => {
@@ -35,6 +38,7 @@ describe("utils", () => {
3538
* function addEtherToken(props: AddEtherToken): Promise<null>
3639
* function switchEtherNetwork(chainId: string): Promise<null>
3740
* function switchOrAppendNetwork(props: AddEtherNetwork): Promise<null>
41+
* function metamaskRequest({ method, params }): any
3842
*/
3943
const INPUT = {} as any
4044
const ERROR = ERRORS.METAMASK_NOT_INSTALLED
@@ -44,15 +48,12 @@ describe("utils", () => {
4448
await expect(addEtherToken(INPUT)).rejects.toEqual(ERROR)
4549
await expect(switchEtherNetwork(INPUT)).rejects.toEqual(ERROR)
4650
await expect(switchOrAppendNetwork(INPUT)).rejects.toEqual(ERROR)
51+
await expect(metamaskRequest(INPUT)).rejects.toEqual(ERROR)
4752

4853
/**
4954
* In this context withInternalError means that window.ethereum exists
5055
* for the `useMetamask` to function but metamask.request fails
5156
*/
52-
const INTERNAL_ERROR = {
53-
code: 0,
54-
message: "INTERNAL_ERROR",
55-
}
5657
exposeMetamask({
5758
request: jest.fn().mockRejectedValue(INTERNAL_ERROR),
5859
})
@@ -61,6 +62,7 @@ describe("utils", () => {
6162
await expect(addEtherToken(INPUT)).rejects.toEqual(INTERNAL_ERROR)
6263
await expect(switchEtherNetwork(INPUT)).rejects.toEqual(INTERNAL_ERROR)
6364
await expect(switchOrAppendNetwork(INPUT)).rejects.toEqual(INTERNAL_ERROR)
65+
await expect(metamaskRequest(INPUT)).rejects.toEqual(INTERNAL_ERROR)
6466
})
6567
it("[Ether,Network,Token]:: resolves withState=SUCCESS", async () => {
6668
/**
@@ -70,6 +72,7 @@ describe("utils", () => {
7072
* function addEtherToken(props: AddEtherToken): Promise<null>
7173
* function switchEtherNetwork(chainId: string): Promise<null>
7274
* function switchOrAppendNetwork(props: AddEtherNetwork): Promise<null>
75+
* function metamaskRequest({ method, params }): any
7376
*/
7477
const SUCCESS = "SUCCESS"
7578
const INPUT = {} as any
@@ -82,6 +85,7 @@ describe("utils", () => {
8285
await expect(addEtherToken(INPUT)).resolves.toEqual(SUCCESS)
8386
await expect(switchEtherNetwork(INPUT)).resolves.toEqual(SUCCESS)
8487
await expect(switchOrAppendNetwork(INPUT)).resolves.toEqual(SUCCESS)
88+
await expect(metamaskRequest(INPUT)).resolves.toEqual(SUCCESS)
8589
})
8690

8791
it("addEtherNetwork:: reloadOnSuccess=true", async () => {
@@ -95,4 +99,96 @@ describe("utils", () => {
9599
).resolves.toEqual(expectedState)
96100
expect(mockReload).toHaveBeenCalled()
97101
})
102+
103+
it("connectToMetamask:: FAILULRE", async () => {
104+
// Fail when window.ethereum=undefined
105+
await expect(connectToMetamask()).rejects.toEqual(
106+
ERRORS.METAMASK_NOT_INSTALLED
107+
)
108+
109+
// Fail for metamak.request
110+
exposeMetamask({
111+
request: jest.fn().mockRejectedValue(INTERNAL_ERROR),
112+
})
113+
await expect(connectToMetamask()).rejects.toEqual(INTERNAL_ERROR)
114+
})
115+
it("connectToMetamask:: resolves with `connectedAccount`", async () => {
116+
const connectedAccount = ACCOUNTS[0]
117+
exposeMetamask({
118+
request: jest.fn().mockResolvedValue(ACCOUNTS),
119+
})
120+
await expect(connectToMetamask()).resolves.toEqual(connectedAccount)
121+
})
122+
123+
it("getMetamaskProvider:: `isUserUnlocked`, provider=null, isMetaMask=false", () => {
124+
expect(getMetamaskProvider()).toBe(null)
125+
126+
// set isMetaMask=true but not define `_metamask`
127+
exposeMetamask()
128+
let provider = getMetamaskProvider()!
129+
expect(provider["_metamask"]).toBeUndefined()
130+
expect(jest.isMockFunction(provider.isUserUnlocked)).toBeFalsy()
131+
132+
// Inject a mock Fn to _metamask.isUnlocked - validate it's the one served
133+
exposeMetamask({
134+
_metamask: {
135+
/** ethereum._metamask.isUnlocked */
136+
isUnlocked: jest.fn(),
137+
},
138+
})
139+
provider = getMetamaskProvider()!
140+
expect(provider["_metamask"]).toBeDefined()
141+
expect(jest.isMockFunction(provider.isUserUnlocked)).toBeTruthy()
142+
})
143+
144+
it("getFIATValue:: console.error - defaultFormatter", () => {
145+
const consoleError = jest
146+
.spyOn(console, "error")
147+
.mockImplementation(() => {})
148+
expect(getFIATValue(1, 1, "NONE")).toEqual("1")
149+
expect(consoleError).toHaveBeenCalledWith(expect.stringMatching("Error"))
150+
consoleError.mockRestore()
151+
})
152+
it("getFIATValue:: currency=USD", () => {
153+
expect(getFIATValue(1, 0)).toEqual("0")
154+
expect(getFIATValue(1, "0")).toEqual("0")
155+
expect(getFIATValue(1, 1)).toEqual("1")
156+
expect(getFIATValue(999, ".05")).toEqual("49.95")
157+
expect(getFIATValue("70000", 30_000)).toEqual("2,100,000,000")
158+
expect(getFIATValue("77", "33")).toEqual("2,541")
159+
})
160+
it("getFIATValue:: currency=JPY", () => {
161+
// Number with significant decimal value
162+
const signiDecs = 123456.789
163+
const getValue = (a, b) => getFIATValue(a, b, "JPY")
164+
165+
expect(getValue(signiDecs, 0)).toEqual("0")
166+
expect(getValue(1, "0")).toEqual("0")
167+
expect(getValue(1, signiDecs)).toEqual("123,456.789")
168+
expect(getValue(signiDecs, ".05")).toEqual("6,172.839")
169+
expect(getValue(signiDecs, 30_000)).toEqual("3,703,703,670")
170+
expect(getFIATValue(999, ".05")).toEqual("49.95")
171+
})
172+
it("getFIATValue:: currency=EUR", () => {
173+
// Number with significant decimal value
174+
const signiDecs = 123456.789
175+
const getValue = (a, b) => getFIATValue(a, b, "EUR")
176+
177+
expect(getValue(signiDecs, 0)).toEqual("0")
178+
expect(getValue(1, signiDecs)).toEqual("123,456.789")
179+
expect(getValue(signiDecs, 30_000)).toEqual("3,703,703,670")
180+
expect(getFIATValue(999, ".05")).toEqual("49.95")
181+
})
182+
183+
it("formatEther:: formats positive numbers", () => {
184+
// Evaluate contrain to 1e-4 as min value
185+
expect(formatEther(1e-5)).toBe("0.0001")
186+
expect(formatEther(0)).toBe("0")
187+
expect(formatEther(1.2)).toBe("1.2")
188+
expect(formatEther(33)).toBe("33")
189+
expect(formatEther(89232.42000000231)).toBe("89232.42")
190+
expect(formatEther(89232.00000042)).toBe("89232")
191+
expect(formatEther(89232.00429)).toBe("89232.0043")
192+
expect(formatEther(89232.00424)).toBe("89232.0042")
193+
})
98194
})

0 commit comments

Comments
 (0)