|
| 1 | +import { screen } from "@testing-library/react"; |
| 2 | +import React from "react"; |
| 3 | +import { vi } from "vitest"; |
| 4 | +import { Button } from "../Button"; |
| 5 | +import { render, userClick, userKeyboard, userTab } from "../utilities/test"; |
| 6 | +import { ColorPicker, useColorPickerState } from "./ColorPicker"; |
| 7 | + |
| 8 | +describe("<ColorPicker />", () => { |
| 9 | + beforeEach(() => { |
| 10 | + vi.useFakeTimers(); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + vi.useRealTimers(); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should render", () => { |
| 18 | + render( |
| 19 | + <ColorPicker> |
| 20 | + <ColorPicker.Trigger> |
| 21 | + <Button>Pick a color</Button> |
| 22 | + </ColorPicker.Trigger> |
| 23 | + </ColorPicker>, |
| 24 | + ); |
| 25 | + expect(screen.getByText("Pick a color")).toBeInTheDocument(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should open", async () => { |
| 29 | + const { user } = render( |
| 30 | + <ColorPicker> |
| 31 | + <ColorPicker.Trigger> |
| 32 | + <Button>Pick a color</Button> |
| 33 | + </ColorPicker.Trigger> |
| 34 | + </ColorPicker>, |
| 35 | + ); |
| 36 | + await userClick(user, screen.getByText("Pick a color")); |
| 37 | + expect(screen.getAllByLabelText("Color picker").length).toBeGreaterThan(0); |
| 38 | + expect(screen.getByLabelText("Hue slider")).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should select a color", async () => { |
| 42 | + const handleChange = vi.fn(); |
| 43 | + const { user } = render( |
| 44 | + <ColorPicker defaultValue="#ff0000" onChange={handleChange}> |
| 45 | + <ColorPicker.Trigger> |
| 46 | + <Button>Pick a color</Button> |
| 47 | + </ColorPicker.Trigger> |
| 48 | + </ColorPicker>, |
| 49 | + ); |
| 50 | + await userClick(user, screen.getByText("Pick a color")); |
| 51 | + expect(screen.getAllByLabelText("Color picker").length).toBeGreaterThan(0); |
| 52 | + await userTab(user); |
| 53 | + await userKeyboard(user, "{ArrowRight}{ArrowUp}{ArrowLeft}{ArrowDown}"); |
| 54 | + expect(handleChange).toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should support context hook", async () => { |
| 58 | + function SelectedColor() { |
| 59 | + const { color } = useColorPickerState(); |
| 60 | + return <span>{color ? color.toString("css") : "None"}</span>; |
| 61 | + } |
| 62 | + |
| 63 | + const handleChange = vi.fn(); |
| 64 | + const { user } = render( |
| 65 | + <ColorPicker defaultValue="#ff0000" onChange={handleChange}> |
| 66 | + <SelectedColor /> |
| 67 | + <ColorPicker.Trigger> |
| 68 | + <Button>Pick a color</Button> |
| 69 | + </ColorPicker.Trigger> |
| 70 | + </ColorPicker>, |
| 71 | + ); |
| 72 | + |
| 73 | + await userClick(user, screen.getByText("Pick a color")); |
| 74 | + expect(screen.getAllByLabelText("Color picker").length).toBeGreaterThan(0); |
| 75 | + await userTab(user); |
| 76 | + await userKeyboard(user, "{ArrowRight}{ArrowUp}{ArrowLeft}{ArrowDown}"); |
| 77 | + expect(screen.getByText("hsla(0, 100%, 49.5%, 1)")).toBeInTheDocument(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments