All files / src/storybook story-code.cy.ts

100% Statements 54/54
100% Branches 2/2
100% Functions 20/20
100% Lines 53/53

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323                14x 14x 14x 14x               14x           14x 13x                             13x           14x 12x                     12x           14x 11x             11x           14x 10x                 10x           14x   9x                   9x               14x 14x 34x                               23x                                   14x 8x 8x         8x     14x 7x 7x             7x     14x 6x                                               6x                   6x     14x 5x                                                   5x                             5x     14x 4x 4x       4x           14x 14x                                         14x 5x                             14x 3x           3x     14x 2x          
import { dedent } from "ts-dedent";
 
import {
  removeSkips,
  transformSource,
  TransformSourceOptions,
} from "./story-code";
 
describe("story-code", () => {
  describe("removeSkips", () => {
    it("should remove lines skipped with @skip", () => {
      const lines = [
        "const notSkipped = 1;",
        "const skipped = 1; // story-code @skip",
        "const notSkipped2 = 2;",
        "// story-code @skip", // test blank line
        // test extra whitespace
        "const skipped2 = 2; //    story-code   @skip",
      ];
      expect(removeSkips(lines)).to.deep.equal([
        "const notSkipped = 1;",
        "const notSkipped2 = 2;",
      ]);
    });
 
    it("should remove next lines skipped with @skip-next", () => {
      const lines = [
        "const notSkipped = 1;",
        "// story-code @skip-next",
        "const skipped = 1;",
        "const notSkipped2 = 2;",
        "// story-code @skip-next",
        "// story-code @skip-next", // test repeated
        "const skipped2 = 2;",
        "// story-code @skip-next",
        // test interaction with skip
        "const alreadySkipped = 3 // story-code @skip",
        // same line still skips
        "// story-code @skip-next @skip",
        "const skipped3 = 3;",
      ];
      expect(removeSkips(lines)).to.deep.equal([
        "const notSkipped = 1;",
        "const notSkipped2 = 2;",
      ]);
    });
 
    it("should remove skip sections", () => {
      const lines = [
        "const notSkipped = 1;",
        "// story-code @skip-start",
        "const skipped = 1;",
        "// story-code @skip-end",
        "const notSkipped2 = 2;",
        "// story-code @skip-start",
        "const skipped2 = 2;",
        "const skipped3 = 3;",
        "// story-code @skip-end",
      ];
      expect(removeSkips(lines)).to.deep.equal([
        "const notSkipped = 1;",
        "const notSkipped2 = 2;",
      ]);
    });
 
    it("should remove skip sections inclusive of commented line", () => {
      const lines = [
        "const notSkipped = 1;",
        "const skipped = 1; // story-code @skip-start",
        "const skipped2 = 2;",
        "const skipped3 = 3; // story-code @skip-end",
        "const notSkipped2 = 2;",
      ];
      expect(removeSkips(lines)).to.deep.equal([
        "const notSkipped = 1;",
        "const notSkipped2 = 2;",
      ]);
    });
 
    it("should ignore dupe starts and skip lines with skip-end regardless", () => {
      const lines = [
        "const notSkipped = 1;",
        "const skipped = 1; // story-code @skip-start",
        "const skipped2 = 2; // story-code @skip-start",
        "const skipped3 = 3;",
        "const skipped4 = 4; // story-code @skip-end",
        "const skipped5 = 5; // story-code @skip-end",
        "const notSkipped2 = 2;",
      ];
      expect(removeSkips(lines)).to.deep.equal([
        "const notSkipped = 1;",
        "const notSkipped2 = 2;",
      ]);
    });
 
    it("should override @skip with regions, but only if in that order", () => {
      // so really just: 'some wonkiness ensues if you combine'
      const lines = [
        "const notSkipped = 1;",
        "const skipped = 1; // story-code @skip-start @skip",
        "const skipped3 = 3;",
        "const skipped4 = 4; // story-code @skip-end",
        "const notSkipped2 = 2;",
        "const skipped = 1; // story-code @skip @skip-start",
        "const skipped3 = 3;",
        "const skipped4 = 4; // story-code @skip-end",
      ];
      expect(removeSkips(lines)).to.deep.equal([
        "const notSkipped = 1;",
        "const notSkipped2 = 2;",
        "const skipped3 = 3;",
      ]);
    });
  });
 
  describe("transformSource", () => {
    const basicTest = (source: string, startLine: number, endLine: number) =>
      transformSource()("<SomeComponent prop={1} />", {
        id: "somedir-nested--some-story",
        originalStoryFn: { name: "SomeStory" },
        parameters: {
          storySource: {
            source,
            locationsMap: {
              "some-story": {
                startLoc: { line: startLine },
                endLoc: { line: endLine },
              },
            },
          },
        },
      } as any);
 
    const basicFile = (include?: "default" | "start") => dedent`
        import type { ComponentStory } from "@storybook/react";
        import { SomeComponent } from "./";
 
        export default { component: SomeComponent };
 
        export const SomeStory: ComponentStory<typeof SomeStory> = (args) => (
         <SomeComponent {...args} />
        );
        // ${include ? "story-code @include-" + include : "not-a-directive"}
        SomeStory.args = { prop: 1 };
 
        export const OtherStory: ComponentStory<typeof SomeStory> = (args) => (
         <SomeComponent {...args} prop={1} />
        );
        OtherStory.args = { prop2: 2 };
      `;
 
    it("should gather start and end locations as provided by storysource addon", () => {
      const result = basicTest(basicFile(), 6, 8);
      const expected = dedent`
        export const SomeStory: ComponentStory<typeof SomeStory> = (args) => (
         <SomeComponent {...args} />
        );
      `;
      expect(result).to.equal(expected);
    });
 
    it("should include default line as a single line", () => {
      const result = basicTest(basicFile("default"), 6, 8);
      const expected = dedent`
        export default { component: SomeComponent };
 
        export const SomeStory: ComponentStory<typeof SomeStory> = (args) => (
         <SomeComponent {...args} />
        );
      `;
      expect(result).to.equal(expected);
    });
 
    it("should gather and include default lines", () => {
      const result = basicTest(
        dedent`
          import type { ComponentStory } from "@storybook/react";
          import { SomeComponent } from "./";
 
          export default {
            component: SomeComponent,
            title: "SomeDir/Nested",
          };
 
          export const SomeStory: ComponentStory<typeof SomeStory> = (args) => (
           <SomeComponent {...args} />
          );
          // story-code @include-default
          SomeStory.args = { prop: 1 };
 
          export const OtherStory: ComponentStory<typeof SomeStory> = (args) => (
           <SomeComponent {...args} prop={1} />
          );
          OtherStory.args = { prop2: 2 };
        `,
        9,
        11
      );
      const expected = dedent`
        export default {
          component: SomeComponent,
          title: "SomeDir/Nested",
        };
 
        export const SomeStory: ComponentStory<typeof SomeStory> = (args) => (
         <SomeComponent {...args} />
        );
      `;
      expect(result).to.equal(expected);
    });
 
    it("should gather and include start lines", () => {
      const result = basicTest(
        dedent`
          import type { ComponentStory } from "@storybook/react";
          import { SomeComponent } from "./";
 
          const somethingElseImportant = 1;
 
          export default {
            component: SomeComponent,
            title: "SomeDir/Nested",
          };
 
          export const SomeStory: ComponentStory<typeof SomeStory> = (args) => (
           <SomeComponent {...args} />
          );
          // story-code @include-start
          SomeStory.args = { prop: 1 };
 
          export const OtherStory: ComponentStory<typeof SomeStory> = (args) => (
           <SomeComponent {...args} prop={1} />
          );
          OtherStory.args = { prop2: 2 };
        `,
        11,
        13
      );
      const expected = dedent`
        import type { ComponentStory } from "@storybook/react";
        import { SomeComponent } from "./";
 
        const somethingElseImportant = 1;
 
        export default {
          component: SomeComponent,
          title: "SomeDir/Nested",
        };
 
        export const SomeStory: ComponentStory<typeof SomeStory> = (args) => (
         <SomeComponent {...args} />
        );
      `;
      expect(result).to.equal(expected);
    });
 
    it("should return the snippet if anything goes wrong", () => {
      const expected = "<SomeComponent prop={1} />";
      expect(basicTest(basicFile(), 1, 1000)).to.equal(
        expected,
        "end line too high"
      );
      expect(basicTest(basicFile(), 1000, 1001)).to.equal(
        expected,
        "start line too high"
      );
    });
 
    describe("object file syntax", () => {
      const source = dedent`
        import type { ComponentStory, ComponentStoryObj } from "@storybook/react";
        import { SomeComponent } from "./";
 
        export default {
          component: SomeComponent,
          title: "SomeDir/Nested",
        };
 
        export const SomeStory: ComponentStory<typeof SomeStory> = (args) => (
         <SomeComponent {...args} />
        );
        // story-code @include-start
        SomeStory.args = { prop: 1 };
 
        export const OtherStoryObj: ComponentStoryObj<typeof SomeStory> = {
         <SomeComponent {...args} prop={1} />
        };
        OtherStoryObj.args = { prop2: 2 };
        // story-code @end
      `;
      const objTest = (opts: TransformSourceOptions) =>
        transformSource(opts)("<OtherStoryObj prop={1} />", {
          id: "somedir-nested--other-story-obj",
          originalStoryFn: { name: null },
          name: "Other Story Obj",
          parameters: {
            storySource: {
              source,
              locationsMap: {
                // note: missing other-story-obj
                "some-story": { startLoc: { line: 9 }, endLoc: { line: 15 } },
              },
            },
          },
        } as any);
 
      it("should get location information from objects despite storysource not generating it", () => {
        const expected = dedent`\n
          export const OtherStoryObj: ComponentStoryObj<typeof SomeStory> = {
           <SomeComponent {...args} prop={1} />
          };
          OtherStoryObj.args = { prop2: 2 };
        `;
        expect(objTest({ includeObjects: true })).to.equal(expected);
      });
 
      it("should get the snippet for objects if includeObjects is not true", () => {
        expect(objTest({})).to.equal("<OtherStoryObj prop={1} />");
      });
    });
  });
});