Skip to content

Commit

Permalink
test: align test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vvava committed Aug 8, 2024
1 parent d8c204f commit 6cf8260
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 187 deletions.
90 changes: 89 additions & 1 deletion src/background/providers/ChainAgnosticProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { ethErrors } from 'eth-rpc-errors';
import AutoPairingPostMessageConnection from '../utils/messaging/AutoPairingPostMessageConnection';
import { ChainAgnosticProvider } from './ChainAgnosticProvider';
import onDomReady from './utils/onDomReady';
import { DAppProviderRequest } from '../connections/dAppConnection/models';

jest.mock('../utils/messaging/AutoPairingPostMessageConnection', () => {
const mocks = {
connect: jest.fn().mockResolvedValue(undefined),
on: jest.fn(),
request: jest.fn().mockResolvedValue({}),
};
return jest.fn().mockReturnValue(mocks);
});

export const matchingPayload = (payload) =>
expect.objectContaining({
data: expect.objectContaining(payload),
});

jest.mock('./utils/onDomReady');
jest.mock('../utils/messaging/AutoPairingPostMessageConnection', () => {
Expand All @@ -21,10 +37,15 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
expect(channelMock.connect).toHaveBeenCalled();
expect(channelMock.request).not.toHaveBeenCalled();
});
it('waits for message channel to be connected', async () => {
it('should wait for message channel to be connected', async () => {
const mockedChannel = new AutoPairingPostMessageConnection(false);

const provider = new ChainAgnosticProvider(channelMock);

await new Promise(process.nextTick);

(onDomReady as jest.Mock).mock.calls[0][0]();

expect(mockedChannel.connect).toHaveBeenCalled();
expect(mockedChannel.request).not.toHaveBeenCalled();

Expand All @@ -35,13 +56,68 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
});
expect(mockedChannel.request).toHaveBeenCalled();
});
it('should call the `DOMAIN_METADATA_METHOD` adter domReady', async () => {
new ChainAgnosticProvider(channelMock);
await new Promise(process.nextTick);
expect(channelMock.request).toHaveBeenCalledTimes(0);
(onDomReady as jest.Mock).mock.calls[0][0]();
await new Promise(process.nextTick);

expect(channelMock.request).toHaveBeenCalledTimes(1);

expect(channelMock.request).toHaveBeenCalledWith(
// matchingPayload({
// method: DAppProviderRequest.INIT_DAPP_STATE,
// })
expect.objectContaining({
params: expect.objectContaining({
request: expect.objectContaining({
method: DAppProviderRequest.DOMAIN_METADATA_METHOD,
}),
}),
})
);
});
});

describe('request', () => {
it('should collect pending requests till the dom is ready', async () => {
const provider = new ChainAgnosticProvider(channelMock);
// wait for init to finish
await new Promise(process.nextTick);

expect(channelMock.request).toHaveBeenCalledTimes(0);

(channelMock.request as jest.Mock).mockResolvedValue('success');
const rpcResultCallback = jest.fn();
provider
.request({
data: {
method: 'some-method',
params: [{ param1: 1 }],
},
})
.then(rpcResultCallback);
await new Promise(process.nextTick);

expect(channelMock.request).toHaveBeenCalledTimes(0);

// domReady triggers sending pending requests as well
(onDomReady as jest.Mock).mock.calls[0][0]();
await new Promise(process.nextTick);

expect(channelMock.request).toHaveBeenCalledTimes(2);

expect(rpcResultCallback).toHaveBeenCalledWith('success');
});
it('should use the rate limits on `eth_requestAccounts` requests', async () => {
const provider = new ChainAgnosticProvider(channelMock);
(channelMock.request as jest.Mock).mockResolvedValue('success');

await new Promise(process.nextTick);

(onDomReady as jest.Mock).mock.calls[0][0]();

const firstCallCallback = jest.fn();
const secondCallCallback = jest.fn();
provider
Expand Down Expand Up @@ -69,6 +145,10 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
const provider = new ChainAgnosticProvider(channelMock);
(channelMock.request as jest.Mock).mockResolvedValue('success');

await new Promise(process.nextTick);

(onDomReady as jest.Mock).mock.calls[0][0]();

const firstCallCallback = jest.fn();
const secondCallCallback = jest.fn();
provider
Expand All @@ -93,6 +173,10 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
const provider = new ChainAgnosticProvider(channelMock);
(channelMock.request as jest.Mock).mockResolvedValueOnce('success');

await new Promise(process.nextTick);

(onDomReady as jest.Mock).mock.calls[0][0]();

await provider.request({
data: { method: 'some-method', params: [{ param1: 1 }] },
sessionId: '00000000-0000-0000-0000-000000000000',
Expand All @@ -106,6 +190,10 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
// response for the actual call
(channelMock.request as jest.Mock).mockResolvedValueOnce('success');

await new Promise(process.nextTick);

(onDomReady as jest.Mock).mock.calls[0][0]();

provider.request({
data: { method: 'some-method', params: [{ param1: 1 }] },
sessionId: '00000000-0000-0000-0000-000000000000',
Expand Down
Loading

0 comments on commit 6cf8260

Please sign in to comment.