UNPKG

gatsby-cypress

Version:
49 lines (47 loc) 2.41 kB
"use strict"; var _apiHandler = _interopRequireWildcard(require("./api-handler")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* global Cypress cy */ // TODO(v6): Remove this command Cypress.Commands.add(`getTestElement`, (selector, options = {}) => cy.get(`[data-testid="${selector}"]`, options)); const TIMEOUT = 30000; Cypress.Commands.add(`waitForAPI`, { prevSubject: `optional` }, (subject, api, { timeout = TIMEOUT } = {}) => { Cypress.log({ name: `waitForAPI`, message: api }); cy.window({ log: false }).then({ timeout: timeout }, win => { if (!win.___apiHandler) { win.___apiHandler = _apiHandler.default.bind(win); } return _apiHandler.waitForAPI.call(win, api).then(() => subject); }); }); Cypress.Commands.add(`waitForAPIorTimeout`, { prevSubject: `optional` }, (subject, api, { timeout = TIMEOUT } = {}) => { cy.window().then({ timeout: timeout + 1000 }, win => { if (!win.___apiHandler) { win.___apiHandler = _apiHandler.default.bind(win); } return Promise.race([_apiHandler.waitForAPI.call(win, api).then(() => subject), new Promise(resolve => { setTimeout(resolve, timeout); })]); }); }); Cypress.Commands.add(`waitForRouteChange`, { prevSubject: `optional` }, subject => cy.waitForAPI(`onRouteUpdate`).then(() => subject));