All files / src intercept.ts

100% Statements 3/3
100% Branches 4/4
100% Functions 2/2
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                  113x                           68x 68x          
import { Method } from "cypress/types/net-stubbing";
 
/**
 * Turn mockData intended for storybook plugin storybook-addon-mock
 * into cypress intercepts. Very likely to be used in a beforeEach.
 * Aliases as the url provided for each mock. Nothing crazy happening
 * here, you could just write `cy.intercept`s for non-storybook component
 * tests, or nbd to have ones that are redundant of mockData
 */
export const mockToCyIntercept = (
  mockData: Array<{
    /** url pattern to intercept */
    url: string;
    /** REST method */
    method: string;
    /** Expected status code */
    status: number;
    /** Response to send upon mocked request */
    response: unknown;
    /** Optionally alias intercept as a given name rather than full url */
    alias?: string;
  }>
) =>
  mockData.forEach(({ response, status, url, method, alias }) => {
    cy.intercept(method as Method, url, {
      body: response,
      statusCode: status,
    }).as(alias ?? url);
  });