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
1 change: 0 additions & 1 deletion packages/polling-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"@types/jest": "^29.5.14",
"deepmerge": "^4.2.2",
"jest": "^29.7.0",
"sinon": "^9.2.4",
"ts-jest": "^29.2.5",
"typedoc": "^0.25.13",
"typedoc-plugin-missing-exports": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { MOCK_ANY_NAMESPACE, Messenger } from '@metamask/messenger';
import type { MockAnyNamespace } from '@metamask/messenger';
import type { NetworkClient } from '@metamask/network-controller';
import EventEmitter from 'events';
import { useFakeTimers } from 'sinon';

import type { BlockTrackerPollingInput } from './BlockTrackerPollingController';
import { BlockTrackerPollingController } from './BlockTrackerPollingController';
Expand Down Expand Up @@ -43,13 +42,13 @@ class TestBlockTracker extends EventEmitter {
}

describe('BlockTrackerPollingController', () => {
let clock: sinon.SinonFakeTimers;
let mockMessenger: Messenger<MockAnyNamespace, never, never>;
let controller: ChildBlockTrackerPollingController;
let mainnetBlockTracker: TestBlockTracker;
let goerliBlockTracker: TestBlockTracker;
let sepoliaBlockTracker: TestBlockTracker;
beforeEach(() => {
jest.useFakeTimers({ doNotFake: ['nextTick', 'queueMicrotask'] });
mockMessenger = new Messenger({ namespace: MOCK_ANY_NAMESPACE });
controller = new ChildBlockTrackerPollingController({
messenger: mockMessenger,
Expand Down Expand Up @@ -82,10 +81,9 @@ describe('BlockTrackerPollingController', () => {
throw new Error(`Unknown networkClientId: ${networkClientId}`);
}
});
clock = useFakeTimers();
});
afterEach(() => {
clock.restore();
jest.useRealTimers();
});

describe('startPolling', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { MOCK_ANY_NAMESPACE, Messenger } from '@metamask/messenger';
import type { MockAnyNamespace } from '@metamask/messenger';
import { createDeferredPromise } from '@metamask/utils';
import { useFakeTimers } from 'sinon';

import { StaticIntervalPollingController } from './StaticIntervalPollingController';
import { advanceTime } from '../../../tests/helpers';
import { jestAdvanceTime } from '../../../tests/helpers';

const TICK_TIME = 5;

Expand Down Expand Up @@ -39,10 +38,10 @@ class ChildBlockTrackerPollingController extends StaticIntervalPollingController
}

describe('StaticIntervalPollingController', () => {
let clock: sinon.SinonFakeTimers;
let mockMessenger: Messenger<MockAnyNamespace, never, never>;
let controller: ChildBlockTrackerPollingController;
beforeEach(() => {
jest.useFakeTimers({ doNotFake: ['nextTick', 'queueMicrotask'] });
mockMessenger = new Messenger({ namespace: MOCK_ANY_NAMESPACE });
controller = new ChildBlockTrackerPollingController({
messenger: mockMessenger,
Expand All @@ -51,36 +50,35 @@ describe('StaticIntervalPollingController', () => {
state: { foo: 'bar' },
});
controller.setIntervalLength(TICK_TIME);
clock = useFakeTimers();
});

afterEach(() => {
clock.restore();
jest.useRealTimers();
});

describe('startPolling', () => {
it('should start polling if not already polling', async () => {
controller.startPolling({ networkClientId: 'mainnet' });
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });
expect(controller._executePoll).toHaveBeenCalledTimes(1);
controller.executePollPromises[0].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });
expect(controller._executePoll).toHaveBeenCalledTimes(2);
controller.stopAllPolling();
});

it('should call _executePoll immediately once and continue calling _executePoll on interval when called again with the same networkClientId', async () => {
controller.startPolling({ networkClientId: 'mainnet' });
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });

controller.startPolling({ networkClientId: 'mainnet' });
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });

expect(controller._executePoll).toHaveBeenCalledTimes(1);
controller.executePollPromises[0].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });
controller.executePollPromises[1].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });
controller.executePollPromises[2].resolve();

expect(controller._executePoll).toHaveBeenCalledTimes(3);
Expand All @@ -92,12 +90,12 @@ describe('StaticIntervalPollingController', () => {
controller.startPolling({
networkClientId: 'mainnet',
});
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });

