forked from OlympusDAO/olympus-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.unit.test.js
executable file
·84 lines (71 loc) · 2.87 KB
/
index.unit.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import axios from "axios";
import { resetAllWhenMocks, when } from "jest-when";
import { getTokenPrice } from "./index";
beforeEach(() => {
resetAllWhenMocks(); //
});
test("getTokenPrice returns expected value", async () => {
const theSpiedMethod = jest.spyOn(axios, "get");
const resp = { data: { coingeckoTicker: { value: 300 } } };
when(theSpiedMethod).calledWith(expect.anything()).mockReturnValue(resp);
let price = await getTokenPrice("olympus");
expect(price).toEqual(300);
});
test("getTokenPrice returns 0 on remote call exceptions", async () => {
const theSpiedMethod = jest.spyOn(axios, "get");
when(theSpiedMethod)
.calledWith(expect.anything())
.mockImplementation(async () => {
throw Error("Remote API down");
});
let price = await getTokenPrice("olympus");
expect(price).toEqual(0);
});
/**
* Enable this test when api.olympusdao.finance allows anon hits.
* Otherwise it throws a CORS access error and the test fails.
*/
test.skip("getTokenPrice via api.olympusdao.finance (real)", async () => {
const theSpiedMethod = jest.spyOn(axios, "get");
when(theSpiedMethod)
.calledWith(expect.stringMatching("https://api.coingecko.com"))
.mockImplementation(async () => {
throw Error("Should not reach this API call if OHM API works.");
});
let price = await getTokenPrice("olympus");
expect(price).toBeGreaterThan(1);
});
test("getTokenPrice via api.olympusdao.finance (mock)", async () => {
const resp = { data: { coingeckoTicker: { value: 356 } } };
const theSpiedMethod = jest.spyOn(axios, "get");
when(theSpiedMethod).calledWith(expect.stringMatching("https://api.olympusdao.finance")).mockReturnValue(resp);
when(theSpiedMethod)
.calledWith(expect.stringMatching("https://api.coingecko.com"))
.mockImplementation(async () => {
throw Error("Should not reach this API call if OHM API works.");
});
let price = await getTokenPrice("olympus");
expect(price).toEqual(356);
});
test("getTokenPrice fallback via api.coingecko.com (real)", async () => {
const theSpiedMethod = jest.spyOn(axios, "get");
when(theSpiedMethod)
.calledWith(expect.stringMatching("https://api.olympusdao.finance"))
.mockImplementation(async () => {
throw Error("Remote OHM API down");
});
let price = await getTokenPrice("olympus");
expect(price).toBeGreaterThan(1);
});
test("getTokenPrice fallback via api.coingecko.com (mock)", async () => {
const theSpiedMethod = jest.spyOn(axios, "get");
when(theSpiedMethod)
.calledWith(expect.stringMatching("https://api.olympusdao.finance"))
.mockImplementation(async () => {
throw Error("Remote OHM API down");
});
const resp = { data: { olympus: { usd: 478 } } };
when(theSpiedMethod).calledWith(expect.stringMatching("https://api.coingecko.com")).mockReturnValue(resp);
let price = await getTokenPrice("olympus");
expect(price).toEqual(478);
});