Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions packages/assets-controllers/src/NftDetectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type ApprovalActions = AddApprovalRequest;

const controllerName = 'NftController' as const;

const flushPromises = () => {
return new Promise(jest.requireActual('timers').setImmediate);
};

describe('NftDetectionController', () => {
let nftDetection: NftDetectionController;
let preferences: PreferencesController;
Expand Down Expand Up @@ -294,19 +298,26 @@ describe('NftDetectionController', () => {
testNftDetection.startPollingByNetworkClientId('mainnet', {
address: '0x1',
});
await Promise.all([jest.advanceTimersByTime(0), flushPromises]);
expect(spy.mock.calls).toHaveLength(1);
await Promise.all([
jest.advanceTimersByTime(DEFAULT_INTERVAL),
Promise.resolve(),
jest.advanceTimersByTime(DEFAULT_INTERVAL / 2),
flushPromises(),
]);
expect(spy.mock.calls).toHaveLength(1);
await Promise.all([
jest.advanceTimersByTime(DEFAULT_INTERVAL),
Promise.resolve(),
jest.advanceTimersByTime(DEFAULT_INTERVAL / 2),
flushPromises(),
]);
expect(spy.mock.calls).toHaveLength(2);
await Promise.all([
jest.advanceTimersByTime(DEFAULT_INTERVAL),
flushPromises(),
]);
expect(spy.mock.calls).toMatchObject([
['mainnet', '0x1'],
['mainnet', '0x1'],
['mainnet', '0x1'],
]);
nftDetection.stopAllPolling();
jest.runOnlyPendingTimers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ const setupTokenListController = (
return { tokenList, tokenListMessenger };
};

const flushPromises = () => {
return new Promise(jest.requireActual('timers').setImmediate);
};

describe('TokenDetectionController', () => {
let tokenDetection: TokenDetectionController;
let preferences: PreferencesController;
Expand Down Expand Up @@ -611,7 +615,7 @@ describe('TokenDetectionController', () => {
});
await Promise.all([
jest.advanceTimersByTime(DEFAULT_INTERVAL),
Promise.resolve(),
flushPromises(),
]);
expect(spy.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet', accountAddress: '0x1' }],
Expand Down
28 changes: 18 additions & 10 deletions packages/assets-controllers/src/TokenListController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1368,20 +1368,28 @@ describe('TokenListController', () => {
);

controller.startPollingByNetworkClientId('goerli');
jest.advanceTimersByTime(pollingIntervalTime / 2);
jest.advanceTimersByTime(0);
await flushPromises();
expect(fetchTokenListByChainIdSpy).toHaveBeenCalledTimes(0);
jest.advanceTimersByTime(pollingIntervalTime / 2);
await flushPromises();

expect(fetchTokenListByChainIdSpy).toHaveBeenCalledTimes(1);
await Promise.all([
jest.advanceTimersByTime(pollingIntervalTime / 2),
Comment thread
jiexi marked this conversation as resolved.
flushPromises(),
]);
expect(fetchTokenListByChainIdSpy).toHaveBeenCalledTimes(1);
await Promise.all([
jest.advanceTimersByTime(pollingIntervalTime / 2),
jest.runOnlyPendingTimers(),
flushPromises(),
]);

expect(fetchTokenListByChainIdSpy).toHaveBeenCalledTimes(2);
await Promise.all([
jest.advanceTimersByTime(pollingIntervalTime),
flushPromises(),
]);

await Promise.all([jest.runOnlyPendingTimers(), flushPromises()]);
expect(fetchTokenListByChainIdSpy).toHaveBeenCalledTimes(2);
expect(fetchTokenListByChainIdSpy).toHaveBeenCalledTimes(3);
});

it('should update tokenList state and tokensChainsCache', async () => {
Expand Down Expand Up @@ -1441,7 +1449,7 @@ describe('TokenListController', () => {
expect(controller.state).toStrictEqual(startingState);

// start polling for sepolia
await controller.startPollingByNetworkClientId('sepolia');
const pollingToken = controller.startPollingByNetworkClientId('sepolia');
// wait a polling interval
jest.advanceTimersByTime(pollingIntervalTime);
await flushPromises();
Expand All @@ -1457,10 +1465,10 @@ describe('TokenListController', () => {
data: sampleSepoliaTokensChainCache,
},
});
controller.stopPollingByPollingToken(pollingToken);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This test seems to do a lot. It's testing startPollingByNetworkClientId, but also stopPollingByPollingToken. I'm wondering if it's worth it to look at this test to see if it can be split up. I realize this is somewhat difficult, because in order to stop polling you have to start it somehow, so these methods seem linked. But I feel like this test would be hard to read for future maintainers. Nothing to do about this now, but something to think about as we increase how many controllers use this pattern.


// start polling for binance
await controller.startPollingByNetworkClientId(
'binance-network-client-id',
);
controller.startPollingByNetworkClientId('binance-network-client-id');
jest.advanceTimersByTime(pollingIntervalTime);
await flushPromises();

Expand Down
16 changes: 13 additions & 3 deletions packages/gas-fee-controller/src/GasFeeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ describe('GasFeeController', () => {
});

describe('polling (by networkClientId)', () => {
it('should call determineGasFeeCalculations (via _executePoll) with a URL that contains the chainId corresponding to the networkClientId after the interval passed via the constructor', async () => {
it('should call determineGasFeeCalculations (via _executePoll) with a URL that contains the chainId corresponding to the networkClientId immedaitely and after each interval passed via the constructor', async () => {
const pollingInterval = 10000;
await setupGasFeeController({
getIsEIP1559Compatible: jest.fn().mockResolvedValue(false),
Expand Down Expand Up @@ -1013,10 +1013,20 @@ describe('GasFeeController', () => {
});

gasFeeController.startPollingByNetworkClientId('goerli');
await clock.tickAsync(0);
expect(mockedDetermineGasFeeCalculations).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
fetchGasEstimatesUrl: `https://some-eip-1559-endpoint/${convertHexToDecimal(
ChainId.goerli,
)}`,
}),
);
await clock.tickAsync(pollingInterval / 2);
expect(mockedDetermineGasFeeCalculations).not.toHaveBeenCalled();
expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledTimes(1);
await clock.tickAsync(pollingInterval / 2);
expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect(mockedDetermineGasFeeCalculations).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
fetchGasEstimatesUrl: `https://some-eip-1559-endpoint/${convertHexToDecimal(
ChainId.goerli,
Expand Down
Loading