Enable cy.mockGraphqlOps to handle async methods
This commit is contained in:
parent
e4046de318
commit
66b9f8ac6a
|
|
@ -60,43 +60,48 @@ const mockGraphql = (options?: any) => {
|
||||||
// rebuilding what was in cypress-graphql-mock package here, because now we can just intercept the graphql call instead of messing with fetch
|
// rebuilding what was in cypress-graphql-mock package here, because now we can just intercept the graphql call instead of messing with fetch
|
||||||
cy.intercept('POST', '/api/graphql', (req) => {
|
cy.intercept('POST', '/api/graphql', (req) => {
|
||||||
const { operationName, query, variables } = req.body;
|
const { operationName, query, variables } = req.body;
|
||||||
const rootValue = getRootValue(currentOperations, operationName, variables);
|
return getRootValue(currentOperations, operationName, variables)
|
||||||
|
.then((rootValue) => {
|
||||||
|
if (!rootValue) {
|
||||||
|
return req;
|
||||||
|
}
|
||||||
|
|
||||||
if (!rootValue) {
|
if (
|
||||||
return req;
|
// Additional checks here because of transpilation.
|
||||||
}
|
// We will lose instanceof if we are not using specific babel plugin, or using pure TS to compile front-end
|
||||||
|
rootValue instanceof GraphQLError ||
|
||||||
|
rootValue.constructor === GraphQLError ||
|
||||||
|
rootValue.constructor.name === 'GraphQLError'
|
||||||
|
) {
|
||||||
|
return req.reply({
|
||||||
|
body: {
|
||||||
|
data: {},
|
||||||
|
errors: [rootValue],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
graphql({
|
||||||
// Additional checks here because of transpilation.
|
schema: schemaWithMocks,
|
||||||
// We will lose instanceof if we are not using specific babel plugin, or using pure TS to compile front-end
|
source: query,
|
||||||
rootValue instanceof GraphQLError ||
|
variableValues: variables,
|
||||||
rootValue.constructor === GraphQLError ||
|
operationName,
|
||||||
rootValue.constructor.name === 'GraphQLError'
|
rootValue,
|
||||||
) {
|
}).then(
|
||||||
return req.reply({
|
(result) => {
|
||||||
body: {
|
req.reply({
|
||||||
data: {},
|
...result,
|
||||||
errors: [rootValue],
|
});
|
||||||
},
|
},
|
||||||
});
|
(e) => {
|
||||||
}
|
console.error(e);
|
||||||
|
}
|
||||||
graphql({
|
);
|
||||||
schema: schemaWithMocks,
|
})
|
||||||
source: query,
|
.catch((e) => {
|
||||||
variableValues: variables,
|
|
||||||
operationName,
|
|
||||||
rootValue,
|
|
||||||
}).then(
|
|
||||||
(result) => {
|
|
||||||
req.reply({
|
|
||||||
...result,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
(e) => {
|
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
req.reply({ statusCode: 500, body: { errors: ['Internal server error'] } });
|
||||||
);
|
});
|
||||||
}).as('graphqlRequest');
|
}).as('graphqlRequest');
|
||||||
|
|
||||||
cy.wrap({
|
cy.wrap({
|
||||||
|
|
@ -235,10 +240,10 @@ Cypress.Commands.add('mockGraphql', mockGraphql);
|
||||||
|
|
||||||
Cypress.Commands.add('mockGraphqlOps', mockGraphqlOps);
|
Cypress.Commands.add('mockGraphqlOps', mockGraphqlOps);
|
||||||
|
|
||||||
const getRootValue = (allOperations: any, operationName: string, variables: any) => {
|
const getRootValue = async (allOperations: any, operationName: string, variables: any) => {
|
||||||
const operation = allOperations[operationName];
|
const operation = allOperations[operationName];
|
||||||
if (typeof operation === 'function') {
|
if (typeof operation === 'function') {
|
||||||
return operation(variables);
|
return Promise.resolve().then(() => operation(variables));
|
||||||
}
|
}
|
||||||
return operation;
|
return Promise.resolve(operation);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue