test(trading): add network param mocks (#4001)

Co-authored-by: asiaznik <artur@vegaprotocol.io>
This commit is contained in:
daro-maj 2023-05-31 18:11:02 +02:00 committed by GitHub
parent 9f63fde123
commit 205f369cdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 6 deletions

View File

@ -30,6 +30,7 @@ import {
withdrawalsQuery,
protocolUpgradeProposalsQuery,
blockStatisticsQuery,
networkParamQuery,
} from '@vegaprotocol/mock';
import type { PartialDeep } from 'type-fest';
import type { MarketDataQuery, MarketsQuery } from '@vegaprotocol/markets';
@ -160,6 +161,7 @@ const mockTradingPage = (
aliasGQLQuery(req, 'Candles', candlesQuery());
aliasGQLQuery(req, 'Withdrawals', withdrawalsQuery());
aliasGQLQuery(req, 'NetworkParams', networkParamsQuery());
aliasGQLQuery(req, 'NetworkParam', networkParamQuery);
aliasGQLQuery(req, 'EstimateFees', estimateFeesQuery());
aliasGQLQuery(req, 'EstimatePosition', estimatePositionQuery());
aliasGQLQuery(req, 'ProposalsList', proposalListQuery());

View File

@ -25,6 +25,21 @@ const hasOperationName = (
);
};
/**
* Extracts variables from the intercepted GQL request.
* @returns Intercepted variables or empty object
*/
const extractVariables = (req: CyHttpMessages.IncomingHttpRequest): object => {
const { body } = req;
return (
(typeof body === 'object' &&
body !== null &&
'variables' in body &&
body.variables) ||
{}
);
};
export function addMockGQLCommand() {
Cypress.Commands.add('mockGQL', (handler: RouteHandler) => {
cy.intercept('POST', Cypress.env('VEGA_URL'), handler).as('GQL');
@ -36,16 +51,25 @@ export const aliasGQLQuery = (
req: CyHttpMessages.IncomingHttpRequest,
operationName: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data?: any,
dataOrDataGetter?: any | ((variables: Record<string, any>) => any),
errors?: Partial<GraphQLError>[],
headers?: Record<string, string>
) => {
if (hasOperationName(req, operationName)) {
req.alias = operationName;
let data = dataOrDataGetter;
if (typeof dataOrDataGetter === 'function') {
const variables = extractVariables(req);
data = dataOrDataGetter(variables);
}
if (data !== undefined || errors !== undefined) {
req.reply({
statusCode: 200,
body: { ...(data && { data }), ...(errors && { errors }) },
body: {
...(data && { data }),
...(errors && { errors }),
},
headers: {
...req.headers,
// basic default block height header response

View File

@ -1,6 +1,7 @@
import merge from 'lodash/merge';
import type {
NetworkParamQuery,
NetworkParamQueryVariables,
NetworkParamsQuery,
} from './__generated__/NetworkParams';
import type { PartialDeep } from 'type-fest';
@ -20,12 +21,12 @@ export const networkParamsQuery = (
};
export const networkParamQuery = (
override?: PartialDeep<NetworkParamQuery>
variables: NetworkParamQueryVariables
): NetworkParamQuery => {
const defaultResult: NetworkParamQuery = {
networkParameter: networkParams[0],
const param = networkParams.find((p) => p.key === variables.key);
return {
networkParameter: param || null,
};
return merge(defaultResult, override);
};
const networkParams = [
@ -34,6 +35,16 @@ const networkParams = [
key: 'governance.proposal.market.requiredMajority',
value: '0.66',
},
{
__typename: 'NetworkParameter' as const,
key: 'transfer.fee.factor',
value: '0.01',
},
{
__typename: 'NetworkParameter' as const,
key: 'spam.protection.minimumWithdrawalQuantumMultiple',
value: '10',
},
{
__typename: 'NetworkParameter' as const,
key: 'blockchains.ethereumConfig',