vega-frontend-monorepo/libs/cypress/src/lib/commands/mock-gql.ts
Joe Tsang 777d330f8e
chore(test fixes): use api token in test (#2316)
* chore: clean jsonrpc wallet api methods - remove permission methods

* chore: clean jsonrpc wallet api methods - remove permission methods

* chore: clean jsonrpc wallet api methods - remove permission methods

* chore: clean jsonrpc wallet api methods - fix failing unit test

* chore: update vega_version

* chore: update vega to v0.63.0 in workflows

* chore: mock api-token in connect wallet response

* chore: fix token env in workflow

* chore: remove dummy wallet leftovers

* chore: fix asset tests

* chore: fix remaining explorer tests

* chore: increase tx timeout

* chore: remove smoke test temporarily

* chore: remove other governance smoke test

* chore: remove smoke staking test

* chore: move validator tests to regression

* chore: revert regression change because they are also running

* chore: move vega wallet tests to slow

* chore: skip tests instead

Co-authored-by: maciek <maciek@vegaprotocol.io>
Co-authored-by: Rado <szpiechrados@gmail.com>
2022-12-05 16:51:53 +00:00

75 lines
2.3 KiB
TypeScript

import type { RouteHandler } from 'cypress/types/net-stubbing';
import { Server, WebSocket } from 'mock-socket';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface onMessage<T = any, V = any> {
(send: (data: T) => void, variables: V): void;
}
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Chainable<Subject> {
mockGQL(handler: RouteHandler): void;
mockGQLSubscription(mocks?: Record<string, onMessage>): void;
mockWalletGQL(handler: RouteHandler): void;
}
}
}
export function addMockWalletGQLCommand() {
Cypress.Commands.add('mockWalletGQL', (handler: RouteHandler): void => {
cy.intercept('POST', '**/api/v2/requests', handler).as('walletGQL');
});
}
export function addMockGQLCommand() {
Cypress.Commands.add('mockGQL', (handler: RouteHandler) => {
cy.intercept('POST', '**/graphql', handler).as('GQL');
});
}
const mockSocketServer = Cypress.env('VEGA_URL')
? new Server(Cypress.env('VEGA_URL').replace('http', 'ws'))
: null;
export function addMockGQLSubscriptionCommand() {
Cypress.Commands.add(
'mockGQLSubscription',
(mocks?: Record<string, onMessage>) => {
cy.on('window:before:load', (win) => {
if (!mockSocketServer) {
return;
}
win.WebSocket = WebSocket;
mockSocketServer.on('connection', (socket) => {
socket.on('message', (rawMessage) => {
if (typeof rawMessage !== 'string') {
return;
}
const message = JSON.parse(rawMessage);
const { id, payload, type } = message;
if (type === 'connection_init') {
socket.send(JSON.stringify({ type: 'connection_ack' }));
return;
}
if (payload && mocks && mocks[payload.operationName]) {
mocks[payload.operationName](
(data) =>
socket.send(
JSON.stringify({
type: 'next',
id,
payload: { errors: [], data },
})
),
payload.variables
);
}
});
});
});
}
);
}