import React, { useMemo, useState, useEffect } from "react"; import { PaginationDots as Component } from "./"; import { theme, styled } from "../theme"; import { Button } from "../button"; import { InputText } from "../input-text"; import { within, userEvent } from "@storybook/testing-library"; import { expect } from "@storybook/jest"; import type { Meta, StoryFn } from "@storybook/react"; const Stack = styled("section", { display: "flex", flexDirection: "column", gap: "$100", marginBlockStart: "$200", borderRadius: "$075", }); const HStack = styled("section", { display: "flex", flexDirection: "row", gap: "$100", borderRadius: "$075", }); export default { title: "PaginationDots", component: Component, } as Meta; const DefaultArgs = { amount: 6, index: 1, label: "Pagination Dots", orientation: "horizontal", }; const Label = styled("h3", { color: theme.colors.primary, margin: 0, textAlign: "center", }); export const PaginationDots = { render: (args) => ( ), args: { ...DefaultArgs }, }; const Template: StoryFn = (args, context) => { const id = useMemo(() => `${Math.random()}-amount`.slice(2), []); const [amount, setAmount] = useState("5"); const [index, setIndex] = useState(1); useEffect(() => setAmount(amount), [amount]); return ( { setAmount(e.target.value); }} label="Total dots" name="pagination" /> ); }; export const WithControls = { render: Template, }; // Function to emulate pausing between interactions const sleep = (ms) => { return new Promise((resolve) => setTimeout(resolve, ms)); }; export const Interactions = { render: Template, play: async ({ canvasElement }) => { const canvas = within(canvasElement); const paginationDots = canvas.getByLabelText("light-pagination"); // change amount of dots await userEvent.type( canvas.getByTestId("light-input-text"), "{backspace}6" ); await sleep(500); // check incrementing index changes ariaValueNow await userEvent.click(canvas.getByTestId("light-inc-btn")); let ariaValueNow = paginationDots.getAttribute("aria-valuenow"); await sleep(500); expect(ariaValueNow).toBe("2"); await sleep(500); // traverse all dots for (let i = 0; i < 9; i++) { const btn = i < 4 ? "light-inc-btn" : "light-dec-btn"; await userEvent.click(canvas.getByTestId(btn)); await sleep(500); } // test ariaValueMax change on amount change await userEvent.type( canvas.getByTestId("light-input-text"), "{backspace}12" ); await sleep(500); let ariaValueMax = paginationDots.getAttribute("aria-valuemax"); expect(ariaValueMax).toBe("12"); // test dots never go below 0 await userEvent.type( canvas.getByTestId("light-input-text"), "{backspace}{backspace}0" ); await sleep(500); ariaValueMax = paginationDots.getAttribute("aria-valuemax"); ariaValueNow = paginationDots.getAttribute("aria-valuenow"); expect(ariaValueMax).toBe("1"); // expect(ariaValueNow).toBe("1"); // tests a large number of dots doesn't modify other elements await userEvent.type( canvas.getByTestId("light-input-text"), "{backspace}50" ); await sleep(500); }, }; export const Chromatic = () => ( );