|
1 | 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
2 | | -import { userEvent } from "vite-plus/test/browser"; |
| 2 | +import { page, userEvent } from "vite-plus/test/browser"; |
3 | 3 |
|
4 | 4 | import type { ReactNode } from "react"; |
5 | 5 |
|
6 | 6 | import { act, render } from "../../../test/render"; |
7 | 7 | import { ThemeColorField } from "./ThemeColorPicker"; |
| 8 | +import { ProviderAccentColorPicker } from "./ProviderAccentColorPicker"; |
8 | 9 |
|
9 | 10 | const HUE = "Accent color hue"; |
10 | 11 | const PLANE = "Accent color saturation and brightness"; |
@@ -82,3 +83,88 @@ describe("theme color field", () => { |
82 | 83 | expect(slider(HUE).getAttribute("aria-valuenow")).toBe("10"); |
83 | 84 | }); |
84 | 85 | }); |
| 86 | + |
| 87 | +describe("provider accent color picker", () => { |
| 88 | + it("leaves the optional color unset when the preview is dismissed", async () => { |
| 89 | + const onCommit = vi.fn(); |
| 90 | + await using _screen = await renderPicker( |
| 91 | + <ProviderAccentColorPicker |
| 92 | + displayName="Example provider" |
| 93 | + value={undefined} |
| 94 | + onCommit={onCommit} |
| 95 | + />, |
| 96 | + ); |
| 97 | + |
| 98 | + await open("Add accent color for Example provider"); |
| 99 | + expect(byLabel<HTMLInputElement>("Custom hex accent color").value).toBe("#2563eb"); |
| 100 | + expect(onCommit).not.toHaveBeenCalled(); |
| 101 | + await userEvent.keyboard("{Escape}"); |
| 102 | + expect(onCommit).not.toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("adds the displayed default without adjusting a color control", async () => { |
| 106 | + const onCommit = vi.fn(); |
| 107 | + await using screen = await renderPicker( |
| 108 | + <ProviderAccentColorPicker |
| 109 | + displayName="Example provider" |
| 110 | + value={undefined} |
| 111 | + onCommit={onCommit} |
| 112 | + />, |
| 113 | + ); |
| 114 | + |
| 115 | + await open("Add accent color for Example provider"); |
| 116 | + await userEvent.click(page.getByRole("button", { name: "Add color", exact: true })); |
| 117 | + |
| 118 | + expect(onCommit).toHaveBeenCalledExactlyOnceWith("#2563eb"); |
| 119 | + await expect |
| 120 | + .element(screen.getByRole("button", { name: "Change accent color for Example provider" })) |
| 121 | + .toBeVisible(); |
| 122 | + await vi.waitFor(() => |
| 123 | + expect(document.querySelector('[aria-label="Custom hex accent color"]')).toBeNull(), |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it("lets keyboard users add the displayed default", async () => { |
| 128 | + const onCommit = vi.fn(); |
| 129 | + await using screen = await renderPicker( |
| 130 | + <ProviderAccentColorPicker |
| 131 | + displayName="Example provider" |
| 132 | + value={undefined} |
| 133 | + onCommit={onCommit} |
| 134 | + />, |
| 135 | + ); |
| 136 | + |
| 137 | + await open("Add accent color for Example provider"); |
| 138 | + await act(async () => byLabel("Accent color saturation").focus()); |
| 139 | + await userEvent.tab(); |
| 140 | + await userEvent.tab(); |
| 141 | + await userEvent.tab(); |
| 142 | + await userEvent.tab(); |
| 143 | + await userEvent.keyboard("{Enter}"); |
| 144 | + |
| 145 | + expect(onCommit).toHaveBeenCalledExactlyOnceWith("#2563eb"); |
| 146 | + await expect |
| 147 | + .element(screen.getByRole("button", { name: "Change accent color for Example provider" })) |
| 148 | + .toBeVisible(); |
| 149 | + }); |
| 150 | + |
| 151 | + it("keeps live color editing and allows clearing back to unset", async () => { |
| 152 | + const onCommit = vi.fn(); |
| 153 | + await using screen = await renderPicker( |
| 154 | + <ProviderAccentColorPicker |
| 155 | + displayName="Example provider" |
| 156 | + value="#ff0000" |
| 157 | + onCommit={onCommit} |
| 158 | + />, |
| 159 | + ); |
| 160 | + |
| 161 | + await open("Change accent color for Example provider"); |
| 162 | + await press(HUE, "ArrowRight", true); |
| 163 | + expect(onCommit).toHaveBeenLastCalledWith("#ff2a00"); |
| 164 | + await userEvent.click(page.getByRole("button", { name: "Clear color", exact: true })); |
| 165 | + expect(onCommit).toHaveBeenLastCalledWith(""); |
| 166 | + await expect |
| 167 | + .element(screen.getByRole("button", { name: "Add accent color for Example provider" })) |
| 168 | + .toBeVisible(); |
| 169 | + }); |
| 170 | +}); |
0 commit comments