|
| 1 | +import React from "react"; |
| 2 | +import { render, fireEvent } from "@testing-library/react-native"; |
| 3 | + |
| 4 | +import "~/lib/i18n"; // initialize i18next so t() resolves (the component chain doesn't) |
| 5 | +import { QuickCreateSheet } from "~/components/home/QuickCreateSheet"; |
| 6 | +import type { AppMeta } from "~/hooks/useApps"; |
| 7 | + |
| 8 | +// The global expo-router mock hands back a fresh `push` per call, so override it |
| 9 | +// here with a shared spy we can assert on. |
| 10 | +const mockPush = jest.fn(); |
| 11 | +jest.mock("expo-router", () => ({ |
| 12 | + useRouter: () => ({ push: mockPush, replace: jest.fn(), back: jest.fn() }), |
| 13 | +})); |
| 14 | + |
| 15 | +const apps: AppMeta[] = [ |
| 16 | + { |
| 17 | + name: "todo_app", |
| 18 | + label: "Todo", |
| 19 | + navigation: [ |
| 20 | + { |
| 21 | + id: "g1", |
| 22 | + type: "group", |
| 23 | + label: "Tasks", |
| 24 | + children: [ |
| 25 | + { id: "n1", type: "object", label: "All Tasks", objectName: "todo_task" }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + // Non-object leaves are ignored by quick-create. |
| 29 | + { id: "n2", type: "dashboard", label: "Dashboard", dashboardName: "d1" }, |
| 30 | + ], |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "crm", |
| 34 | + label: "CRM", |
| 35 | + navigation: [ |
| 36 | + { id: "n3", type: "object", label: "Contacts", objectName: "contact" }, |
| 37 | + // Same app/object surfaced under a second view — must dedupe. |
| 38 | + { id: "n4", type: "object", label: "Contacts (Kanban)", objectName: "contact" }, |
| 39 | + ], |
| 40 | + }, |
| 41 | +]; |
| 42 | + |
| 43 | +describe("QuickCreateSheet", () => { |
| 44 | + beforeEach(() => mockPush.mockClear()); |
| 45 | + |
| 46 | + it("lists creatable objects flattened from the apps' navigation, deduped", () => { |
| 47 | + const { getByText, queryByText, queryAllByText } = render( |
| 48 | + <QuickCreateSheet open onOpenChange={jest.fn()} apps={apps} />, |
| 49 | + ); |
| 50 | + |
| 51 | + // Object leaves (incl. those nested in a group) surface. |
| 52 | + expect(getByText("All Tasks")).toBeTruthy(); |
| 53 | + expect(getByText("Contacts")).toBeTruthy(); |
| 54 | + // The dashboard leaf is not a creatable object. |
| 55 | + expect(queryByText("Dashboard")).toBeNull(); |
| 56 | + // The second view over `contact` is deduped away. |
| 57 | + expect(queryByText("Contacts (Kanban)")).toBeNull(); |
| 58 | + expect(queryAllByText("Contacts")).toHaveLength(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it("opens the object's create form and closes the sheet on press", () => { |
| 62 | + const onOpenChange = jest.fn(); |
| 63 | + const { getByLabelText } = render( |
| 64 | + <QuickCreateSheet open onOpenChange={onOpenChange} apps={apps} />, |
| 65 | + ); |
| 66 | + |
| 67 | + fireEvent.press(getByLabelText("All Tasks")); |
| 68 | + |
| 69 | + expect(onOpenChange).toHaveBeenCalledWith(false); |
| 70 | + expect(mockPush).toHaveBeenCalledWith("/(app)/todo_app/todo_task/new"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("shows an empty hint when no app publishes a creatable object", () => { |
| 74 | + const { getByText } = render( |
| 75 | + <QuickCreateSheet open onOpenChange={jest.fn()} apps={[]} />, |
| 76 | + ); |
| 77 | + expect(getByText("No objects available to create.")).toBeTruthy(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments