|
| 1 | +/** |
| 2 | + * Tests for useBlankPageLayout – validates blank page layout management, |
| 3 | + * item addition, removal, reordering, updates, and template selection. |
| 4 | + */ |
| 5 | +import { renderHook, act } from "@testing-library/react-native"; |
| 6 | +import { useBlankPageLayout, LayoutItem } from "~/hooks/useBlankPageLayout"; |
| 7 | + |
| 8 | +describe("useBlankPageLayout", () => { |
| 9 | + it("returns default state initially", () => { |
| 10 | + const { result } = renderHook(() => useBlankPageLayout()); |
| 11 | + |
| 12 | + expect(result.current.items).toEqual([]); |
| 13 | + expect(result.current.template).toBe("blank"); |
| 14 | + }); |
| 15 | + |
| 16 | + it("sets layout items", () => { |
| 17 | + const { result } = renderHook(() => useBlankPageLayout()); |
| 18 | + |
| 19 | + const items: LayoutItem[] = [ |
| 20 | + { id: "i1", type: "text", config: { content: "Hello" }, order: 0 }, |
| 21 | + { id: "i2", type: "image", config: { src: "img.png" }, order: 1 }, |
| 22 | + ]; |
| 23 | + |
| 24 | + act(() => { |
| 25 | + result.current.setItems(items); |
| 26 | + }); |
| 27 | + |
| 28 | + expect(result.current.items).toHaveLength(2); |
| 29 | + }); |
| 30 | + |
| 31 | + it("adds an item sorted by order", () => { |
| 32 | + const { result } = renderHook(() => useBlankPageLayout()); |
| 33 | + |
| 34 | + act(() => { |
| 35 | + result.current.addItem({ id: "i2", type: "image", config: {}, order: 2 }); |
| 36 | + }); |
| 37 | + |
| 38 | + act(() => { |
| 39 | + result.current.addItem({ id: "i1", type: "text", config: {}, order: 1 }); |
| 40 | + }); |
| 41 | + |
| 42 | + expect(result.current.items).toHaveLength(2); |
| 43 | + expect(result.current.items[0].id).toBe("i1"); |
| 44 | + expect(result.current.items[1].id).toBe("i2"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("removes an item by id", () => { |
| 48 | + const { result } = renderHook(() => useBlankPageLayout()); |
| 49 | + |
| 50 | + act(() => { |
| 51 | + result.current.setItems([ |
| 52 | + { id: "i1", type: "text", config: {}, order: 0 }, |
| 53 | + { id: "i2", type: "image", config: {}, order: 1 }, |
| 54 | + ]); |
| 55 | + }); |
| 56 | + |
| 57 | + act(() => { |
| 58 | + result.current.removeItem("i1"); |
| 59 | + }); |
| 60 | + |
| 61 | + expect(result.current.items).toHaveLength(1); |
| 62 | + expect(result.current.items[0].id).toBe("i2"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("moves an item to a new order", () => { |
| 66 | + const { result } = renderHook(() => useBlankPageLayout()); |
| 67 | + |
| 68 | + act(() => { |
| 69 | + result.current.setItems([ |
| 70 | + { id: "i1", type: "text", config: {}, order: 0 }, |
| 71 | + { id: "i2", type: "image", config: {}, order: 1 }, |
| 72 | + { id: "i3", type: "video", config: {}, order: 2 }, |
| 73 | + ]); |
| 74 | + }); |
| 75 | + |
| 76 | + act(() => { |
| 77 | + result.current.moveItem("i1", 5); |
| 78 | + }); |
| 79 | + |
| 80 | + expect(result.current.items[0].id).toBe("i2"); |
| 81 | + expect(result.current.items[2].id).toBe("i1"); |
| 82 | + expect(result.current.items[2].order).toBe(5); |
| 83 | + }); |
| 84 | + |
| 85 | + it("updates an item's configuration", () => { |
| 86 | + const { result } = renderHook(() => useBlankPageLayout()); |
| 87 | + |
| 88 | + act(() => { |
| 89 | + result.current.setItems([ |
| 90 | + { id: "i1", type: "text", config: { content: "Hello" }, order: 0 }, |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | + act(() => { |
| 95 | + result.current.updateItem("i1", { config: { content: "Updated" } }); |
| 96 | + }); |
| 97 | + |
| 98 | + expect(result.current.items[0].config).toEqual({ content: "Updated" }); |
| 99 | + }); |
| 100 | + |
| 101 | + it("sets the template type", () => { |
| 102 | + const { result } = renderHook(() => useBlankPageLayout()); |
| 103 | + |
| 104 | + act(() => { |
| 105 | + result.current.setTemplate("dashboard"); |
| 106 | + }); |
| 107 | + |
| 108 | + expect(result.current.template).toBe("dashboard"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("changes template without affecting items", () => { |
| 112 | + const { result } = renderHook(() => useBlankPageLayout()); |
| 113 | + |
| 114 | + act(() => { |
| 115 | + result.current.addItem({ id: "i1", type: "text", config: {}, order: 0 }); |
| 116 | + }); |
| 117 | + |
| 118 | + act(() => { |
| 119 | + result.current.setTemplate("form"); |
| 120 | + }); |
| 121 | + |
| 122 | + expect(result.current.template).toBe("form"); |
| 123 | + expect(result.current.items).toHaveLength(1); |
| 124 | + }); |
| 125 | +}); |
0 commit comments