mirror of
https://github.com/Retropex/mempool.git
synced 2025-05-13 02:30:41 +02:00
Add a new WS mocking utility to send fixtures or single messages
This commit is contained in:
parent
3c08b5c72b
commit
c43f436b04
@ -44,6 +44,7 @@
|
|||||||
|
|
||||||
import { PageIdleDetector } from './PageIdleDetector';
|
import { PageIdleDetector } from './PageIdleDetector';
|
||||||
import { mockWebSocket } from './websocket';
|
import { mockWebSocket } from './websocket';
|
||||||
|
import { mockWebSocketV2 } from './websocket';
|
||||||
|
|
||||||
/* global Cypress */
|
/* global Cypress */
|
||||||
const codes = {
|
const codes = {
|
||||||
@ -72,6 +73,10 @@ Cypress.Commands.add('mockMempoolSocket', () => {
|
|||||||
mockWebSocket();
|
mockWebSocket();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Cypress.Commands.add('mockMempoolSocketV2', () => {
|
||||||
|
mockWebSocketV2();
|
||||||
|
});
|
||||||
|
|
||||||
Cypress.Commands.add('changeNetwork', (network: "testnet" | "testnet4" | "signet" | "liquid" | "mainnet") => {
|
Cypress.Commands.add('changeNetwork', (network: "testnet" | "testnet4" | "signet" | "liquid" | "mainnet") => {
|
||||||
cy.get('.dropdown-toggle').click().then(() => {
|
cy.get('.dropdown-toggle').click().then(() => {
|
||||||
cy.get(`a.${network}`).click().then(() => {
|
cy.get(`a.${network}`).click().then(() => {
|
||||||
|
1
frontend/cypress/support/index.d.ts
vendored
1
frontend/cypress/support/index.d.ts
vendored
@ -5,6 +5,7 @@ declare namespace Cypress {
|
|||||||
waitForSkeletonGone(): Chainable<any>
|
waitForSkeletonGone(): Chainable<any>
|
||||||
waitForPageIdle(): Chainable<any>
|
waitForPageIdle(): Chainable<any>
|
||||||
mockMempoolSocket(): Chainable<any>
|
mockMempoolSocket(): Chainable<any>
|
||||||
|
mockMempoolSocketV2(): Chainable<any>
|
||||||
changeNetwork(network: "testnet"|"testnet4"|"signet"|"liquid"|"mainnet"): Chainable<any>
|
changeNetwork(network: "testnet"|"testnet4"|"signet"|"liquid"|"mainnet"): Chainable<any>
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -27,6 +27,37 @@ const createMock = (url: string) => {
|
|||||||
return mocks[url];
|
return mocks[url];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const mockWebSocketV2 = () => {
|
||||||
|
cy.on('window:before:load', (win) => {
|
||||||
|
const winWebSocket = win.WebSocket;
|
||||||
|
cy.stub(win, 'WebSocket').callsFake((url) => {
|
||||||
|
console.log(url);
|
||||||
|
if ((new URL(url).pathname.indexOf('/sockjs-node/') !== 0)) {
|
||||||
|
const { server, websocket } = createMock(url);
|
||||||
|
|
||||||
|
win.mockServer = server;
|
||||||
|
win.mockServer.on('connection', (socket) => {
|
||||||
|
win.mockSocket = socket;
|
||||||
|
});
|
||||||
|
|
||||||
|
win.mockServer.on('message', (message) => {
|
||||||
|
console.log(message);
|
||||||
|
});
|
||||||
|
|
||||||
|
return websocket;
|
||||||
|
} else {
|
||||||
|
return new winWebSocket(url);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
cy.on('window:before:unload', () => {
|
||||||
|
for (const url in mocks) {
|
||||||
|
cleanupMock(url);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export const mockWebSocket = () => {
|
export const mockWebSocket = () => {
|
||||||
cy.on('window:before:load', (win) => {
|
cy.on('window:before:load', (win) => {
|
||||||
const winWebSocket = win.WebSocket;
|
const winWebSocket = win.WebSocket;
|
||||||
@ -65,6 +96,27 @@ export const mockWebSocket = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const receiveWebSocketMessageFromServer = ({
|
||||||
|
params
|
||||||
|
}: { params?: any } = {}) => {
|
||||||
|
cy.window().then((win) => {
|
||||||
|
if (params.message) {
|
||||||
|
console.log('sending message');
|
||||||
|
win.mockSocket.send(params.message.contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params.file) {
|
||||||
|
cy.readFile(`cypress/fixtures/${params.file.path}`, 'utf-8').then((fixture) => {
|
||||||
|
console.log('sending payload');
|
||||||
|
win.mockSocket.send(JSON.stringify(fixture));
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
export const emitMempoolInfo = ({
|
export const emitMempoolInfo = ({
|
||||||
params
|
params
|
||||||
}: { params?: any } = {}) => {
|
}: { params?: any } = {}) => {
|
||||||
@ -82,16 +134,22 @@ export const emitMempoolInfo = ({
|
|||||||
switch (params.command) {
|
switch (params.command) {
|
||||||
case "init": {
|
case "init": {
|
||||||
win.mockSocket.send('{"conversions":{"USD":32365.338815782445}}');
|
win.mockSocket.send('{"conversions":{"USD":32365.338815782445}}');
|
||||||
cy.readFile('cypress/fixtures/mainnet_live2hchart.json', 'ascii').then((fixture) => {
|
cy.readFile('cypress/fixtures/mainnet_live2hchart.json', 'utf-8').then((fixture) => {
|
||||||
win.mockSocket.send(JSON.stringify(fixture));
|
win.mockSocket.send(JSON.stringify(fixture));
|
||||||
});
|
});
|
||||||
cy.readFile('cypress/fixtures/mainnet_mempoolInfo.json', 'ascii').then((fixture) => {
|
cy.readFile('cypress/fixtures/mainnet_mempoolInfo.json', 'utf-8').then((fixture) => {
|
||||||
win.mockSocket.send(JSON.stringify(fixture));
|
win.mockSocket.send(JSON.stringify(fixture));
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "rbfTransaction": {
|
case "rbfTransaction": {
|
||||||
cy.readFile('cypress/fixtures/mainnet_rbf.json', 'ascii').then((fixture) => {
|
cy.readFile('cypress/fixtures/mainnet_rbf.json', 'utf-8').then((fixture) => {
|
||||||
|
win.mockSocket.send(JSON.stringify(fixture));
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'trackTx': {
|
||||||
|
cy.readFile('cypress/fixtures/track_tx.json', 'utf-8').then((fixture) => {
|
||||||
win.mockSocket.send(JSON.stringify(fixture));
|
win.mockSocket.send(JSON.stringify(fixture));
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user