Skip to content

Commit 74c6cbc

Browse files
authored
chore: Sync base component hook with upstream (#67)
1 parent 2466ea6 commit 74c6cbc

File tree

5 files changed

+37
-175
lines changed

5 files changed

+37
-175
lines changed
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33
import { render } from "@testing-library/react";
4-
import { expect, test, vi } from "vitest";
4+
import { afterEach, expect, test, vi } from "vitest";
55

6-
import { COMPONENT_METADATA_KEY } from "@cloudscape-design/component-toolkit/internal";
6+
import { COMPONENT_METADATA_KEY, useComponentMetrics } from "@cloudscape-design/component-toolkit/internal";
77

88
import useBaseComponent, {
99
InternalBaseComponentProps,
1010
} from "../../../../lib/components/internal/base-component/use-base-component";
11-
import { useTelemetry } from "../../../../lib/components/internal/base-component/use-telemetry";
1211
import { PACKAGE_VERSION } from "../../../../lib/components/internal/environment";
1312

1413
type InternalDemoProps = InternalBaseComponentProps;
1514
function InternalDemo({ __internalRootRef }: InternalDemoProps) {
1615
return <div ref={__internalRootRef}>Internal Demo Component</div>;
1716
}
1817

18+
declare global {
19+
interface Node {
20+
[COMPONENT_METADATA_KEY]?: { name: string; version: string };
21+
}
22+
}
23+
1924
function Demo({ variant }: { variant: string }) {
2025
const baseComponentProps = useBaseComponent("DemoComponent", { props: { variant } });
2126
return <InternalDemo {...baseComponentProps} />;
2227
}
2328

24-
vi.mock("../../../../lib/components/internal/base-component/use-telemetry", () => {
25-
return { useTelemetry: vi.fn(() => null) };
29+
vi.mock("@cloudscape-design/component-toolkit/internal", async (importOriginal) => {
30+
return { ...(await importOriginal()), useComponentMetrics: vi.fn(() => {}) };
31+
});
32+
33+
afterEach(() => {
34+
vi.resetAllMocks();
2635
});
2736

2837
test("should attach the metadata to the returned root DOM node", () => {
2938
const { container } = render(<Demo variant="default" />);
30-
const rootNode: any = container.firstChild;
31-
expect(rootNode[COMPONENT_METADATA_KEY]?.name).toBe("DemoComponent");
32-
expect(rootNode[COMPONENT_METADATA_KEY]?.version).toBe(PACKAGE_VERSION);
39+
const rootNode = container.firstChild;
40+
expect(rootNode![COMPONENT_METADATA_KEY]!.name).toBe("DemoComponent");
41+
expect(rootNode![COMPONENT_METADATA_KEY]!.version).toBe(PACKAGE_VERSION);
3342
});
3443

3544
test("should call the useTelemetry hook passing down the given component name and its props", () => {
36-
vi.resetAllMocks();
3745
render(<Demo variant="default" />);
38-
expect(useTelemetry).toHaveBeenCalledWith("DemoComponent", { props: { variant: "default" } });
46+
expect(useComponentMetrics).toHaveBeenCalledWith(
47+
"DemoComponent",
48+
expect.objectContaining({ packageVersion: PACKAGE_VERSION }),
49+
{
50+
props: { variant: "default" },
51+
},
52+
);
3953
});

src/internal/base-component/use-base-component.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import {
77
ComponentConfiguration,
88
initAwsUiVersions,
99
useComponentMetadata,
10+
useComponentMetrics,
11+
useFocusVisible,
1012
} from "@cloudscape-design/component-toolkit/internal";
1113

12-
import { PACKAGE_SOURCE, PACKAGE_VERSION } from "../environment";
13-
import useFocusVisible from "../utils/focus-visible";
14-
import { useTelemetry } from "./use-telemetry";
14+
import { PACKAGE_SOURCE, PACKAGE_VERSION, THEME } from "../environment";
15+
import { getVisualTheme } from "../utils/get-visual-theme";
16+
import { useVisualRefresh } from "./use-visual-refresh";
1517

1618
initAwsUiVersions(PACKAGE_SOURCE, PACKAGE_VERSION);
1719

@@ -25,8 +27,14 @@ export interface InternalBaseComponentProps {
2527
* root DOM node and emits the telemetry for this component.
2628
*/
2729
export default function useBaseComponent<T = any>(componentName: string, config?: ComponentConfiguration) {
28-
useTelemetry(componentName, config);
30+
const isVisualRefresh = useVisualRefresh();
31+
const theme = getVisualTheme(THEME, isVisualRefresh);
32+
useComponentMetrics(componentName, { packageSource: PACKAGE_SOURCE, packageVersion: PACKAGE_VERSION, theme }, config);
2933
useFocusVisible();
30-
const elementRef = useComponentMetadata<T>(componentName, PACKAGE_VERSION);
34+
const elementRef = useComponentMetadata<T>(componentName, {
35+
packageName: PACKAGE_SOURCE,
36+
version: PACKAGE_VERSION,
37+
theme,
38+
});
3139
return { __internalRootRef: elementRef };
3240
}

src/internal/base-component/use-telemetry.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/internal/utils/__tests__/focus-visible.test.tsx

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/internal/utils/focus-visible.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)