hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { fireEvent, render, screen } from "@solidjs/testing-library"; |
| 2 | import { FloatingActionButton } from "@/components/layout/floating-action-button"; |
| 3 | import { IconBell } from "@/components/ui/icons"; |
| 4 | import { PreferencesProvider } from "@/stores/preferences-context"; |
| 5 | |
| 6 | // A 400×700 layer: the hide target's top-left is (172, 632), the resting |
| 7 | // bottom-right button's is (332, 632). |
| 8 | const W = 400; |
| 9 | const H = 700; |
| 10 | |
| 11 | function renderFab(onSelect = vi.fn()) { |
| 12 | render(() => ( |
| 13 | <PreferencesProvider> |
| 14 | <FloatingActionButton |
| 15 | actions={[{ id: "notifications", label: "Bildirimler", Icon: IconBell, badge: 2, onSelect }]} |
| 16 | badge={3} |
| 17 | hidden={false} |
| 18 | /> |
| 19 | </PreferencesProvider> |
| 20 | )); |
| 21 | return { fab: screen.getByRole("button", { name: /hızlı işlemler|quick actions/i, expanded: false }), onSelect }; |
| 22 | } |
| 23 | |
| 24 | function drag(el: HTMLElement, from: [number, number], to: [number, number]) { |
| 25 | fireEvent.pointerDown(el, { pointerId: 1, clientX: from[0], clientY: from[1], button: 0, pointerType: "touch" }); |
| 26 | fireEvent.pointerMove(el, { pointerId: 1, clientX: (from[0] + to[0]) / 2, clientY: (from[1] + to[1]) / 2 }); |
| 27 | fireEvent.pointerMove(el, { pointerId: 1, clientX: to[0], clientY: to[1] }); |
| 28 | fireEvent.pointerUp(el, { pointerId: 1, clientX: to[0], clientY: to[1] }); |
| 29 | fireEvent.click(el); |
| 30 | } |
| 31 | |
| 32 | describe("FloatingActionButton", () => { |
| 33 | beforeEach(() => { |
| 34 | localStorage.clear(); |
| 35 | vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockReturnValue({ |
| 36 | width: W, height: H, top: 0, left: 0, right: W, bottom: H, x: 0, y: 0, toJSON: () => ({}), |
| 37 | } as DOMRect); |
| 38 | HTMLElement.prototype.setPointerCapture = vi.fn(); |
| 39 | }); |
| 40 | afterEach(() => vi.restoreAllMocks()); |
| 41 | |
| 42 | it("fans its actions out on a tap and runs the one picked", () => { |
| 43 | const { fab, onSelect } = renderFab(); |
| 44 | fireEvent.pointerDown(fab, { pointerId: 1, clientX: 10, clientY: 10, button: 0 }); |
| 45 | fireEvent.pointerUp(fab, { pointerId: 1, clientX: 12, clientY: 11 }); |
| 46 | fireEvent.click(fab); |
| 47 | expect(fab.getAttribute("aria-expanded")).toBe("true"); |
| 48 | |
| 49 | fireEvent.click(screen.getByRole("button", { name: "Bildirimler" })); |
| 50 | expect(onSelect).toHaveBeenCalledOnce(); |
| 51 | expect(fab.getAttribute("aria-expanded")).toBe("false"); |
| 52 | }); |
| 53 | |
| 54 | it("remembers where it was dropped, and a drag is not a tap", () => { |
| 55 | const { fab } = renderFab(); |
| 56 | drag(fab, [360, 660], [60, 300]); |
| 57 | expect(fab.getAttribute("aria-expanded")).toBe("false"); |
| 58 | const saved = JSON.parse(localStorage.getItem("hezarfen.floatingButton")!); |
| 59 | expect(saved.parked).toBe(false); |
| 60 | expect(saved.x).toBeLessThan(0.1); |
| 61 | expect(saved.y).toBeGreaterThan(0.3); |
| 62 | expect(saved.y).toBeLessThan(0.5); |
| 63 | }); |
| 64 | |
| 65 | it("parks on the edge when dropped on the hide target and comes back with one tap", () => { |
| 66 | const { fab } = renderFab(); |
| 67 | // 332 → 172 horizontally lands the button on the hide target. |
| 68 | drag(fab, [360, 660], [200, 660]); |
| 69 | expect(screen.queryByRole("button", { name: /hızlı işlemler|quick actions/i, expanded: false })).toBeNull(); |
| 70 | const saved = JSON.parse(localStorage.getItem("hezarfen.floatingButton")!); |
| 71 | // Parked, but still remembering the spot it was dragged from. |
| 72 | expect(saved).toEqual({ x: 1, y: 1, parked: true }); |
| 73 | |
| 74 | fireEvent.click(screen.getByRole("button", { name: /göster|show quick actions/i })); |
| 75 | expect(screen.getByRole("button", { name: /hızlı işlemler|quick actions/i, expanded: false })).toBeTruthy(); |
| 76 | expect(JSON.parse(localStorage.getItem("hezarfen.floatingButton")!).parked).toBe(false); |
| 77 | }); |
| 78 | }); |