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, withdrawalsQuery,
protocolUpgradeProposalsQuery, protocolUpgradeProposalsQuery,
blockStatisticsQuery, blockStatisticsQuery,
networkParamQuery,
} from '@vegaprotocol/mock'; } from '@vegaprotocol/mock';
import type { PartialDeep } from 'type-fest'; import type { PartialDeep } from 'type-fest';
import type { MarketDataQuery, MarketsQuery } from '@vegaprotocol/markets'; import type { MarketDataQuery, MarketsQuery } from '@vegaprotocol/markets';
@ -160,6 +161,7 @@ const mockTradingPage = (
aliasGQLQuery(req, 'Candles', candlesQuery()); aliasGQLQuery(req, 'Candles', candlesQuery());
aliasGQLQuery(req, 'Withdrawals', withdrawalsQuery()); aliasGQLQuery(req, 'Withdrawals', withdrawalsQuery());
aliasGQLQuery(req, 'NetworkParams', networkParamsQuery()); aliasGQLQuery(req, 'NetworkParams', networkParamsQuery());
aliasGQLQuery(req, 'NetworkParam', networkParamQuery);
aliasGQLQuery(req, 'EstimateFees', estimateFeesQuery()); aliasGQLQuery(req, 'EstimateFees', estimateFeesQuery());
aliasGQLQuery(req, 'EstimatePosition', estimatePositionQuery()); aliasGQLQuery(req, 'EstimatePosition', estimatePositionQuery());
aliasGQLQuery(req, 'ProposalsList', proposalListQuery()); 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() { export function addMockGQLCommand() {
Cypress.Commands.add('mockGQL', (handler: RouteHandler) => { Cypress.Commands.add('mockGQL', (handler: RouteHandler) => {
cy.intercept('POST', Cypress.env('VEGA_URL'), handler).as('GQL'); cy.intercept('POST', Cypress.env('VEGA_URL'), handler).as('GQL');
@ -36,16 +51,25 @@ export const aliasGQLQuery = (
req: CyHttpMessages.IncomingHttpRequest, req: CyHttpMessages.IncomingHttpRequest,
operationName: string, operationName: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
data?: any, dataOrDataGetter?: any | ((variables: Record<string, any>) => any),
errors?: Partial<GraphQLError>[], errors?: Partial<GraphQLError>[],
headers?: Record<string, string> headers?: Record<string, string>
) => { ) => {
if (hasOperationName(req, operationName)) { if (hasOperationName(req, operationName)) {
req.alias = operationName; req.alias = operationName;
let data = dataOrDataGetter;
if (typeof dataOrDataGetter === 'function') {
const variables = extractVariables(req);
data = dataOrDataGetter(variables);
}
if (data !== undefined || errors !== undefined) { if (data !== undefined || errors !== undefined) {
req.reply({ req.reply({
statusCode: 200, statusCode: 200,
body: { ...(data && { data }), ...(errors && { errors }) }, body: {
...(data && { data }),
...(errors && { errors }),
},
headers: { headers: {
...req.headers, ...req.headers,
// basic default block height header response // basic default block height header response

View File

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