Skip to content

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

Migrate snippets CollectionRow from JSX to TS (#75138)

Andrei Chernykhd34785dc6ea1
diff --git a/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.jsx b/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.tsx
similarity index 74%
rename from enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.jsx
rename to enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.tsx
index 9342ae6558b..5ce1a470255 100644
--- a/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.jsx
+++ b/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.tsx
@@ -1,17 +1,23 @@
-/* eslint-disable react/prop-types */
import cx from "classnames";
import { CollectionRowMenu } from "metabase/collections/components/CollectionRowMenu";
import CS from "metabase/css/core/index.css";
import { Ellipsified, Icon } from "metabase/ui";
+import type { Collection, CollectionId } from "metabase-types/api";
const ICON_SIZE = 16;
-function CollectionRow({ item: collection, setSnippetCollectionId }) {
+interface CollectionRowProps {
+ item: Collection;
+ setSnippetCollectionId?: (id: CollectionId) => void;
+}
+
+export function CollectionRow({
+ item: collection,
+ setSnippetCollectionId,
+}: CollectionRowProps) {
const onSelectCollection = () => {
- if (setSnippetCollectionId) {
- setSnippetCollectionId(collection.id);
- }
+ setSnippetCollectionId?.(collection.id);
};
return (
@@ -41,6 +47,3 @@ function CollectionRow({ item: collection, setSnippetCollectionId }) {
</div>
);
}
-
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export default CollectionRow;
diff --git a/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.unit.spec.tsx
new file mode 100644
index 00000000000..3d186cb9f07
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.unit.spec.tsx
@@ -0,0 +1,56 @@
+import userEvent from "@testing-library/user-event";
+
+import { renderWithProviders, screen } from "__support__/ui";
+import { createMockCollection } from "metabase-types/api/mocks";
+
+import { CollectionRow } from "./CollectionRow";
+
+const COLLECTION_NAME = "My snippets folder";
+
+const setup = ({
+ archived = false,
+ setSnippetCollectionId = jest.fn(),
+}: {
+ archived?: boolean;
+ setSnippetCollectionId?: (id: number | string) => void;
+} = {}) => {
+ const collection = createMockCollection({
+ id: 7,
+ name: COLLECTION_NAME,
+ archived,
+ can_write: true,
+ });
+
+ renderWithProviders(
+ <CollectionRow
+ item={collection}
+ setSnippetCollectionId={setSnippetCollectionId}
+ />,
+ );
+
+ return { setSnippetCollectionId };
+};
+
+describe("CollectionRow", () => {
+ it("renders the collection name", () => {
+ setup();
+
+ expect(screen.getByText(COLLECTION_NAME)).toBeInTheDocument();
+ });
+
+ it("selects the collection when clicked", async () => {
+ const { setSnippetCollectionId } = setup();
+
+ await userEvent.click(screen.getByText(COLLECTION_NAME));
+
+ expect(setSnippetCollectionId).toHaveBeenCalledWith(7);
+ });
+
+ it("is not clickable when the collection is archived", async () => {
+ const { setSnippetCollectionId } = setup({ archived: true });
+
+ await userEvent.click(screen.getByText(COLLECTION_NAME));
+
+ expect(setSnippetCollectionId).not.toHaveBeenCalled();
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/snippets/index.js b/enterprise/frontend/src/metabase-enterprise/snippets/index.js
index 1c9623a986f..61dbef5cf94 100644
--- a/enterprise/frontend/src/metabase-enterprise/snippets/index.js
+++ b/enterprise/frontend/src/metabase-enterprise/snippets/index.js
@@ -11,7 +11,7 @@ import {
import { setOpenModalWithProps } from "metabase/redux/ui";
import { hasPremiumFeature } from "metabase-enterprise/settings";
-import CollectionRow from "./components/CollectionRow";
+import { CollectionRow } from "./components/CollectionRow";
import { MoveSnippetModal } from "./components/MoveSnippetModal";
import { SnippetCollectionPermissionsModal } from "./components/SnippetCollectionPermissionsModal";
import { SnippetCollectionPickerModal } from "./components/SnippetCollectionPickerModal";