UNPKG

orphic-cypress

Version:

Set of utilities and typescript transformers to cover storybook stories with cypress component tests

60 lines (59 loc) 2.24 kB
import type { ComponentStoryCy, ComponentStoryObjCy, StoryFileCy } from "./types"; /** * Object of function name keys to stubbed actions values. * Might be more likely that you'd access these stubs via `cy.get("@actions")` */ export type WrappedActions = { [fnName: string]: ReturnType<typeof cy.stub | typeof cy.spy>; }; /** * Object of either component obj of function * @private */ export type Stories = { [name: string]: (ComponentStoryCy<any> | ComponentStoryObjCy<any>) & { /** this seems to be the accurate storyName for the component */ storyName: string; }; }; /** * Get argTypes from both the default export and the individual story. * Useful for a per-component beforeEach or top-of-test declaration. * Note that you'll want to return undefined from `beforeEach` * * ```ts * describe("SomeComponent", () => { * beforeEach(() => { * stubStoryActions(SomeComponent, stories); * }); * * it("should render ok and call someAction on init", () => { * cy.mount(<SomeComponent {...this.actions} />); * cy.get("@actions").its("someAction").should("be.calledWith", ""); * }); * }); * ``` * * ```ts * it("should do something", () => { * // could just be `const actions = { someAction: cy.stub(), ... }` * const actions = stubStoryActions(SomeStory, stories); * cy.mount(<SomeStory {...actions} />); * cy.dataCy("something").click().then(() => { * expect(actions.someAction).to.have.callCount(1); * }); * // or without the promise * cy.dataCy("something").click(); * cy.get("@actions").its("someAction").should("have.callCount", 2); * }); * ``` * * Mostly an internal detail: precedence order, 3 through end will have essentially the same effect * 1) explicitly provided args/props for story which will become spies * 2) explicitly provided args at default export which will become spies * 3) local argTypes action definition * 4) global argTypes action definition * 5) local argTypes regex definition * 6) global argTypes regex definition */ export declare const stubStoryActions: <T extends StoryFileCy>(composedStory: ComponentStoryCy<any> | ComponentStoryObjCy<any>, stories: T, seed?: WrappedActions) => WrappedActions;