-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds test for token list fetching (#361)
* 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
Showing
5 changed files
with
78 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters