Skip to content

Commit b11d94f

Browse files
committed
Add exchgane service unit tests
1 parent 1371d7e commit b11d94f

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import axios from "axios"
2+
import BigNumber from "bignumber.js"
3+
4+
import {
5+
BaseExchangeService,
6+
UniswapExchangeService,
7+
} from "../exchange-service"
8+
9+
jest.mock("axios")
10+
11+
describe("Test exchange service", () => {
12+
describe("Test BaseExchangeService", () => {
13+
const exchange = new BaseExchangeService()
14+
exchange._getUniswapPairData = jest.fn()
15+
exchange._getKeepTokenPriceInUSD = jest.fn()
16+
exchange._getBTCPriceInUSD = jest.fn()
17+
const mockedPairId = 1
18+
19+
test("should call function that implements fetching uniswap pair data", () => {
20+
exchange.getUniswapPairData(mockedPairId)
21+
22+
expect(exchange._getUniswapPairData).toHaveBeenCalledWith(mockedPairId)
23+
})
24+
25+
test("should call function that implements fetching KEEP token price", () => {
26+
exchange.getKeepTokenPriceInUSD()
27+
28+
expect(exchange._getKeepTokenPriceInUSD).toHaveBeenCalled()
29+
})
30+
31+
test("should call function that implements fetching BTC price", () => {
32+
exchange.getBTCPriceInUSD()
33+
34+
expect(exchange._getBTCPriceInUSD).toHaveBeenCalled()
35+
})
36+
})
37+
38+
describe("Test UniswapExchangeService", () => {
39+
const exchange = new UniswapExchangeService()
40+
const mockedPairId = 1
41+
42+
const mockedResponse = {
43+
data: {
44+
data: {
45+
pair: {
46+
reserveUSD: 30000,
47+
reserveETH: 1000,
48+
token0: {
49+
derivedETH: 0.2,
50+
},
51+
},
52+
},
53+
},
54+
}
55+
56+
test("should fetch uniswap pair data correctly", async () => {
57+
axios.post.mockResolvedValue(mockedResponse)
58+
59+
const result = await exchange.getUniswapPairData(mockedPairId)
60+
61+
const mockCalls = axios.post.mock.calls
62+
expect(axios.post).toHaveBeenCalled()
63+
expect(mockCalls[0][0]).toEqual(exchange.UNISWAP_API_URL)
64+
expect(
65+
mockCalls[0][1].query.toString().includes(mockedPairId)
66+
).toBeTruthy()
67+
68+
expect(result).toStrictEqual(mockedResponse.data.data.pair)
69+
})
70+
71+
test("should fetch keep token price correctly", async () => {
72+
const spy = jest.spyOn(exchange, "_getTokenPriceInUSD")
73+
const getUniswapPairDataSpy = jest.spyOn(exchange, "_getUniswapPairData")
74+
axios.post.mockResolvedValue(mockedResponse)
75+
const pairData = mockedResponse.data.data.pair
76+
const expectedPrice = new BigNumber(pairData.reserveUSD)
77+
.div(pairData.reserveETH)
78+
.multipliedBy(pairData.token0.derivedETH)
79+
80+
const result = await exchange.getKeepTokenPriceInUSD()
81+
82+
expect(spy).toHaveBeenCalledWith(
83+
"0xe6f19dab7d43317344282f803f8e8d240708174a"
84+
)
85+
expect(getUniswapPairDataSpy).toHaveBeenCalledWith(
86+
"0xe6f19dab7d43317344282f803f8e8d240708174a"
87+
)
88+
expect(result).toEqual(expectedPrice)
89+
})
90+
91+
test("should fetch BTC price correctly", () => {
92+
const spy = jest
93+
.spyOn(exchange, "_getTokenPriceInUSD")
94+
.mockResolvedValue("300")
95+
96+
exchange.getBTCPriceInUSD()
97+
98+
expect(spy).toHaveBeenCalledWith(
99+
"0xe6f19dab7d43317344282f803f8e8d240708174a"
100+
)
101+
})
102+
})
103+
})

0 commit comments

Comments
 (0)