Enable cy.mockGraphqlOps to handle async methods

This commit is contained in:
Ramon Wenger 2023-12-13 18:02:49 +01:00
parent e4046de318
commit 66b9f8ac6a
1 changed files with 42 additions and 37 deletions

View File

@ -60,8 +60,8 @@ 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
cy.intercept('POST', '/api/graphql', (req) => {
const { operationName, query, variables } = req.body;
const rootValue = getRootValue(currentOperations, operationName, variables);
return getRootValue(currentOperations, operationName, variables)
.then((rootValue) => {
if (!rootValue) {
return req;
}
@ -97,6 +97,11 @@ const mockGraphql = (options?: any) => {
console.error(e);
}
);
})
.catch((e) => {
console.error(e);
req.reply({ statusCode: 500, body: { errors: ['Internal server error'] } });
});
}).as('graphqlRequest');
cy.wrap({
@ -235,10 +240,10 @@ Cypress.Commands.add('mockGraphql', mockGraphql);
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];
if (typeof operation === 'function') {
return operation(variables);
return Promise.resolve().then(() => operation(variables));
}
return operation;
return Promise.resolve(operation);
};