@cappa/plugin-storybook 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,524 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
- import { cappaPluginStorybook, type StorybookStory } from "./plugin";
3
-
4
- // Mock the logger
5
- vi.mock("@cappa/logger", () => ({
6
- getLogger: () => ({
7
- debug: vi.fn(),
8
- start: vi.fn(),
9
- warn: vi.fn(),
10
- error: vi.fn(),
11
- }),
12
- }));
13
-
14
- vi.mock("./util", () => ({
15
- buildFilename: vi.fn(() => "button--primary.png"),
16
- freezeUI: vi.fn(async () => {}),
17
- waitForVisualIdle: vi.fn(async () => {}),
18
- }));
19
-
20
- vi.mock("./storybook-url", () => ({
21
- buildStorybookIframeUrl: vi.fn(
22
- () => "http://localhost:6006/iframe.html?id=story",
23
- ),
24
- }));
25
-
26
- // Mock fetch
27
- global.fetch = vi.fn();
28
-
29
- describe("cappaPluginStorybook - Glob Pattern Matching", () => {
30
- const mockStories: StorybookStory[] = [
31
- {
32
- id: "button--primary",
33
- name: "Primary",
34
- title: "Button",
35
- kind: "Button",
36
- story: "Primary",
37
- type: "story",
38
- },
39
- {
40
- id: "button--secondary",
41
- name: "Secondary",
42
- title: "Button",
43
- kind: "Button",
44
- story: "Secondary",
45
- type: "story",
46
- },
47
- {
48
- id: "input--default",
49
- name: "Default",
50
- title: "Input",
51
- kind: "Input",
52
- story: "Default",
53
- type: "story",
54
- },
55
- {
56
- id: "input--with-label",
57
- name: "With Label",
58
- title: "Input",
59
- kind: "Input",
60
- story: "With Label",
61
- type: "story",
62
- },
63
- {
64
- id: "card--basic",
65
- name: "Basic",
66
- title: "Card",
67
- kind: "Card",
68
- story: "Basic",
69
- type: "story",
70
- },
71
- {
72
- id: "card--advanced",
73
- name: "Advanced",
74
- title: "Card",
75
- kind: "Card",
76
- story: "Advanced",
77
- type: "story",
78
- },
79
- ];
80
-
81
- const mockStorybookResponse = {
82
- entries: mockStories.reduce(
83
- (acc, story) => {
84
- acc[story.id] = story;
85
- return acc;
86
- },
87
- {} as Record<string, StorybookStory>,
88
- ),
89
- };
90
-
91
- beforeEach(() => {
92
- vi.clearAllMocks();
93
- (global.fetch as any).mockReset();
94
- (global.fetch as any).mockResolvedValue({
95
- ok: true,
96
- json: () => Promise.resolve(mockStorybookResponse),
97
- });
98
- });
99
-
100
- describe("includeStories glob patterns", () => {
101
- it("should include stories matching exact story id", async () => {
102
- const plugin = cappaPluginStorybook({
103
- storybookUrl: "http://localhost:6006",
104
- includeStories: ["button--primary"],
105
- });
106
-
107
- const result = await plugin.discover();
108
- expect(result).toHaveLength(1);
109
- expect(result[0].data.story.id).toBe("button--primary");
110
- });
111
-
112
- it("should include stories matching wildcard patterns", async () => {
113
- const plugin = cappaPluginStorybook({
114
- storybookUrl: "http://localhost:6006",
115
- includeStories: ["button--*"],
116
- });
117
-
118
- const result = await plugin.discover();
119
- expect(result).toHaveLength(2);
120
- expect(result.map((r) => r.data.story.id)).toEqual([
121
- "button--primary",
122
- "button--secondary",
123
- ]);
124
- });
125
-
126
- it("should include stories matching title patterns", async () => {
127
- const plugin = cappaPluginStorybook({
128
- storybookUrl: "http://localhost:6006",
129
- includeStories: ["Button"],
130
- });
131
-
132
- const result = await plugin.discover();
133
- expect(result).toHaveLength(2);
134
- expect(result.map((r) => r.data.story.id)).toEqual([
135
- "button--primary",
136
- "button--secondary",
137
- ]);
138
- });
139
-
140
- it("should include stories matching name patterns", async () => {
141
- const plugin = cappaPluginStorybook({
142
- storybookUrl: "http://localhost:6006",
143
- includeStories: ["Primary"],
144
- });
145
-
146
- const result = await plugin.discover();
147
- expect(result).toHaveLength(1);
148
- expect(result[0].data.story.id).toBe("button--primary");
149
- });
150
-
151
- it("should include stories matching full path patterns", async () => {
152
- const plugin = cappaPluginStorybook({
153
- storybookUrl: "http://localhost:6006",
154
- includeStories: ["Button/*"],
155
- });
156
-
157
- const result = await plugin.discover();
158
- expect(result).toHaveLength(2);
159
- expect(result.map((r) => r.data.story.id)).toEqual([
160
- "button--primary",
161
- "button--secondary",
162
- ]);
163
- });
164
-
165
- it("should include stories matching multiple patterns", async () => {
166
- const plugin = cappaPluginStorybook({
167
- storybookUrl: "http://localhost:6006",
168
- includeStories: ["button--*", "input--*"],
169
- });
170
-
171
- const result = await plugin.discover();
172
- expect(result).toHaveLength(4);
173
- expect(result.map((r) => r.data.story.id)).toEqual([
174
- "button--primary",
175
- "button--secondary",
176
- "input--default",
177
- "input--with-label",
178
- ]);
179
- });
180
-
181
- it("should include stories matching complex glob patterns", async () => {
182
- const plugin = cappaPluginStorybook({
183
- storybookUrl: "http://localhost:6006",
184
- includeStories: ["*--primary", "*--default"],
185
- });
186
-
187
- const result = await plugin.discover();
188
- expect(result).toHaveLength(2);
189
- expect(result.map((r) => r.data.story.id)).toEqual([
190
- "button--primary",
191
- "input--default",
192
- ]);
193
- });
194
- });
195
-
196
- describe("excludeStories glob patterns", () => {
197
- it("should exclude stories matching exact story id", async () => {
198
- const plugin = cappaPluginStorybook({
199
- storybookUrl: "http://localhost:6006",
200
- excludeStories: ["button--primary"],
201
- });
202
-
203
- const result = await plugin.discover();
204
- expect(result).toHaveLength(5);
205
- expect(result.map((r) => r.data.story.id)).not.toContain(
206
- "button--primary",
207
- );
208
- });
209
-
210
- it("should exclude stories matching wildcard patterns", async () => {
211
- const plugin = cappaPluginStorybook({
212
- storybookUrl: "http://localhost:6006",
213
- excludeStories: ["button--*"],
214
- });
215
-
216
- const result = await plugin.discover();
217
- expect(result).toHaveLength(4);
218
- expect(result.map((r) => r.data.story.id)).not.toContain(
219
- "button--primary",
220
- );
221
- expect(result.map((r) => r.data.story.id)).not.toContain(
222
- "button--secondary",
223
- );
224
- });
225
-
226
- it("should exclude stories matching title patterns", async () => {
227
- const plugin = cappaPluginStorybook({
228
- storybookUrl: "http://localhost:6006",
229
- excludeStories: ["Button"],
230
- });
231
-
232
- const result = await plugin.discover();
233
- expect(result).toHaveLength(4);
234
- expect(result.map((r) => r.data.story.id)).not.toContain(
235
- "button--primary",
236
- );
237
- expect(result.map((r) => r.data.story.id)).not.toContain(
238
- "button--secondary",
239
- );
240
- });
241
-
242
- it("should exclude stories matching name patterns", async () => {
243
- const plugin = cappaPluginStorybook({
244
- storybookUrl: "http://localhost:6006",
245
- excludeStories: ["Primary"],
246
- });
247
-
248
- const result = await plugin.discover();
249
- expect(result).toHaveLength(5);
250
- expect(result.map((r) => r.data.story.id)).not.toContain(
251
- "button--primary",
252
- );
253
- });
254
-
255
- it("should exclude stories matching full path patterns", async () => {
256
- const plugin = cappaPluginStorybook({
257
- storybookUrl: "http://localhost:6006",
258
- excludeStories: ["Button/*"],
259
- });
260
-
261
- const result = await plugin.discover();
262
- expect(result).toHaveLength(4);
263
- expect(result.map((r) => r.data.story.id)).not.toContain(
264
- "button--primary",
265
- );
266
- expect(result.map((r) => r.data.story.id)).not.toContain(
267
- "button--secondary",
268
- );
269
- });
270
-
271
- it("should exclude stories matching multiple patterns", async () => {
272
- const plugin = cappaPluginStorybook({
273
- storybookUrl: "http://localhost:6006",
274
- excludeStories: ["button--*", "input--*"],
275
- });
276
-
277
- const result = await plugin.discover();
278
- expect(result).toHaveLength(2);
279
- expect(result.map((r) => r.data.story.id)).toEqual([
280
- "card--basic",
281
- "card--advanced",
282
- ]);
283
- });
284
- });
285
-
286
- describe("combined includeStories and excludeStories", () => {
287
- it("should apply include patterns first, then exclude patterns", async () => {
288
- const plugin = cappaPluginStorybook({
289
- storybookUrl: "http://localhost:6006",
290
- includeStories: ["*--*"], // Include all stories
291
- excludeStories: ["button--*"], // Then exclude button stories
292
- });
293
-
294
- const result = await plugin.discover();
295
- expect(result).toHaveLength(4);
296
- expect(result.map((r) => r.data.story.id)).not.toContain(
297
- "button--primary",
298
- );
299
- expect(result.map((r) => r.data.story.id)).not.toContain(
300
- "button--secondary",
301
- );
302
- });
303
-
304
- it("should work with overlapping patterns", async () => {
305
- const plugin = cappaPluginStorybook({
306
- storybookUrl: "http://localhost:6006",
307
- includeStories: ["*--primary", "*--default"], // Include primary and default stories
308
- excludeStories: ["button--*"], // Exclude all button stories
309
- });
310
-
311
- const result = await plugin.discover();
312
- expect(result).toHaveLength(1);
313
- expect(result[0].data.story.id).toBe("input--default");
314
- });
315
- });
316
-
317
- describe("edge cases", () => {
318
- it("should handle empty includeStories array", async () => {
319
- const plugin = cappaPluginStorybook({
320
- storybookUrl: "http://localhost:6006",
321
- includeStories: [],
322
- });
323
-
324
- const result = await plugin.discover();
325
- expect(result).toHaveLength(6); // All stories included
326
- });
327
-
328
- it("should handle empty excludeStories array", async () => {
329
- const plugin = cappaPluginStorybook({
330
- storybookUrl: "http://localhost:6006",
331
- excludeStories: [],
332
- });
333
-
334
- const result = await plugin.discover();
335
- expect(result).toHaveLength(6); // All stories included
336
- });
337
-
338
- it("should handle patterns that match no stories", async () => {
339
- const plugin = cappaPluginStorybook({
340
- storybookUrl: "http://localhost:6006",
341
- includeStories: ["nonexistent--*"],
342
- });
343
-
344
- const result = await plugin.discover();
345
- expect(result).toHaveLength(0);
346
- });
347
-
348
- it("should handle case-sensitive matching", async () => {
349
- const plugin = cappaPluginStorybook({
350
- storybookUrl: "http://localhost:6006",
351
- includeStories: ["button"], // lowercase
352
- });
353
-
354
- const result = await plugin.discover();
355
- expect(result).toHaveLength(0); // No matches because "Button" is capitalized
356
- });
357
-
358
- it("should handle special glob characters in story names", async () => {
359
- // Add a story with special characters
360
- const specialStory: StorybookStory = {
361
- id: "special--with-dots.and-brackets[test]",
362
- name: "With Dots.And Brackets[Test]",
363
- title: "Special",
364
- kind: "Special",
365
- story: "With Dots.And Brackets[Test]",
366
- type: "story",
367
- };
368
-
369
- const specialResponse = {
370
- entries: {
371
- ...mockStorybookResponse.entries,
372
- [specialStory.id]: specialStory,
373
- },
374
- };
375
-
376
- (global.fetch as any).mockResolvedValue({
377
- ok: true,
378
- json: () => Promise.resolve(specialResponse),
379
- });
380
-
381
- const plugin = cappaPluginStorybook({
382
- storybookUrl: "http://localhost:6006",
383
- includeStories: ["special--*"],
384
- });
385
-
386
- const result = await plugin.discover();
387
- expect(result).toHaveLength(1);
388
- expect(result[0].data.story.id).toBe(
389
- "special--with-dots.and-brackets[test]",
390
- );
391
- });
392
- });
393
- });
394
-
395
- describe("console logging configuration", () => {
396
- const story: StorybookStory = {
397
- id: "button--primary",
398
- name: "Primary",
399
- title: "Button",
400
- kind: "Button",
401
- story: "Primary",
402
- type: "story",
403
- };
404
-
405
- beforeEach(() => {
406
- vi.clearAllMocks();
407
- });
408
-
409
- const createPage = () => {
410
- const exposedFunctions: Record<
411
- string,
412
- (id: string, payload: unknown) => unknown
413
- > = {};
414
-
415
- return {
416
- on: vi.fn(),
417
- evaluate: vi.fn(async (fn: any) => {
418
- if (typeof fn === "function") {
419
- const source = fn.toString();
420
- if (source.includes("__cappa_parameters")) {
421
- return true;
422
- }
423
- if (source.includes("__cappa_has_play_function")) {
424
- return false;
425
- }
426
- }
427
-
428
- return undefined;
429
- }),
430
- exposeFunction: vi.fn(
431
- async (name: string, fn: (id: string, payload: unknown) => unknown) => {
432
- exposedFunctions[name] = fn;
433
- },
434
- ),
435
- goto: vi.fn(async () => {
436
- await exposedFunctions.__cappa_parameters?.(story.id, {
437
- viewport: { width: 800, height: 600 },
438
- });
439
- }),
440
- setViewportSize: vi.fn(async () => {}),
441
- viewportSize: vi.fn(() => ({ width: 1024, height: 768 })),
442
- waitForTimeout: vi.fn(async () => {}),
443
- locator: vi.fn(() => ({ locator: true })),
444
- waitForFunction: vi.fn(async () => {}),
445
- };
446
- };
447
-
448
- const createScreenshotTool = () => ({
449
- viewport: { width: 1024, height: 768 },
450
- logConsoleEvents: true,
451
- getVariantFilename: vi.fn(
452
- (filename: string, variant: { id: string }) =>
453
- `${filename}-${variant.id}.png`,
454
- ),
455
- captureWithVariants: vi.fn(async () => ({
456
- base: {
457
- filename: "button--primary.png",
458
- filepath: "button--primary.png",
459
- skipped: false,
460
- comparisonResult: { passed: true },
461
- },
462
- variants: [],
463
- })),
464
- });
465
-
466
- const createTask = () => ({
467
- id: story.id,
468
- url: "http://localhost:6006/iframe.html?id=button--primary",
469
- data: { story },
470
- });
471
-
472
- it("logs console events by default", async () => {
473
- const plugin = cappaPluginStorybook({
474
- storybookUrl: "http://localhost:6006",
475
- });
476
-
477
- const page = createPage();
478
- const screenshotTool = createScreenshotTool();
479
- const context = (await plugin.initPage?.()) ?? { latchMap: new Map() };
480
-
481
- await plugin.execute(
482
- createTask(),
483
- page as any,
484
- screenshotTool as any,
485
- context,
486
- );
487
-
488
- expect(
489
- (page.on as any).mock.calls.some(
490
- ([event]: [string]) => event === "console",
491
- ),
492
- ).toBe(true);
493
- });
494
-
495
- it("skips console logging when disabled", async () => {
496
- const page = createPage();
497
- const screenshotTool = {
498
- ...createScreenshotTool(),
499
- logConsoleEvents: false,
500
- };
501
- const plugin = cappaPluginStorybook({
502
- storybookUrl: "http://localhost:6006",
503
- });
504
- const context = (await plugin.initPage?.()) ?? { latchMap: new Map() };
505
-
506
- await plugin.execute(
507
- createTask(),
508
- page as any,
509
- screenshotTool as any,
510
- context,
511
- );
512
-
513
- expect(
514
- (page.on as any).mock.calls.some(
515
- ([event]: [string]) => event === "console",
516
- ),
517
- ).toBe(false);
518
- expect(
519
- (page.on as any).mock.calls.some(
520
- ([event]: [string]) => event === "pageerror",
521
- ),
522
- ).toBe(true);
523
- });
524
- });