Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fail unit tests on React key warnings (#83062)

Romeo Van Snick759bc574e3ab
diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/metabot-analytics/components/ConversationFilters/ConversationFilters.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/metabot-analytics/components/ConversationFilters/ConversationFilters.tsx
index db8a615887a..18dcafacc35 100644
--- a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/metabot-analytics/components/ConversationFilters/ConversationFilters.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/metabot-analytics/components/ConversationFilters/ConversationFilters.tsx
@@ -74,9 +74,11 @@ function ConversationDateFilter({
!retentionCutoff ||
!getShortcutStartDate(item.value)?.isBefore(retentionCutoff);
+ // Mantine keys each group by its name, so the names must be unique. The
+ // dropdown hides them, which is why they are not translated.
return [
{
- group: "",
+ group: "days",
items: [
{ label: t`Today`, value: "thisday" },
{ label: t`Yesterday`, value: "past1days" },
@@ -85,7 +87,7 @@ function ConversationDateFilter({
].filter(withinRetention),
},
{
- group: "",
+ group: "months",
items: [
{ label: t`Previous month`, value: "past1months" },
{ label: t`Previous 3 months`, value: "past3months" },
@@ -93,7 +95,7 @@ function ConversationDateFilter({
].filter(withinRetention),
},
{
- group: "",
+ group: "custom range",
items: [
{ label: t`Fixed date range…`, value: SPECIFIC_TYPE_VALUE },
{ label: t`Relative date range…`, value: RELATIVE_TYPE_VALUE },
@@ -176,6 +178,7 @@ function ConversationDateFilter({
allowDeselect={false}
leftSection={<Icon name="calendar" />}
title={displayLabel}
+ styles={{ groupLabel: { display: "none" } }}
data-testid="conversation-filters-date-select"
/>
</Popover.Target>
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.tsx
index f0e2f42aae8..f3efd900d32 100644
--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.tsx
@@ -134,7 +134,7 @@ export const AllChangesView = ({ entities, title }: AllChangesViewProps) => {
group.items.length > 0 || group.tableGroups.length > 0;
return (
- <Fragment key={group.collectionId}>
+ <Fragment key={group.collectionId ?? "root"}>
{groupIndex > 0 && <Divider />}
<Box p="lg">
<Group
diff --git a/frontend/src/metabase/admin/settings/components/widgets/PublicLinksListing/PublicResources.unit.spec.tsx b/frontend/src/metabase/admin/settings/components/widgets/PublicLinksListing/PublicResources.unit.spec.tsx
index 1c04c995daf..8e3301f5e7b 100644
--- a/frontend/src/metabase/admin/settings/components/widgets/PublicLinksListing/PublicResources.unit.spec.tsx
+++ b/frontend/src/metabase/admin/settings/components/widgets/PublicLinksListing/PublicResources.unit.spec.tsx
@@ -94,7 +94,7 @@ describe("PublicResources", () => {
},
{
name: "Test Question 3",
- id: 2,
+ id: 3,
public_uuid: "5bec9624-3102-428a-8b9f-47c56bd96c6c",
},
]);
@@ -139,7 +139,7 @@ describe("PublicResources", () => {
},
{
name: "Test Dashboard 3",
- id: 2,
+ id: 3,
public_uuid: "2b80845e-0ff4-4182-8a9a-083cc904f5dd",
},
]);
diff --git a/frontend/src/metabase/auth/components/Login/Login.tsx b/frontend/src/metabase/auth/components/Login/Login.tsx
index 1ace1e60996..4902b160db6 100644
--- a/frontend/src/metabase/auth/components/Login/Login.tsx
+++ b/frontend/src/metabase/auth/components/Login/Login.tsx
@@ -1,3 +1,4 @@
+import { Fragment } from "react";
import { useMount } from "react-use";
import { t } from "ttag";
import _ from "underscore";
@@ -64,12 +65,12 @@ export const Login = (): JSX.Element => {
</Box>
))}
{passwordProvider.map((provider) => (
- <>
+ <Fragment key={provider.name}>
<Divider mt="2rem" />
- <Box key={provider.name} mt="1rem" ta="center">
+ <Box mt="1rem" ta="center">
<provider.Button isCard={true} redirectUrl={redirectUrl} />
</Box>
- </>
+ </Fragment>
))}
</Box>
)}
diff --git a/frontend/src/metabase/collections/components/CollectionMenu/CollectionMenu.tsx b/frontend/src/metabase/collections/components/CollectionMenu/CollectionMenu.tsx
index 97bcc80b35a..2211d9cabac 100644
--- a/frontend/src/metabase/collections/components/CollectionMenu/CollectionMenu.tsx
+++ b/frontend/src/metabase/collections/components/CollectionMenu/CollectionMenu.tsx
@@ -105,6 +105,7 @@ export const CollectionMenu = ({
if (hasDqCandidates) {
cleanupItems.push(
<Menu.Item
+ key="collection-move-questions"
leftSection={<Icon name="add_to_dash" />}
component={ForwardRefLink}
to={`${url}/move-questions-dashboard`}
diff --git a/frontend/src/metabase/timelines/collections/components/TimelineDetailsModal/TimelineDetailsModal.unit.spec.tsx b/frontend/src/metabase/timelines/collections/components/TimelineDetailsModal/TimelineDetailsModal.unit.spec.tsx
index afb30cdb460..743dc2e2008 100644
--- a/frontend/src/metabase/timelines/collections/components/TimelineDetailsModal/TimelineDetailsModal.unit.spec.tsx
+++ b/frontend/src/metabase/timelines/collections/components/TimelineDetailsModal/TimelineDetailsModal.unit.spec.tsx
@@ -51,9 +51,9 @@ describe("TimelineDetailsModal", () => {
const props = getProps({
timeline: createMockTimeline({
events: [
- createMockTimelineEvent({ name: "RC1" }),
- createMockTimelineEvent({ name: "RC2" }),
- createMockTimelineEvent({ name: "Release" }),
+ createMockTimelineEvent({ id: 1, name: "RC1" }),
+ createMockTimelineEvent({ id: 2, name: "RC2" }),
+ createMockTimelineEvent({ id: 3, name: "Release" }),
],
}),
});
diff --git a/frontend/src/metabase/transforms/pages/TransformIndexesPage/TransformIndexTable/TransformIndexTable.unit.spec.tsx b/frontend/src/metabase/transforms/pages/TransformIndexesPage/TransformIndexTable/TransformIndexTable.unit.spec.tsx
index f1a9ea12537..4a500f6c0c1 100644
--- a/frontend/src/metabase/transforms/pages/TransformIndexesPage/TransformIndexTable/TransformIndexTable.unit.spec.tsx
+++ b/frontend/src/metabase/transforms/pages/TransformIndexesPage/TransformIndexTable/TransformIndexTable.unit.spec.tsx
@@ -88,8 +88,16 @@ describe("TransformIndexTable", () => {
setup({
kindLabels: new Map([["btree", "B-Tree"]]),
indexes: [
- createMockTableIndexEntry({ name: "idx_btree", kind: "btree" }),
- createMockTableIndexEntry({ name: "idx_gin", kind: "gin" }),
+ createMockTableIndexEntry({
+ name: "idx_btree",
+ kind: "btree",
+ request: createMockTableIndexRequest({ id: 1 }),
+ }),
+ createMockTableIndexEntry({
+ name: "idx_gin",
+ kind: "gin",
+ request: createMockTableIndexRequest({ id: 2 }),
+ }),
],
});
await waitForLoaderToBeRemoved();
diff --git a/frontend/src/metabase/visualizations/components/settings/ColorRange/ColorRange.tsx b/frontend/src/metabase/visualizations/components/settings/ColorRange/ColorRange.tsx
index 5e889fb4dbc..6ae70af8576 100644
--- a/frontend/src/metabase/visualizations/components/settings/ColorRange/ColorRange.tsx
+++ b/frontend/src/metabase/visualizations/components/settings/ColorRange/ColorRange.tsx
@@ -52,10 +52,8 @@ export const ColorRange = forwardRef(function ColorRange(
)}
>
{_.range(0, sections).map((section) => (
- <>
- {/* @ts-expect-error color range needs access to arbitrary color values */}
- <Box key={section} flex="1" bg={scale(section)} />
- </>
+ // @ts-expect-error color range needs access to arbitrary color values
+ <Box key={section} flex="1" bg={scale(section)} />
))}
</Flex>
);
diff --git a/frontend/test/jest-setup-env.js b/frontend/test/jest-setup-env.js
index 3410d3f5133..b4c43a3f25b 100644
--- a/frontend/test/jest-setup-env.js
+++ b/frontend/test/jest-setup-env.js
@@ -1,7 +1,42 @@
+import { format } from "util";
+
import "@testing-library/jest-dom";
import { cleanup } from "@testing-library/react";
import fetchMock from "fetch-mock";
+const REACT_KEY_WARNING_PATTERNS = [
+ /Each child in a list should have a unique "key" prop/,
+ /Encountered two children with the same key/,
+];
+
+const reactKeyWarnings = [];
+const originalConsoleError = console.error;
+
+// Throwing here would land inside React's render, where an error boundary can
+// swallow it, so the warnings are collected and reported after the test.
+console.error = (...args) => {
+ originalConsoleError(...args);
+
+ const [template] = args;
+ const isReactKeyWarning =
+ typeof template === "string" &&
+ REACT_KEY_WARNING_PATTERNS.some((pattern) => pattern.test(template));
+
+ if (isReactKeyWarning) {
+ reactKeyWarnings.push(format(...args));
+ }
+};
+
+afterEach(() => {
+ const warnings = reactKeyWarnings.splice(0);
+
+ if (warnings.length > 0) {
+ throw new Error(
+ `Test completed with React key warnings:\n${warnings.join("\n")}`,
+ );
+ }
+});
+
// jsdom has no layout, so popover positioning computes nothing useful while
// costing getComputedStyle calls and an extra re-render per position update.
jest.mock("@floating-ui/dom", () => ({