Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

1.3 KB
Raw Blame
// @vitest-environment jsdom
import "@testing-library/jest-dom";
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { I18nProvider } from "@/contexts/I18nContext";
import { TextColorField } from "./TextColorField";

afterEach(cleanup);

function renderOn(plate: string, onChange = vi.fn()) {
	render(
		<I18nProvider>
			<TextColorField label="Text colour" value="#ffffff" plate={plate} onChange={onChange} />
		</I18nProvider>,
	);
	return onChange;
}

// By its name: the hex is its tooltip, drawn only on hover.
const swatch = (color: string) => screen.queryByRole("button", { name: `Set color ${color}` });

describe("TextColorField", () => {
	it("offers only the palette colours that read on a dark plate", () => {
		renderOn("rgba(0, 0, 0, 0.7)");
		expect(swatch("#ffffff")).toBeInTheDocument();
		expect(swatch("#111111")).not.toBeInTheDocument();
	});

	it("offers the whole palette when there is no plate", () => {
		renderOn("transparent");
		expect(swatch("#ffffff")).toBeInTheDocument();
		expect(swatch("#111111")).toBeInTheDocument();
	});

	it("applies a swatch on click", () => {
		const onChange = renderOn("rgba(255, 255, 255, 0.85)");
		fireEvent.click(swatch("#1d4ed8") as HTMLElement);
		expect(onChange).toHaveBeenCalledWith("#1d4ed8");
	});
});