import React from "react"; import { Pressable } from "react-native"; import { fireEvent, render, screen } from "@testing-library/react-native"; import { getCell } from "../index"; import { standardCell } from "../standardCell"; import { selectableCell } from "../selectableCell"; import { dynamicCollectionCell } from "../dynamicCollectionCell"; import { mapMenuItemToPlaylistData } from "../../AudioPlayer/Components/Item"; describe("getCell", () => { it("returns selectable cell for collection_selector", () => { expect(getCell("collection_selector")).toBe(selectableCell); }); it("returns selectable cell for preference_editor", () => { expect(getCell("preference_editor")).toBe(selectableCell); }); it("returns standard cell when role is undefined", () => { expect(getCell(undefined)).toBe(standardCell); }); it("returns standard cell for unknown roles", () => { expect(getCell("unknown_role")).toBe(standardCell); }); it("returns the dynamic collection cell for dynamic_collection", () => { expect(getCell("dynamic_collection")).toBe(dynamicCollectionCell); }); it("does not use behavior to select the cell family", () => { expect(getCell(undefined, { selectMode: "multi" })).toBe(standardCell); expect(getCell("collection_selector", { selectMode: "single" })).toBe( selectableCell ); }); }); describe("mapMenuItemToPlaylistData", () => { const item = { title: "Playlist 1", summary: "5 items", icon: "https://example.com/icon.png", }; it("defaults to multiSelect trailing button when selectMode is multi or undefined", () => { const data1 = mapMenuItemToPlaylistData(item, { selectMode: "multi" }); expect(data1.trailingButton).toBe("multiSelect"); const data2 = mapMenuItemToPlaylistData(item); expect(data2.trailingButton).toBe("multiSelect"); }); it("omits trailing button when behavior selectMode is none", () => { const data = mapMenuItemToPlaylistData(item, { selectMode: "none" }); expect(data.trailingButton).toBeUndefined(); }); it("omits trailing button when item trailingButton is explicitly none", () => { const itemWithNone = { ...item, trailingButton: "none" }; const data = mapMenuItemToPlaylistData(itemWithNone, { selectMode: "multi", }); expect(data.trailingButton).toBeUndefined(); }); }); describe("selectableCell checkbox press", () => { it("invokes onPress with the playlist item when the checkbox is pressed", () => { const onPress = jest.fn(); const item = { id: "queue", title: "Queue" }; render( selectableCell.renderItem({ item, index: 0, onPress, context: { behavior: { selectMode: "multi" } } as any, }) as React.ReactElement ); const pressables = screen.UNSAFE_getAllByType(Pressable); fireEvent.press(pressables[pressables.length - 1]); expect(onPress).toHaveBeenCalledWith(item); }); });