Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Oct 3, 2023
1 parent cd0de7a commit c9b92b0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
47 changes: 26 additions & 21 deletions packages/assets-controllers/src/TokenListController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import { NetworkStatus } from '@metamask/network-controller';
import nock from 'nock';
import * as sinon from 'sinon';

import {
TOKEN_END_POINT_API,
fetchTokenList as tokenServiceFetchTokenList,
} from './token-service';
import * as tokenService from './token-service';
import type {
TokenListStateChange,
GetTokenListState,
Expand Down Expand Up @@ -642,7 +639,7 @@ describe('TokenListController', () => {
});

it('should update tokenList state when network updates are passed via onNetworkStateChange callback', async () => {
nock(TOKEN_END_POINT_API)
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.mainnet)}`)
.reply(200, sampleMainnetTokenList)
.persist();
Expand Down Expand Up @@ -805,7 +802,7 @@ describe('TokenListController', () => {
});

it('should update token list from api', async () => {
nock(TOKEN_END_POINT_API)
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.mainnet)}`)
.reply(200, sampleMainnetTokenList)
.persist();
Expand Down Expand Up @@ -843,12 +840,12 @@ describe('TokenListController', () => {
});

it('should update the cache before threshold time if the current data is undefined', async () => {
nock(TOKEN_END_POINT_API)
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.mainnet)}`)
.once()
.reply(200, undefined);

nock(TOKEN_END_POINT_API)
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.mainnet)}`)
.reply(200, sampleMainnetTokenList)
.persist();
Expand Down Expand Up @@ -898,7 +895,7 @@ describe('TokenListController', () => {
});

it('should update token list after removing data with duplicate symbols', async () => {
nock(TOKEN_END_POINT_API)
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.mainnet)}`)
.reply(200, sampleWithDuplicateSymbols)
.persist();
Expand Down Expand Up @@ -941,7 +938,7 @@ describe('TokenListController', () => {
});

it('should update token list after removing data less than 3 occurrences', async () => {
nock(TOKEN_END_POINT_API)
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.mainnet)}`)
.reply(200, sampleWithLessThan3OccurencesResponse)
.persist();
Expand All @@ -965,7 +962,7 @@ describe('TokenListController', () => {
});

it('should update token list when the token property changes', async () => {
nock(TOKEN_END_POINT_API)
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.mainnet)}`)
.reply(200, sampleMainnetTokenList)
.persist();
Expand Down Expand Up @@ -993,7 +990,7 @@ describe('TokenListController', () => {
});

it('should update the cache when the timestamp expires', async () => {
nock(TOKEN_END_POINT_API)
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.mainnet)}`)
.reply(200, sampleMainnetTokenList)
.persist();
Expand Down Expand Up @@ -1023,7 +1020,7 @@ describe('TokenListController', () => {
});

it('should update token list when the chainId change', async () => {
nock(TOKEN_END_POINT_API)
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.mainnet)}`)
.reply(200, sampleMainnetTokenList)
.get(`/tokens/${convertHexToDecimal(ChainId.goerli)}`)
Expand Down Expand Up @@ -1120,7 +1117,7 @@ describe('TokenListController', () => {
});

it('should update preventPollingOnNetworkRestart and restart the polling on network restart', async () => {
nock(TOKEN_END_POINT_API)
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.mainnet)}`)
.reply(200, sampleMainnetTokenList)
.get(`/tokens/${convertHexToDecimal(ChainId.goerli)}`)
Expand Down Expand Up @@ -1197,22 +1194,30 @@ describe('TokenListController', () => {
});
});

describe('executePoll', () => {
describe.only('executePoll', () => {

Check failure on line 1197 in packages/assets-controllers/src/TokenListController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Unexpected focused test
it('should execute the poll', async () => {
const tokenServiceFetchTokenListSpy = sinon.spy(
tokenServiceFetchTokenList,
);

const spy = jest.spyOn(tokenService, 'fetchTokenList');
const controllerMessenger = getControllerMessenger();
controllerMessenger.registerActionHandler(
'NetworkController:getNetworkClientById',
jest.fn().mockReturnValue({
configuration: {
type: NetworkType.sepolia,
chainId: ChainId.sepolia,
},
}),
);
const messenger = getRestrictedMessenger(controllerMessenger);
const controller = new TokenListController({
chainId: ChainId.mainnet,
preventPollingOnNetworkRestart: false,
messenger,
state: existingState,
state: expiredCacheExistingState,
});
await controller.executePoll('sepolia');
expect(tokenServiceFetchTokenListSpy.calledWith('0x1')).toBe(true);
expect(spy.mock.calls[0]).toStrictEqual(
expect.arrayContaining([ChainId.sepolia]),
);
});
});
});
6 changes: 3 additions & 3 deletions packages/assets-controllers/src/TokenListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ export class TokenListController extends PollingController<
tokenList = { ...cachedTokens };
} else {
// Fetch fresh token list
const tokensFromAPI: TokenListToken[] = await safelyExecute(() =>
fetchTokenList(chainId, this.abortController.signal),
);
const tokensFromAPI: TokenListToken[] = await safelyExecute(() => {
return fetchTokenList(chainId, this.abortController.signal);
});

if (!tokensFromAPI) {
// Fallback to expired cached tokens
Expand Down
1 change: 1 addition & 0 deletions packages/assets-controllers/src/token-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export async function fetchTokenList(
abortSignal: AbortSignal,
{ timeout = defaultTimeout } = {},
): Promise<unknown> {
console.log('chainId:', chainId);
const tokenURL = getTokensURL(chainId);
const response = await queryApi(tokenURL, abortSignal, timeout);
if (response) {
Expand Down

0 comments on commit c9b92b0

Please sign in to comment.