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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,58 @@ describe('QueuedRequestController', () => {
expect(controller.state).toStrictEqual({ queuedRequestCount: 0 });
});

it('updates queuedRequestCount when flushing requests for an origin', async () => {
const { messenger } = buildControllerMessenger();
const controller = new QueuedRequestController({
messenger: buildQueuedRequestControllerMessenger(messenger),
shouldRequestSwitchNetwork: () => false,
canRequestSwitchNetworkWithoutApproval: () => false,
clearPendingConfirmations: jest.fn(),
showApprovalRequest: jest.fn(),
});

const firstRequest = controller.enqueueRequest(
{ ...buildRequest(), origin: 'https://example.com' },
() => Promise.resolve(),
);
const secondRequest = controller.enqueueRequest(
{ ...buildRequest(), origin: 'https://example2.com' },
() => Promise.resolve(),
);
const thirdRequest = controller.enqueueRequest(
{ ...buildRequest(), origin: 'https://example2.com' },
() => Promise.resolve(),
);

expect(controller.state.queuedRequestCount).toBe(2);

// When the selected network changes for a domain, the queued requests for that domain/origin are flushed
messenger.publish(
'SelectedNetworkController:stateChange',
{ domains: {} },
[
{
op: 'replace',
path: ['domains', 'https://example2.com'],
},
],
);

expect(controller.state.queuedRequestCount).toBe(0);

await firstRequest;
await expect(secondRequest).rejects.toThrow(
new Error(
'The request has been rejected due to a change in selected network. Please verify the selected network and retry the request.',
),
);
await expect(thirdRequest).rejects.toThrow(
new Error(
'The request has been rejected due to a change in selected network. Please verify the selected network and retry the request.',
),
);
});

describe('enqueueRequest', () => {
it('throws an error if networkClientId is not provided', async () => {
const controller = buildQueuedRequestController();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export class QueuedRequestController extends BaseController<
this.#requestQueue = this.#requestQueue.filter(
({ request }) => request.origin !== flushOrigin,
);
this.#updateQueuedRequestCount();
}

/**
Expand Down
Loading