Skip to content

Instantly share code, notes, and snippets.

@mesmacosta
Created October 19, 2019 15:48
Show Gist options
  • Select an option

  • Save mesmacosta/9f84958cb2a89b45e10132eb7ad8240d to your computer and use it in GitHub Desktop.

Select an option

Save mesmacosta/9f84958cb2a89b45e10132eb7ad8240d to your computer and use it in GitHub Desktop.
/**
* A Wrapper to wait for a given StubbedAPI to return using cypress wait method
*
* @see StubbedAPI
* @see https://on.cypress.io/wait
* @param api - StubbedAPI that will be used.
* @param message - Message that will be shown once api returns.
* @param assertionFunction - Function that will be called after api returns,
* it will expose the xhr object for assertion
* @param log - Enable/Disable logging for cypress runner
* @param timeout - Amount of time that will be waited for the api to return
* @example
```
commons.waitForStubbedApi({
api,
assertionFunction: (xhr: any) => {
expect(xhr.url).to.include('pizza.flavour=Pepperoni')
},
message: 'API call has data',
})
```
*/
export function waitForStubbedApi({
api,
message,
assertionFunction,
log = true,
timeout = 30000}:
{
api: StubbedAPI;
message?: string;
assertionFunction?: (xhr: any) => void;
log?: boolean;
timeout?: number;
}): void {
cy.wait(`@${api.name}`, { log, timeout }).then((xhr: any) => {
if (xhr) {
const requestBody = xhr.request ? xhr.request.body : null
const responseBody = xhr.response.body
const messageValue = message ? message : 'Api response should not be null'
assert.isNotNull(responseBody, messageValue)
}
if (assertionFunction) {
assertionFunction(xhr)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment