cypress graphql request example
Example 1: cypress graphql request example
describe('my page', () => {
beforeEach(function() {
cy.fixture('allCars').as('carsQuery')
})
context('mock & visit', () => {
beforeEach(function() {
cy.mockGraphQL([this.carsQuery])
cy.visit('http://localhost:8080')
})
it('my action', () => {})
})
})
Example 2: cypress graphql request example
Cypress.Commands.add('mockGraphQL', stubs => {
cy.on('window:before:load', win => {
cy.stub(win, 'fetch', (...args) => {
console.log('Handling fetch stub', args)
const [url, request] = args
const postBody = JSON.parse(request.body)
let promise
if (url.indexOf('api') !== -1) {
stubs.some(stub => {
if (postBody.operationName === stub.operation) {
console.log('STUBBING', stub.operation)
promise = Promise.resolve({
ok: true,
text() {
return Promise.resolve(JSON.stringify(stub.response))
}
})
return true
}
return false
})
}
if (promise) {
return promise
}
console.log('Could not find a stub for the operation.')
return false
})
})
})