controller.startPolling({
networkClientId: 'rinkeby',
});
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet' }],
Expand All @@ -106,7 +104,7 @@ describe('StaticIntervalPollingController', () => {

controller.executePollPromises[0].resolve();
controller.executePollPromises[1].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet' }],
Expand All @@ -117,7 +115,7 @@ describe('StaticIntervalPollingController', () => {

controller.executePollPromises[2].resolve();
controller.executePollPromises[3].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet' }],
Expand All @@ -135,26 +133,26 @@ describe('StaticIntervalPollingController', () => {
controller.startPolling({
networkClientId: 'mainnet',
});
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet' }],
]);
controller.executePollPromises[0].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });

controller.startPolling({
networkClientId: 'sepolia',
});
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet' }],
[{ networkClientId: 'sepolia' }],
]);

controller.executePollPromises[1].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet' }],
Expand All @@ -163,7 +161,7 @@ describe('StaticIntervalPollingController', () => {
]);

controller.executePollPromises[2].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet' }],
Expand All @@ -173,7 +171,7 @@ describe('StaticIntervalPollingController', () => {
]);

controller.executePollPromises[3].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet' }],
Expand All @@ -184,7 +182,7 @@ describe('StaticIntervalPollingController', () => {
]);

controller.executePollPromises[4].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet' }],
Expand All @@ -203,12 +201,12 @@ describe('StaticIntervalPollingController', () => {
const pollingToken = controller.startPolling({
networkClientId: 'mainnet',
});
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });
expect(controller._executePoll).toHaveBeenCalledTimes(1);
controller.executePollPromises[0].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });
controller.stopPollingByPollingToken(pollingToken);
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });
expect(controller._executePoll).toHaveBeenCalledTimes(2);
controller.stopAllPolling();
});
Expand All @@ -217,15 +215,15 @@ describe('StaticIntervalPollingController', () => {
const pollingToken1 = controller.startPolling({
networkClientId: 'mainnet',
});
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });

controller.startPolling({ networkClientId: 'mainnet' });
expect(controller._executePoll).toHaveBeenCalledTimes(1);
controller.executePollPromises[0].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });
controller.stopPollingByPollingToken(pollingToken1);
controller.executePollPromises[1].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });
expect(controller._executePoll).toHaveBeenCalledTimes(3);
controller.stopAllPolling();
});
Expand All @@ -243,18 +241,18 @@ describe('StaticIntervalPollingController', () => {
networkClientId: 'mainnet',
address: '0x1',
});
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });
controller.startPolling({
networkClientId: 'mainnet',
address: '0x2',
});
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });

controller.startPolling({
networkClientId: 'sepolia',
address: '0x2',
});
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet', address: '0x1' }],
Expand All @@ -265,7 +263,7 @@ describe('StaticIntervalPollingController', () => {
controller.executePollPromises[0].resolve();
controller.executePollPromises[1].resolve();
controller.executePollPromises[2].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet', address: '0x1' }],
Expand All @@ -279,7 +277,7 @@ describe('StaticIntervalPollingController', () => {
controller.executePollPromises[3].resolve();
controller.executePollPromises[4].resolve();
controller.executePollPromises[5].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });

expect(controller._executePoll.mock.calls).toMatchObject([
[{ networkClientId: 'mainnet', address: '0x1' }],
Expand All @@ -297,13 +295,13 @@ describe('StaticIntervalPollingController', () => {
const pollingToken = controller.startPolling({
networkClientId: 'mainnet',
});
await advanceTime({ clock, duration: 0 });
await jestAdvanceTime({ duration: 0 });
expect(controller._executePoll).toHaveBeenCalledTimes(1);
controller.stopPollingByPollingToken(pollingToken);
controller.executePollPromises[0].resolve();
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });
expect(controller._executePoll).toHaveBeenCalledTimes(1);
await advanceTime({ clock, duration: TICK_TIME });
await jestAdvanceTime({ duration: TICK_TIME });
expect(controller._executePoll).toHaveBeenCalledTimes(1);
controller.stopAllPolling();
});
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4408,7 +4408,6 @@ __metadata:
deepmerge: "npm:^4.2.2"
fast-json-stable-stringify: "npm:^2.1.0"
jest: "npm:^29.7.0"
sinon: "npm:^9.2.4"
ts-jest: "npm:^29.2.5"
typedoc: "npm:^0.25.13"
typedoc-plugin-missing-exports: "npm:^2.0.0"
Expand Down