|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import type { DocgenProviderDescriptor, Options } from 'storybook/internal/types'; |
| 4 | + |
| 5 | +import type { FrameworkOptions } from '../types.ts'; |
| 6 | +import { experimental_docgenProvider, experimental_manifests } from './preset.ts'; |
| 7 | + |
| 8 | +const optionsWith = (docgen?: FrameworkOptions['docgen'], features: Record<string, boolean> = {}) => |
| 9 | + ({ |
| 10 | + presets: { |
| 11 | + apply: async (key: string) => { |
| 12 | + if (key === 'frameworkOptions') { |
| 13 | + return { docgen }; |
| 14 | + } |
| 15 | + return key === 'features' ? features : {}; |
| 16 | + }, |
| 17 | + }, |
| 18 | + }) as unknown as Options; |
| 19 | + |
| 20 | +const existing: DocgenProviderDescriptor[] = [{ moduleSpecifier: '/addon/docgen-worker.js' }]; |
| 21 | +const docgenServerOn = { experimentalDocgenServer: true }; |
| 22 | +const componentsManifestOn = { ...docgenServerOn, componentsManifest: true }; |
| 23 | + |
| 24 | +describe('experimental_docgenProvider', () => { |
| 25 | + it('appends a descriptor pointing at the renderer worker module', async () => { |
| 26 | + const descriptors = await experimental_docgenProvider( |
| 27 | + existing, |
| 28 | + optionsWith('vue-component-meta', docgenServerOn) |
| 29 | + ); |
| 30 | + |
| 31 | + expect(descriptors).toHaveLength(2); |
| 32 | + // Appended, so addon providers stack on top of ours rather than replacing it. |
| 33 | + expect(descriptors[0]).toBe(existing[0]); |
| 34 | + expect(descriptors[1].moduleSpecifier).toMatch(/docgen-worker\.js$/); |
| 35 | + }); |
| 36 | + |
| 37 | + // The worker extracts with vue-component-meta only. Registering for vue-docgen-api would |
| 38 | + // silently swap the engine the project asked for; `docgen: false` opted out of extraction |
| 39 | + // altogether and must stay opted out. |
| 40 | + it.each(['vue-docgen-api' as const, undefined, false as const])( |
| 41 | + 'registers nothing for docgen: %s', |
| 42 | + async (docgen) => { |
| 43 | + await expect( |
| 44 | + experimental_docgenProvider(existing, optionsWith(docgen, docgenServerOn)) |
| 45 | + ).resolves.toEqual(existing); |
| 46 | + } |
| 47 | + ); |
| 48 | +}); |
| 49 | + |
| 50 | +describe('experimental_manifests', () => { |
| 51 | + const manifests = (docgen?: FrameworkOptions['docgen'], features?: Record<string, boolean>) => |
| 52 | + experimental_manifests({}, optionsWith(docgen, features) as never); |
| 53 | + |
| 54 | + // Core asserts `components.meta.docgen` is present whenever the feature is on, so omitting it |
| 55 | + // fails a Vue `storybook build` outright rather than degrading the debugger. |
| 56 | + it('declares the engine so core can label the components debugger', async () => { |
| 57 | + await expect(manifests('vue-component-meta', componentsManifestOn)).resolves.toEqual({ |
| 58 | + components: { v: 0, components: {}, meta: { docgen: 'vue-component-meta', durationMs: 0 } }, |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it.each(['vue-docgen-api' as const, undefined, false as const])( |
| 63 | + 'rejects the components manifest for docgen: %s', |
| 64 | + async (docgen) => { |
| 65 | + await expect(manifests(docgen, componentsManifestOn)).rejects.toThrow( |
| 66 | + "The Vue docgen manifest currently requires `docgen: 'vue-component-meta'` in `framework.options`." |
| 67 | + ); |
| 68 | + } |
| 69 | + ); |
| 70 | + |
| 71 | + // The manifest is populated from docgen-service payloads. When another engine (or none) runs, |
| 72 | + // there are no payloads, so claiming an engine here would report "0 components" against it. |
| 73 | + it.each(['vue-docgen-api' as const, undefined, false as const])( |
| 74 | + 'contributes nothing for docgen: %s', |
| 75 | + async (docgen) => { |
| 76 | + await expect(manifests(docgen, docgenServerOn)).resolves.toEqual({}); |
| 77 | + } |
| 78 | + ); |
| 79 | + |
| 80 | + // Vue has no legacy component manifest, so with the feature off there is nothing to contribute. |
| 81 | + it('contributes nothing when the docgen service is off', async () => { |
| 82 | + await expect(manifests('vue-component-meta')).resolves.toEqual({}); |
| 83 | + }); |
| 84 | +}); |
0 commit comments