Skip to content

Commit

Permalink
Use a jest function for handling events from nock calls in GasFeeCont…
Browse files Browse the repository at this point in the history
…roller.test.ts
  • Loading branch information
danjm committed Sep 15, 2021
1 parent cb63b22 commit 4f8ae4b
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/gas/GasFeeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('GasFeeController', () => {
let getIsEIP1559Compatible: jest.Mock<Promise<boolean>>;
let getChainId: jest.Mock<`0x${string}` | `${number}` | number>;
let mockGasFeeRequest: any;
let nockCounter = 0;
const mockRequestHandler = jest.fn();

beforeAll(() => {
nock.disableNetConnect();
Expand Down Expand Up @@ -76,9 +76,7 @@ describe('GasFeeController', () => {
estimatedBaseFee: '28',
})
.persist();
mockGasFeeRequest.on('request', () => {
nockCounter += 1;
});
mockGasFeeRequest.on('request', mockRequestHandler);

nock(TEST_LEGACY_FEE_API.replace('<chain_id>', '0x1'))
.get(/.+/u)
Expand All @@ -103,8 +101,8 @@ describe('GasFeeController', () => {
});

afterEach(() => {
nockCounter = 0;
nock.cleanAll();
jest.clearAllMocks();
gasFeeController.destroy();
});

Expand Down Expand Up @@ -140,7 +138,7 @@ describe('GasFeeController', () => {
const result1 = await firstCallPromise;
const result2 = await secondCallPromise;

expect(nockCounter).toBe(1);
expect(mockRequestHandler).toHaveBeenCalledTimes(1);
expect(result1).toHaveLength(36);
expect(result2).toStrictEqual(pollToken);
});
Expand All @@ -158,21 +156,21 @@ describe('GasFeeController', () => {
await firstCallPromise;
await secondCallPromise;

expect(nockCounter).toBe(1);
expect(mockRequestHandler).toHaveBeenCalledTimes(1);

gasFeeController.stopPolling();

const result3 = await gasFeeController.getGasFeeEstimatesAndStartPolling(
undefined,
);
expect(result3).toHaveLength(36);
expect(nockCounter).toBe(2);
expect(mockRequestHandler).toHaveBeenCalledTimes(2);

const result4 = await gasFeeController.getGasFeeEstimatesAndStartPolling(
undefined,
);
expect(result4).toHaveLength(36);
expect(nockCounter).toBe(2);
expect(mockRequestHandler).toHaveBeenCalledTimes(2);
});
});

Expand Down

0 comments on commit 4f8ae4b

Please sign in to comment.