|
| 1 | +import { page } from "@vitest/browser/context"; |
| 2 | +import { expect, it } from "vitest"; |
| 3 | +import { render } from "vitest-browser-svelte"; |
| 4 | +import type { ComponentProps } from "svelte"; |
| 5 | +import CommandScrollTest from "./command-scroll-test.svelte"; |
| 6 | + |
| 7 | +function setup(props: Partial<ComponentProps<typeof CommandScrollTest>> = {}) { |
| 8 | + // oxlint-disable-next-line no-explicit-any |
| 9 | + const returned = render(CommandScrollTest, props as any); |
| 10 | + const input = page.getByTestId("input"); |
| 11 | + const root = page.getByTestId("root"); |
| 12 | + const list = page.getByTestId("list"); |
| 13 | + const viewport = page.getByTestId("viewport"); |
| 14 | + return { |
| 15 | + ...returned, |
| 16 | + root, |
| 17 | + input, |
| 18 | + list, |
| 19 | + viewport, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +it("should scroll initial value into view when it's not the first item", async () => { |
| 24 | + setup({ value: "Popover" }); |
| 25 | + |
| 26 | + const item = page.getByText("Popover"); |
| 27 | + await expect.element(item).toHaveAttribute("data-selected"); |
| 28 | + |
| 29 | + // check that the item is visible in the viewport |
| 30 | + const itemElement = item.element() as HTMLElement; |
| 31 | + const viewport = page.getByTestId("viewport").element() as HTMLElement; |
| 32 | + |
| 33 | + // wait for any scroll animations to complete |
| 34 | + await new Promise((resolve) => setTimeout(resolve, 150)); |
| 35 | + |
| 36 | + const itemRect = itemElement.getBoundingClientRect(); |
| 37 | + const viewportRect = viewport.getBoundingClientRect(); |
| 38 | + |
| 39 | + // verify the selected item is within the viewport bounds |
| 40 | + expect(itemRect.top).toBeGreaterThanOrEqual(viewportRect.top - 1); // allow 1px tolerance |
| 41 | + expect(itemRect.bottom).toBeLessThanOrEqual(viewportRect.bottom + 1); // allow 1px tolerance |
| 42 | +}); |
| 43 | + |
| 44 | +it("should scroll initial value in the middle of the list into view", async () => { |
| 45 | + setup({ value: "Radio Group" }); |
| 46 | + |
| 47 | + const item = page.getByText("Radio Group"); |
| 48 | + await expect.element(item).toHaveAttribute("data-selected"); |
| 49 | + |
| 50 | + const itemElement = item.element() as HTMLElement; |
| 51 | + const viewport = page.getByTestId("viewport").element() as HTMLElement; |
| 52 | + |
| 53 | + // wait for any scroll animations to complete |
| 54 | + await new Promise((resolve) => setTimeout(resolve, 150)); |
| 55 | + |
| 56 | + const itemRect = itemElement.getBoundingClientRect(); |
| 57 | + const viewportRect = viewport.getBoundingClientRect(); |
| 58 | + |
| 59 | + // verify the selected item is within the viewport bounds |
| 60 | + expect(itemRect.top).toBeGreaterThanOrEqual(viewportRect.top - 1); |
| 61 | + expect(itemRect.bottom).toBeLessThanOrEqual(viewportRect.bottom + 1); |
| 62 | +}); |
| 63 | + |
| 64 | +it("should respect disableInitialScroll prop and not scroll", async () => { |
| 65 | + setup({ value: "Popover", disableInitialScroll: true }); |
| 66 | + |
| 67 | + const item = page.getByText("Popover"); |
| 68 | + await expect.element(item).toHaveAttribute("data-selected"); |
| 69 | + |
| 70 | + const viewport = page.getByTestId("viewport").element() as HTMLElement; |
| 71 | + |
| 72 | + // wait a bit to ensure no scrolling happens |
| 73 | + await new Promise((resolve) => setTimeout(resolve, 150)); |
| 74 | + |
| 75 | + // with disableInitialScroll, viewport should remain at the top |
| 76 | + expect(viewport.scrollTop).toBe(0); |
| 77 | +}); |
| 78 | + |
| 79 | +it("should not scroll when initial value is the first item", async () => { |
| 80 | + setup({ value: "Introduction" }); |
| 81 | + |
| 82 | + const item = page.getByText("Introduction"); |
| 83 | + await expect.element(item).toHaveAttribute("data-selected"); |
| 84 | + |
| 85 | + const viewport = page.getByTestId("viewport").element() as HTMLElement; |
| 86 | + |
| 87 | + // wait a bit |
| 88 | + await new Promise((resolve) => setTimeout(resolve, 150)); |
| 89 | + |
| 90 | + // viewport should remain at the top since first item is already visible |
| 91 | + expect(viewport.scrollTop).toBe(0); |
| 92 | +}); |
0 commit comments