// @vitest-environment happy-dom import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { logger, once } from 'storybook/internal/client-logger'; import { getCustomElements, isValidMetaData, setCustomElements, setCustomElementsManifest, } from './framework-api.ts'; const RUNTIME_MANIFEST_DEPRECATION = 'The runtime Custom Elements Manifest is deprecated and will be removed in Storybook 12. Set framework.options.customElementsManifest in .storybook/main and enable features.docgenServer instead.'; describe('framework-api', (): void => { beforeEach((): void => { once.clear(); vi.stubGlobal('__STORYBOOK_CUSTOM_ELEMENTS__', undefined); vi.stubGlobal('__STORYBOOK_CUSTOM_ELEMENTS_MANIFEST__', undefined); }); afterEach((): void => { vi.restoreAllMocks(); once.clear(); }); it.each([ [ 'setCustomElements', setCustomElements, `setCustomElements() is deprecated: ${RUNTIME_MANIFEST_DEPRECATION}`, ], [ 'setCustomElementsManifest', setCustomElementsManifest, `setCustomElementsManifest() is deprecated: ${RUNTIME_MANIFEST_DEPRECATION}`, ], ])('warns once for %s', (_name, setter, expectedWarning): void => { const warn = vi.spyOn(logger, 'warn').mockImplementation((): void => {}); setter({ tags: [] }); setter({ tags: [] }); expect(warn.mock.calls).toEqual([[expectedWarning]]); }); it('prefers the manifest global over the legacy global', (): void => { vi.stubGlobal('__STORYBOOK_CUSTOM_ELEMENTS__', { tags: [{ name: 'legacy-element' }] }); vi.stubGlobal('__STORYBOOK_CUSTOM_ELEMENTS_MANIFEST__', { modules: [{ declarations: [{ tagName: 'manifest-element' }] }], }); expect(getCustomElements()).toMatchInlineSnapshot(` { "modules": [ { "declarations": [ { "tagName": "manifest-element", }, ], }, ], } `); }); it('throws for invalid metadata', (): void => { expect(() => isValidMetaData({})).toThrowErrorMatchingInlineSnapshot( `[Error: You need to set up valid Custom Elements metadata with a manifest generated by @custom-elements-manifest/analyzer. Configure it with framework.options.customElementsManifest in .storybook/main.]` ); }); });