Skip to content

Commit

Permalink
adds test for token list fetching (#361)
Browse files Browse the repository at this point in the history
* adds test for token list fetching

* removes outdated testcase

* makes loading of token list more robust

* Disabled @typescript-eslint/no-unused-expressions for test sources
We use chai for testing and the empty check is always a false positive finding.
  • Loading branch information
schmanu authored Mar 15, 2022
1 parent c6ca4b0 commit f8e1892
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ module.exports = {
},
],
},
overrides: [
{
files: ["*.test.ts", "*.test.tsx"],
rules: {
"@typescript-eslint/no-unused-expressions": "off",
},
},
],
};
1 change: 0 additions & 1 deletion src/__tests__/balanceCheck.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import BigNumber from "bignumber.js";
import { expect } from "chai";

Expand Down
1 change: 0 additions & 1 deletion src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import { BigNumber } from "bignumber.js";
import * as chai from "chai";
import { expect } from "chai";
Expand Down
74 changes: 66 additions & 8 deletions src/__tests__/tokenList.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,77 @@
import { expect } from "chai";
import { ethers } from "ethers";

import { fetchTokenList } from "../hooks/token";
import { networkInfo } from "../networks";

describe("fetchTokenList()", () => {
beforeEach(() => {
jest.spyOn(window, "fetch").mockImplementation(() => {
return Promise.resolve({
json: () =>
Promise.resolve({
tokens: [
{
name: "Gnosis",
address: "0x6810e776880c02933d47db1b9fc05908e5386b96",
symbol: "GNO",
decimals: 18,
},
{
name: "Dai Stablecoin",
address: "0x6b175474e89094c44da98b954eedeac495271d0f",
symbol: "DAI",
decimals: 18,
},
{
name: "Spam Token",
address: "",
symbol: "SPAM",
decimals: 0,
},
],
}),
status: 200,
} as any);
});
});

describe("Mainnet tokenlist", () => {
it("Should parse its response correctly", async () => {
const resultingTokens = await fetchTokenList(1);
const gnoAddress = ethers.utils.getAddress("0x6810e776880c02933d47db1b9fc05908e5386b96");

expect(resultingTokens).to.have.lengthOf(2);
expect(resultingTokens.get(gnoAddress)?.symbol).to.equal("GNO");
expect(resultingTokens.get(gnoAddress)?.address.toLowerCase()).eq(gnoAddress.toLowerCase());
expect(resultingTokens.get(gnoAddress)?.decimals).to.equal(18);
});

it("Should not crash on errors", async () => {
jest.spyOn(window, "fetch").mockImplementation(() => {
return Promise.reject("Unexpected error.");
});

const resultingTokens = await fetchTokenList(1);
expect(resultingTokens).to.be.empty;
});

it("Should not crash on unsuccessful fetches", async () => {
jest.spyOn(window, "fetch").mockImplementation(() => {
return Promise.resolve({
status: 404,
statusText: "Page not found",
} as any);
});

const resultingTokens = await fetchTokenList(1);
expect(resultingTokens).to.be.empty;
});
});

describe("Fetch should resolve for all networks", () => {
for (const chainId of networkInfo.keys()) {
it(`fetches tokens for ${networkInfo.get(chainId)?.name} network`, () => {
expect(() => fetchTokenList(chainId)).to.not.throw();
});
}
});

describe("useTokenList()", () => {
it("Throws on unknown networks", () => {
// TODO - not sure how to test this.
expect(1).to.equal(1);
});
});
5 changes: 4 additions & 1 deletion src/hooks/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const fetchTokenList = async (chainId: number): Promise<TokenMap> => {
switch (chainId) {
case 1:
const mainnetTokenURL = "https://tokens.coingecko.com/uniswap/all.json";
tokens = (await (await fetch(mainnetTokenURL)).json()).tokens;
tokens = await fetch(mainnetTokenURL)
.then((response) => response.json())
.then((response) => response.tokens)
.catch(() => []);
break;
case 4:
// Hardcoded this because the list provided at
Expand Down

0 comments on commit f8e1892

Please sign in to comment.