|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import { render } from "@testing-library/react"; |
| 4 | +import { afterEach, expect, test, vi } from "vitest"; |
| 5 | + |
| 6 | +import { COMPONENT_METADATA_KEY } from "@cloudscape-design/component-toolkit/internal"; |
| 7 | + |
| 8 | +import useBaseComponent, { |
| 9 | + InternalBaseComponentProps, |
| 10 | +} from "../../../../lib/components/internal/base-component/use-base-component"; |
| 11 | +import { PACKAGE_SOURCE, PACKAGE_VERSION } from "../../../../lib/components/internal/environment"; |
| 12 | + |
| 13 | +type InternalDemoProps = InternalBaseComponentProps; |
| 14 | +function InternalDemo({ __internalRootRef }: InternalDemoProps) { |
| 15 | + return <div ref={__internalRootRef}>Internal Demo Component</div>; |
| 16 | +} |
| 17 | + |
| 18 | +declare global { |
| 19 | + interface Node { |
| 20 | + [COMPONENT_METADATA_KEY]?: { name: string; version: string; packageName: string; theme: string }; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function Demo() { |
| 25 | + const baseComponentProps = useBaseComponent("DemoComponent"); |
| 26 | + return <InternalDemo {...baseComponentProps} />; |
| 27 | +} |
| 28 | + |
| 29 | +vi.mock("../../../../lib/components/internal/utils/get-visual-theme", async (importOriginal) => { |
| 30 | + return { ...(await importOriginal()), getVisualTheme: vi.fn(() => "test theme") }; |
| 31 | +}); |
| 32 | + |
| 33 | +afterEach(() => { |
| 34 | + vi.resetAllMocks(); |
| 35 | +}); |
| 36 | + |
| 37 | +test("should attach the metadata to the returned root DOM node", () => { |
| 38 | + const { container } = render(<Demo />); |
| 39 | + const rootNode = container.firstChild; |
| 40 | + expect(rootNode![COMPONENT_METADATA_KEY]!.name).toBe("DemoComponent"); |
| 41 | + expect(rootNode![COMPONENT_METADATA_KEY]!.version).toBe(PACKAGE_VERSION); |
| 42 | + expect(rootNode![COMPONENT_METADATA_KEY]!.theme).toBe("test theme"); |
| 43 | + expect(rootNode![COMPONENT_METADATA_KEY]!.packageName).toBe(PACKAGE_SOURCE); |
| 44 | +}); |
0 commit comments