All files / stories/StubActions WithStorySettings.stories.tsx

100% Statements 49/49
100% Branches 0/0
100% Functions 14/14
100% Lines 47/47

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139            58x 3x   58x       58x 3x 3x 3x 3x 3x 3x 3x 3x           58x 58x     58x 7x 7x     7x   7x   7x   7x   7x       7x 7x 7x 7x     7x         58x       6x         6x   6x 6x             58x   58x 2x     58x   2x 2x     2x 2x 2x               58x                 4x 4x                                   58x           5x 5x      
import React from "react";
import type { ComponentStoryCy, ComponentStoryObjCy } from "orphic-cypress";
import { ClickCount } from "stories";
 
export default { component: ClickCount };
 
export const StubActions: ComponentStoryCy<typeof ClickCount> = (args) => (
  <ClickCount {...args} />
);
StubActions.argTypes = {
  onClick: { action: "onClickStub" },
  onClick2: { action: "onClick2Stub" },
};
StubActions.cy = () => {
  cy.dataCy("count").should("contain", 0);
  cy.dataCy("button").click();
  cy.get("@onClickStub").should("be.calledOnceWith", 0);
  cy.dataCy("count").should("contain", 1);
  cy.dataCy("button").click();
  cy.get("@onClickStub").should("have.callCount", 2).and("be.calledWith", 1);
  cy.dataCy("button-2").click().click().click();
  cy.get("@onClick2Stub").should("have.callCount", 3);
};
// story-code @end
 
export const CheckOtherAliasesAndAccessOptions: ComponentStoryCy<
  typeof ClickCount
> = (args) => <ClickCount {...args} />;
CheckOtherAliasesAndAccessOptions.argTypes = {
  onClick: { action: "onClickStub" },
};
CheckOtherAliasesAndAccessOptions.cy = function () {
  cy.dataCy("count").should("contain", 0);
  cy.dataCy("button").click();
 
  // name on actions is `onClick`
  cy.get("@actions").its("onClick").should("be.calledOnceWith", 0);
  // but the action string also functions as an alias
  cy.get("@onClickStub").should("be.calledOnceWith", 0);
  // and it's also aliased as argTypesRegex.onClick since the argtypes would have applied
  cy.get("@argTypesRegex.onClick").should("be.calledWith", 0);
 
  cy.dataCy("count").should("contain", 1);
 
  cy.dataCy("button")
    .click()
    .then(() => {
      // just proving that `this` access works
      const self = this as any;
      expect(self.actions.onClick).to.have.callCount(2).and.be.calledWith(1);
      expect(self.onClickStub).to.have.callCount(2).and.be.calledWith(1);
      expect(self["argTypesRegex.onClick"])
        .to.have.callCount(2)
        .and.be.calledWith(1);
      cy.dataCy("count").should("contain", 2);
    });
};
// story-code @end
 
export const ImplicitArgtypeViaRegex: ComponentStoryObjCy<typeof ClickCount> = {
  cy: {
    "should stub via argtype regex when no other reasons for stubbing exist":
      function () {
        cy.dataCy("button")
          .click()
          .then(() => {
            // confirming that `this` access also works
            // @ts-ignore
            expect(this.actions.onClick).to.be.calledOnceWith(0);
          });
        cy.get("@actions").its("onClick").should("be.calledOnceWith", 0);
        cy.get("@argTypesRegex.onClick").should("be.calledOnceWith", 0);
      },
  },
};
 
export const WithExplicitStubAsPropToStory: ComponentStoryObjCy<
  typeof ClickCount
> = {
  cyTest: (Story) => {
    beforeEach(() => {
      cy.spy(() => 1).as("beforeEachSpy");
    });
 
    it("should call the spy and not the argTypesRegex stub", function () {
      // could be alias syntax, but showing variant
      cy.mount(<Story onClick={this.beforeEachSpy} />);
      cy.dataCy("button")
        .click()
        .then(() => {
          expect(this.actions.onClick).to.have.callCount(0);
          expect(this["argTypesRegex.onClick"]).to.have.callCount(0);
          expect(this.beforeEachSpy).to.be.calledOnceWith(0);
        });
    });
  },
};
 
export const MockIfProvidedViaArgsRegardlessOfDocgen: ComponentStoryObjCy<
  typeof ClickCount
> = {
  args: {
    // @ts-ignore
    onSomethingElse:
      /* istanbul ignore next */ // story-code @skip
      () => 1,
  },
  cy: () => {
    // these won't be called, but asserting not called proves they were stubbed and are stubs/spies
    cy.get("@actions").its("onSomethingElse").should("not.be.called");
    cy.get("@argTypesRegex.onSomethingElse").should("not.be.called");
  },
  // story-code @skip-start
  parameters: {
    docs: {
      description: {
        story: `
Proves that you can mock without presence of docgen plugin as long as it comes in as an arg.
Providing a mock not acceptable by typescript was easier than building a separate pipeline
without docgen`,
      },
    },
  },
  // story-code @skip-end
};
 
export const MockIfProvidedViaArgTypesRegardlessOfDocgen: ComponentStoryObjCy<
  typeof ClickCount
> = {
  argTypes: {
    // @ts-ignore
    onSomethingElse: { action: "onSomethingElseAlias" },
  },
  cy: () => {
    cy.get("@actions").its("onSomethingElse").should("not.be.called");
    cy.get("@onSomethingElseAlias").should("not.be.called");
  },
};