Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed PollingController to be a mixin so it can be used with both V1 and V2 controllers #1736

Merged
merged 9 commits into from
Sep 28, 2023
28 changes: 7 additions & 21 deletions packages/polling-controller/src/PollingController.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ControllerMessenger } from '@metamask/base-controller';

import type { PollingCompleteType } from './PollingController';
import PollingController from './PollingController';
import { PollingController } from './PollingController';

const TICK_TIME = 1000;

Expand All @@ -27,7 +26,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
jest.advanceTimersByTime(TICK_TIME);
Expand All @@ -48,7 +46,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
const pollingToken = controller.start('mainnet');
jest.advanceTimersByTime(TICK_TIME);
Expand All @@ -69,7 +66,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
const pollingToken1 = controller.start('mainnet');
controller.start('mainnet');
Expand All @@ -93,7 +89,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
expect(() => {
Expand All @@ -113,7 +108,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
expect(() => {
Expand All @@ -136,7 +130,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
jest.advanceTimersByTime(TICK_TIME);
Expand All @@ -158,7 +151,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
controller.start('mainnet');
Expand All @@ -177,25 +169,20 @@ describe('PollingController', () => {
}
const name = 'PollingController';

const mockMessenger = new ControllerMessenger<
any,
PollingCompleteType<typeof name>
>();

mockMessenger.subscribe(`${name}:pollingComplete`, pollingComplete);
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name,
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.onPollingComplete('mainnet', pollingComplete);
const pollingToken = controller.start('mainnet');
controller.stop(pollingToken);
expect(pollingComplete).toHaveBeenCalledTimes(1);
});
it('should poll at the interval length passed via the constructor', async () => {
it('should poll at the interval length when set via setIntervalLength', async () => {
jest.useFakeTimers();

class MyGasFeeController extends PollingController<any, any, any> {
Expand All @@ -208,8 +195,8 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME * 3,
});
controller.setIntervalLength(TICK_TIME * 3);
controller.start('mainnet');
jest.advanceTimersByTime(TICK_TIME);
await Promise.resolve();
Expand Down Expand Up @@ -238,7 +225,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
controller.start('rinkeby');
Expand All @@ -259,7 +245,7 @@ describe('PollingController', () => {
controller.stopAll();
});

it('should poll multiple networkClientIds at the interval length passed via the constructor', async () => {
it('should poll multiple networkClientIds when setting interval length', async () => {
jest.useFakeTimers();

class MyGasFeeController extends PollingController<any, any, any> {
Expand All @@ -272,8 +258,8 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME * 2,
});
controller.setIntervalLength(TICK_TIME * 2);
controller.start('mainnet');
jest.advanceTimersByTime(TICK_TIME);
await Promise.resolve();
Expand Down
Loading
Loading