@open-elements/ui 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/components/action-icon-button.d.ts +11 -0
  2. package/dist/components/action-icon-button.d.ts.map +1 -0
  3. package/dist/components/action-icon-button.js +14 -0
  4. package/dist/components/action-icon-button.js.map +1 -0
  5. package/dist/components/combobox.d.ts +1 -1
  6. package/dist/components/combobox.d.ts.map +1 -1
  7. package/dist/components/combobox.js +1 -1
  8. package/dist/components/combobox.js.map +1 -1
  9. package/dist/components/copy-to-clipboard-button.d.ts +7 -0
  10. package/dist/components/copy-to-clipboard-button.d.ts.map +1 -0
  11. package/dist/components/copy-to-clipboard-button.js +16 -0
  12. package/dist/components/copy-to-clipboard-button.js.map +1 -0
  13. package/dist/components/external-link-button.d.ts +7 -0
  14. package/dist/components/external-link-button.d.ts.map +1 -0
  15. package/dist/components/external-link-button.js +10 -0
  16. package/dist/components/external-link-button.js.map +1 -0
  17. package/dist/components/mailto-button.d.ts +7 -0
  18. package/dist/components/mailto-button.d.ts.map +1 -0
  19. package/dist/components/mailto-button.js +10 -0
  20. package/dist/components/mailto-button.js.map +1 -0
  21. package/dist/components/table-pagination.d.ts +21 -0
  22. package/dist/components/table-pagination.d.ts.map +1 -0
  23. package/dist/components/table-pagination.js +15 -0
  24. package/dist/components/table-pagination.js.map +1 -0
  25. package/dist/components/tooltip-icon-button.d.ts +24 -0
  26. package/dist/components/tooltip-icon-button.d.ts.map +1 -0
  27. package/dist/components/tooltip-icon-button.js +28 -0
  28. package/dist/components/tooltip-icon-button.js.map +1 -0
  29. package/dist/components/translate-button.d.ts +28 -0
  30. package/dist/components/translate-button.d.ts.map +1 -0
  31. package/dist/components/translate-button.js +24 -0
  32. package/dist/components/translate-button.js.map +1 -0
  33. package/dist/components/translate-dialog.d.ts +28 -0
  34. package/dist/components/translate-dialog.d.ts.map +1 -0
  35. package/dist/components/translate-dialog.js +66 -0
  36. package/dist/components/translate-dialog.js.map +1 -0
  37. package/dist/index.d.ts +12 -0
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +10 -0
  40. package/dist/index.js.map +1 -1
  41. package/dist/tsconfig.build.tsbuildinfo +1 -1
  42. package/package.json +1 -1
  43. package/src/components/__tests__/action-icon-button.test.tsx +62 -0
  44. package/src/components/__tests__/copy-to-clipboard-button.test.tsx +48 -0
  45. package/src/components/__tests__/external-link-button.test.tsx +28 -0
  46. package/src/components/__tests__/mailto-button.test.tsx +30 -0
  47. package/src/components/__tests__/table-pagination.test.tsx +115 -0
  48. package/src/components/__tests__/tooltip-icon-button.test.tsx +94 -0
  49. package/src/components/__tests__/translate-button.test.tsx +98 -0
  50. package/src/components/__tests__/translate-dialog.test.tsx +107 -0
  51. package/src/components/action-icon-button.tsx +40 -0
  52. package/src/components/combobox.tsx +1 -1
  53. package/src/components/copy-to-clipboard-button.tsx +28 -0
  54. package/src/components/external-link-button.tsx +22 -0
  55. package/src/components/mailto-button.tsx +22 -0
  56. package/src/components/table-pagination.tsx +88 -0
  57. package/src/components/tooltip-icon-button.tsx +83 -0
  58. package/src/components/translate-button.tsx +82 -0
  59. package/src/components/translate-dialog.tsx +148 -0
  60. package/src/index.ts +24 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-elements/ui",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Shared UI component library for Open Elements applications",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -0,0 +1,62 @@
1
+ import { describe, it, expect, afterEach, vi } from "vitest";
2
+ import { cleanup, fireEvent, render, screen } from "@testing-library/react";
3
+ import { ActionIconButton } from "../action-icon-button.tsx";
4
+
5
+ afterEach(() => {
6
+ cleanup();
7
+ });
8
+
9
+ describe("ActionIconButton", () => {
10
+ it("renders children inside the button", () => {
11
+ render(
12
+ <ActionIconButton onClick={() => {}}>
13
+ <svg data-testid="icon" />
14
+ </ActionIconButton>,
15
+ );
16
+ expect(screen.getByTestId("icon")).toBeInTheDocument();
17
+ });
18
+
19
+ it("calls onClick when clicked", () => {
20
+ const handleClick = vi.fn();
21
+ render(
22
+ <ActionIconButton onClick={handleClick}>
23
+ <svg />
24
+ </ActionIconButton>,
25
+ );
26
+ fireEvent.click(screen.getByRole("button"));
27
+ expect(handleClick).toHaveBeenCalledTimes(1);
28
+ });
29
+
30
+ it("stops propagation so parent click handlers do not fire", () => {
31
+ const parentHandler = vi.fn();
32
+ render(
33
+ <div onClick={parentHandler}>
34
+ <ActionIconButton onClick={() => {}}>
35
+ <svg />
36
+ </ActionIconButton>
37
+ </div>,
38
+ );
39
+ fireEvent.click(screen.getByRole("button"));
40
+ expect(parentHandler).not.toHaveBeenCalled();
41
+ });
42
+
43
+ it("sets title and aria-label when title is provided", () => {
44
+ render(
45
+ <ActionIconButton onClick={() => {}} title="Edit">
46
+ <svg />
47
+ </ActionIconButton>,
48
+ );
49
+ const button = screen.getByRole("button");
50
+ expect(button).toHaveAttribute("title", "Edit");
51
+ expect(button).toHaveAttribute("aria-label", "Edit");
52
+ });
53
+
54
+ it("applies success tone class when tone is 'success'", () => {
55
+ render(
56
+ <ActionIconButton onClick={() => {}} tone="success">
57
+ <svg />
58
+ </ActionIconButton>,
59
+ );
60
+ expect(screen.getByRole("button").className).toContain("text-primary");
61
+ });
62
+ });
@@ -0,0 +1,48 @@
1
+ import { describe, it, expect, afterEach, beforeEach, vi } from "vitest";
2
+ import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
3
+ import { CopyToClipboardButton } from "../copy-to-clipboard-button.tsx";
4
+
5
+ const writeText = vi.fn();
6
+ Object.assign(navigator, { clipboard: { writeText } });
7
+
8
+ beforeEach(() => {
9
+ vi.useFakeTimers();
10
+ writeText.mockClear();
11
+ });
12
+
13
+ afterEach(() => {
14
+ vi.useRealTimers();
15
+ cleanup();
16
+ });
17
+
18
+ describe("CopyToClipboardButton", () => {
19
+ it("renders the copy icon initially", () => {
20
+ const { container } = render(<CopyToClipboardButton value="hello" />);
21
+ expect(container.querySelector("svg.lucide-copy")).toBeInTheDocument();
22
+ expect(container.querySelector("svg.lucide-check")).toBeNull();
23
+ });
24
+
25
+ it("writes the value to the clipboard when clicked", () => {
26
+ render(<CopyToClipboardButton value="hello@example.com" />);
27
+ fireEvent.click(screen.getByRole("button"));
28
+ expect(writeText).toHaveBeenCalledWith("hello@example.com");
29
+ });
30
+
31
+ it("swaps to the check icon after clicking", () => {
32
+ const { container } = render(<CopyToClipboardButton value="x" />);
33
+ fireEvent.click(screen.getByRole("button"));
34
+ expect(container.querySelector("svg.lucide-check")).toBeInTheDocument();
35
+ expect(container.querySelector("svg.lucide-copy")).toBeNull();
36
+ });
37
+
38
+ it("reverts to the copy icon after the timeout", () => {
39
+ const { container } = render(<CopyToClipboardButton value="x" />);
40
+ fireEvent.click(screen.getByRole("button"));
41
+ expect(container.querySelector("svg.lucide-check")).toBeInTheDocument();
42
+ act(() => {
43
+ vi.advanceTimersByTime(2000);
44
+ });
45
+ expect(container.querySelector("svg.lucide-copy")).toBeInTheDocument();
46
+ expect(container.querySelector("svg.lucide-check")).toBeNull();
47
+ });
48
+ });
@@ -0,0 +1,28 @@
1
+ import { describe, it, expect, afterEach, beforeEach, vi } from "vitest";
2
+ import { cleanup, fireEvent, render, screen } from "@testing-library/react";
3
+ import { ExternalLinkButton } from "../external-link-button.tsx";
4
+
5
+ const openSpy = vi.fn();
6
+
7
+ beforeEach(() => {
8
+ openSpy.mockClear();
9
+ vi.spyOn(window, "open").mockImplementation(openSpy);
10
+ });
11
+
12
+ afterEach(() => {
13
+ vi.restoreAllMocks();
14
+ cleanup();
15
+ });
16
+
17
+ describe("ExternalLinkButton", () => {
18
+ it("renders the external-link icon", () => {
19
+ const { container } = render(<ExternalLinkButton href="https://example.com" />);
20
+ expect(container.querySelector("svg.lucide-external-link")).toBeInTheDocument();
21
+ });
22
+
23
+ it("opens the URL in a new tab with noopener,noreferrer on click", () => {
24
+ render(<ExternalLinkButton href="https://example.com" />);
25
+ fireEvent.click(screen.getByRole("button"));
26
+ expect(openSpy).toHaveBeenCalledWith("https://example.com", "_blank", "noopener,noreferrer");
27
+ });
28
+ });
@@ -0,0 +1,30 @@
1
+ import { describe, it, expect, afterEach, beforeEach } from "vitest";
2
+ import { cleanup, fireEvent, render, screen } from "@testing-library/react";
3
+ import { MailtoButton } from "../mailto-button.tsx";
4
+
5
+ const originalLocation = window.location;
6
+
7
+ beforeEach(() => {
8
+ Object.defineProperty(window, "location", {
9
+ writable: true,
10
+ value: { ...originalLocation, href: "" },
11
+ });
12
+ });
13
+
14
+ afterEach(() => {
15
+ Object.defineProperty(window, "location", { writable: true, value: originalLocation });
16
+ cleanup();
17
+ });
18
+
19
+ describe("MailtoButton", () => {
20
+ it("renders the mail icon", () => {
21
+ const { container } = render(<MailtoButton email="x@y.z" />);
22
+ expect(container.querySelector("svg.lucide-mail")).toBeInTheDocument();
23
+ });
24
+
25
+ it("navigates to the mailto: URL on click", () => {
26
+ render(<MailtoButton email="user@example.com" />);
27
+ fireEvent.click(screen.getByRole("button"));
28
+ expect(window.location.href).toBe("mailto:user@example.com");
29
+ });
30
+ });
@@ -0,0 +1,115 @@
1
+ import { describe, it, expect, afterEach, beforeEach, vi } from "vitest";
2
+ import { cleanup, fireEvent, render, screen } from "@testing-library/react";
3
+ import { TablePagination, type PaginationTranslations } from "../table-pagination.tsx";
4
+
5
+ const translations: PaginationTranslations = {
6
+ perPage: "per page",
7
+ previous: "Previous",
8
+ next: "Next",
9
+ totalOne: "{count} entry",
10
+ totalOther: "{count} entries",
11
+ };
12
+
13
+ const localStorageMock = (() => {
14
+ let store: Record<string, string> = {};
15
+ return {
16
+ getItem: vi.fn((key: string) => store[key] ?? null),
17
+ setItem: vi.fn((key: string, value: string) => {
18
+ store[key] = value;
19
+ }),
20
+ removeItem: vi.fn((key: string) => {
21
+ delete store[key];
22
+ }),
23
+ clear: vi.fn(() => {
24
+ store = {};
25
+ }),
26
+ };
27
+ })();
28
+ Object.defineProperty(globalThis, "localStorage", { value: localStorageMock });
29
+
30
+ beforeEach(() => {
31
+ localStorageMock.clear();
32
+ localStorageMock.setItem.mockClear();
33
+ });
34
+
35
+ afterEach(() => {
36
+ cleanup();
37
+ });
38
+
39
+ const defaultProps = {
40
+ page: 0,
41
+ pageSize: 20,
42
+ pageSizeOptions: [10, 20, 50, 100, 200] as const,
43
+ storageKey: "pageSize.test",
44
+ translations,
45
+ onPageChange: vi.fn(),
46
+ onPageSizeChange: vi.fn(),
47
+ };
48
+
49
+ describe("TablePagination", () => {
50
+ it("renders the singular total label when only one entry", () => {
51
+ render(<TablePagination {...defaultProps} totalElements={1} totalPages={1} />);
52
+ expect(screen.getByText(/· 1 entry/)).toBeInTheDocument();
53
+ });
54
+
55
+ it("renders the plural total label when more than one entry", () => {
56
+ render(<TablePagination {...defaultProps} totalElements={35} totalPages={2} />);
57
+ expect(screen.getByText(/· 35 entries/)).toBeInTheDocument();
58
+ });
59
+
60
+ it("hides previous/next buttons when totalPages <= 1", () => {
61
+ render(<TablePagination {...defaultProps} totalElements={5} totalPages={1} />);
62
+ expect(screen.queryByText("Previous")).toBeNull();
63
+ expect(screen.queryByText("Next")).toBeNull();
64
+ });
65
+
66
+ it("keeps the page-size selector visible when totalPages <= 1", () => {
67
+ render(<TablePagination {...defaultProps} totalElements={5} totalPages={1} />);
68
+ expect(screen.getByText("per page")).toBeInTheDocument();
69
+ });
70
+
71
+ it("disables previous on the first page and enables next", () => {
72
+ render(<TablePagination {...defaultProps} totalElements={35} totalPages={2} />);
73
+ expect(screen.getByText("Previous").closest("button")).toBeDisabled();
74
+ expect(screen.getByText("Next").closest("button")).not.toBeDisabled();
75
+ });
76
+
77
+ it("disables next on the last page", () => {
78
+ render(<TablePagination {...defaultProps} page={1} totalElements={35} totalPages={2} />);
79
+ expect(screen.getByText("Next").closest("button")).toBeDisabled();
80
+ });
81
+
82
+ it("calls onPageChange with page+1 when next is clicked", () => {
83
+ const onPageChange = vi.fn();
84
+ render(
85
+ <TablePagination
86
+ {...defaultProps}
87
+ onPageChange={onPageChange}
88
+ totalElements={35}
89
+ totalPages={2}
90
+ />,
91
+ );
92
+ fireEvent.click(screen.getByText("Next"));
93
+ expect(onPageChange).toHaveBeenCalledWith(1);
94
+ });
95
+
96
+ it("calls onPageChange with page-1 when previous is clicked", () => {
97
+ const onPageChange = vi.fn();
98
+ render(
99
+ <TablePagination
100
+ {...defaultProps}
101
+ page={1}
102
+ onPageChange={onPageChange}
103
+ totalElements={35}
104
+ totalPages={2}
105
+ />,
106
+ );
107
+ fireEvent.click(screen.getByText("Previous"));
108
+ expect(onPageChange).toHaveBeenCalledWith(0);
109
+ });
110
+
111
+ it("sets aria-label on the size selector", () => {
112
+ render(<TablePagination {...defaultProps} totalElements={5} totalPages={1} />);
113
+ expect(screen.getByLabelText("per page")).toBeInTheDocument();
114
+ });
115
+ });
@@ -0,0 +1,94 @@
1
+ import { describe, it, expect, afterEach, vi } from "vitest";
2
+ import { cleanup, fireEvent, render, screen } from "@testing-library/react";
3
+ import { Pencil, Trash2 } from "lucide-react";
4
+ import { TooltipIconButton } from "../tooltip-icon-button.tsx";
5
+ import { TooltipProvider } from "../tooltip.tsx";
6
+
7
+ function renderWithTooltip(ui: React.ReactElement) {
8
+ return render(<TooltipProvider>{ui}</TooltipProvider>);
9
+ }
10
+
11
+ afterEach(() => {
12
+ cleanup();
13
+ });
14
+
15
+ describe("TooltipIconButton", () => {
16
+ it("renders the icon", () => {
17
+ const { container } = renderWithTooltip(
18
+ <TooltipIconButton icon={<Pencil />} tooltip="Edit" onClick={() => {}} />,
19
+ );
20
+ expect(container.querySelector("svg.lucide-pencil")).toBeInTheDocument();
21
+ });
22
+
23
+ it("uses the tooltip string as the button's accessible name", () => {
24
+ renderWithTooltip(
25
+ <TooltipIconButton icon={<Pencil />} tooltip="Edit user" onClick={() => {}} />,
26
+ );
27
+ expect(screen.getByRole("button", { name: "Edit user" })).toBeInTheDocument();
28
+ });
29
+
30
+ it("calls onClick when clicked", () => {
31
+ const handleClick = vi.fn();
32
+ renderWithTooltip(<TooltipIconButton icon={<Pencil />} tooltip="Edit" onClick={handleClick} />);
33
+ fireEvent.click(screen.getByRole("button"));
34
+ expect(handleClick).toHaveBeenCalledTimes(1);
35
+ });
36
+
37
+ it("stops click propagation so parent handlers do not fire", () => {
38
+ const parentHandler = vi.fn();
39
+ renderWithTooltip(
40
+ <div onClick={parentHandler}>
41
+ <TooltipIconButton icon={<Pencil />} tooltip="Edit" onClick={() => {}} />
42
+ </div>,
43
+ );
44
+ fireEvent.click(screen.getByRole("button"));
45
+ expect(parentHandler).not.toHaveBeenCalled();
46
+ });
47
+
48
+ it("applies the destructive tone class", () => {
49
+ renderWithTooltip(
50
+ <TooltipIconButton
51
+ icon={<Trash2 />}
52
+ tooltip="Delete"
53
+ tone="destructive"
54
+ onClick={() => {}}
55
+ />,
56
+ );
57
+ expect(screen.getByRole("button").className).toContain("text-destructive");
58
+ });
59
+
60
+ it("applies the default (primary) tone class when tone is omitted", () => {
61
+ renderWithTooltip(<TooltipIconButton icon={<Pencil />} tooltip="Edit" onClick={() => {}} />);
62
+ expect(screen.getByRole("button").className).toContain("text-primary");
63
+ });
64
+
65
+ it("disables the button when disabled is true and does not fire onClick", () => {
66
+ const handleClick = vi.fn();
67
+ renderWithTooltip(
68
+ <TooltipIconButton
69
+ icon={<Trash2 />}
70
+ tooltip="Cannot delete"
71
+ disabled
72
+ onClick={handleClick}
73
+ />,
74
+ );
75
+ const button = screen.getByRole("button");
76
+ expect(button).toBeDisabled();
77
+ fireEvent.click(button);
78
+ expect(handleClick).not.toHaveBeenCalled();
79
+ });
80
+
81
+ it("renders the asChild element instead of a button when asChild is set", () => {
82
+ const { container } = renderWithTooltip(
83
+ <TooltipIconButton tooltip="Edit tag" asChild>
84
+ <a href="/tags/123/edit">
85
+ <Pencil />
86
+ </a>
87
+ </TooltipIconButton>,
88
+ );
89
+ const anchor = container.querySelector("a");
90
+ expect(anchor).toHaveAttribute("href", "/tags/123/edit");
91
+ expect(anchor).toHaveAttribute("aria-label", "Edit tag");
92
+ expect(anchor?.querySelector("svg.lucide-pencil")).toBeInTheDocument();
93
+ });
94
+ });
@@ -0,0 +1,98 @@
1
+ import { describe, it, expect, afterEach, vi } from "vitest";
2
+ import { render, screen, cleanup, fireEvent } from "@testing-library/react";
3
+ import { TranslateButton } from "../translate-button.tsx";
4
+ import { LanguageProvider } from "../../i18n/language-context.tsx";
5
+ import { TooltipProvider } from "../tooltip.tsx";
6
+
7
+ afterEach(() => {
8
+ cleanup();
9
+ vi.restoreAllMocks();
10
+ });
11
+
12
+ const T = {
13
+ button: "Translate",
14
+ dialog: {
15
+ title: "Translation",
16
+ loading: "Translating…",
17
+ error: "Translation failed",
18
+ copy: "Copy",
19
+ copied: "Copied!",
20
+ close: "Close",
21
+ },
22
+ } as const;
23
+
24
+ const dummyTranslations = { de: {}, en: {} } as const;
25
+
26
+ function renderButton(props: {
27
+ readonly text: string | null | undefined;
28
+ readonly configured: boolean | null;
29
+ readonly size?: "sm" | "md";
30
+ }) {
31
+ const onTranslate = vi.fn().mockResolvedValue({ translatedText: "Hello" });
32
+ const utils = render(
33
+ <LanguageProvider translations={dummyTranslations} defaultLanguage="en">
34
+ <TooltipProvider>
35
+ <TranslateButton
36
+ text={props.text}
37
+ size={props.size}
38
+ configured={props.configured}
39
+ onTranslate={onTranslate}
40
+ translations={T}
41
+ />
42
+ </TooltipProvider>
43
+ </LanguageProvider>,
44
+ );
45
+ return { ...utils, onTranslate };
46
+ }
47
+
48
+ describe("TranslateButton", () => {
49
+ it("renders null when text is empty", () => {
50
+ renderButton({ text: "", configured: true });
51
+ expect(screen.queryByTestId("translate-button")).toBeNull();
52
+ });
53
+
54
+ it("renders null when text is whitespace", () => {
55
+ renderButton({ text: " ", configured: true });
56
+ expect(screen.queryByTestId("translate-button")).toBeNull();
57
+ });
58
+
59
+ it("renders null when text is null", () => {
60
+ renderButton({ text: null, configured: true });
61
+ expect(screen.queryByTestId("translate-button")).toBeNull();
62
+ });
63
+
64
+ it("renders null while configured is still null (probe in flight)", () => {
65
+ renderButton({ text: "something", configured: null });
66
+ expect(screen.queryByTestId("translate-button")).toBeNull();
67
+ });
68
+
69
+ it("renders null when configured is false", () => {
70
+ renderButton({ text: "something", configured: false });
71
+ expect(screen.queryByTestId("translate-button")).toBeNull();
72
+ });
73
+
74
+ it("renders the button when text is non-empty and configured is true", () => {
75
+ renderButton({ text: "something", configured: true });
76
+ const button = screen.getByTestId("translate-button");
77
+ expect(button).toBeInTheDocument();
78
+ expect(button).toHaveAttribute("aria-label", T.button);
79
+ });
80
+
81
+ it("uses size='sm' classes when size is 'sm'", () => {
82
+ renderButton({ text: "x", configured: true, size: "sm" });
83
+ const icon = screen.getByTestId("translate-button").querySelector("svg");
84
+ expect(icon).toHaveClass("h-3.5", "w-3.5");
85
+ });
86
+
87
+ it("uses size='md' (default) classes when size is omitted", () => {
88
+ renderButton({ text: "x", configured: true });
89
+ const icon = screen.getByTestId("translate-button").querySelector("svg");
90
+ expect(icon).toHaveClass("h-4", "w-4");
91
+ });
92
+
93
+ it("opens the dialog on click with sourceText equal to the button text", () => {
94
+ const { onTranslate } = renderButton({ text: "Hallo", configured: true });
95
+ fireEvent.click(screen.getByTestId("translate-button"));
96
+ expect(onTranslate).toHaveBeenCalledWith("Hallo", "en");
97
+ });
98
+ });
@@ -0,0 +1,107 @@
1
+ import { describe, it, expect, afterEach, vi } from "vitest";
2
+ import { render, screen, cleanup, waitFor, fireEvent, act } from "@testing-library/react";
3
+ import { TranslateDialog, type TranslateResult } from "../translate-dialog.tsx";
4
+ import { LanguageProvider } from "../../i18n/language-context.tsx";
5
+
6
+ afterEach(() => {
7
+ cleanup();
8
+ vi.restoreAllMocks();
9
+ });
10
+
11
+ const T = {
12
+ title: "Translation",
13
+ loading: "Translating…",
14
+ error: "Translation failed",
15
+ copy: "Copy",
16
+ copied: "Copied!",
17
+ close: "Close",
18
+ } as const;
19
+
20
+ const dummyTranslations = { de: {}, en: {} } as const;
21
+
22
+ function renderDialog(props: {
23
+ readonly open: boolean;
24
+ readonly sourceText?: string;
25
+ readonly onTranslate?: (text: string, lang: string) => Promise<TranslateResult>;
26
+ readonly onOpenChange?: (open: boolean) => void;
27
+ readonly language?: "de" | "en";
28
+ }) {
29
+ const onTranslate = props.onTranslate ?? vi.fn().mockResolvedValue({ translatedText: "Hello" });
30
+ const onOpenChange = props.onOpenChange ?? vi.fn();
31
+ const utils = render(
32
+ <LanguageProvider translations={dummyTranslations} defaultLanguage={props.language ?? "en"}>
33
+ <TranslateDialog
34
+ open={props.open}
35
+ onOpenChange={onOpenChange}
36
+ sourceText={props.sourceText ?? "Hallo"}
37
+ onTranslate={onTranslate}
38
+ translations={T}
39
+ />
40
+ </LanguageProvider>,
41
+ );
42
+ return { ...utils, onTranslate, onOpenChange };
43
+ }
44
+
45
+ describe("TranslateDialog", () => {
46
+ it("calls onTranslate once with the current language and renders the result", async () => {
47
+ const onTranslate = vi.fn().mockResolvedValue({ translatedText: "Hello world" });
48
+ renderDialog({ open: true, sourceText: "Hallo Welt", onTranslate, language: "en" });
49
+ expect(onTranslate).toHaveBeenCalledTimes(1);
50
+ expect(onTranslate).toHaveBeenCalledWith("Hallo Welt", "en");
51
+ expect(screen.getByTestId("translate-dialog-loading")).toBeInTheDocument();
52
+ await waitFor(() => {
53
+ expect(screen.getByTestId("translate-dialog-result")).toHaveTextContent("Hello world");
54
+ });
55
+ });
56
+
57
+ it("uses 'de' as target when the language provider is set to de", () => {
58
+ const onTranslate = vi.fn().mockResolvedValue({ translatedText: "x" });
59
+ renderDialog({ open: true, onTranslate, language: "de" });
60
+ expect(onTranslate).toHaveBeenCalledWith("Hallo", "de");
61
+ });
62
+
63
+ it("uses 'en' as target for any non-de language", () => {
64
+ const onTranslate = vi.fn().mockResolvedValue({ translatedText: "x" });
65
+ renderDialog({ open: true, onTranslate, language: "en" });
66
+ expect(onTranslate).toHaveBeenCalledWith("Hallo", "en");
67
+ });
68
+
69
+ it("renders the error message when onTranslate rejects", async () => {
70
+ const onTranslate = vi.fn().mockRejectedValue(new Error("boom"));
71
+ renderDialog({ open: true, onTranslate });
72
+ await waitFor(() => {
73
+ expect(screen.getByTestId("translate-dialog-error")).toHaveTextContent(T.error);
74
+ });
75
+ expect(screen.getByTestId("translate-dialog-copy")).toBeDisabled();
76
+ });
77
+
78
+ it("does not call onTranslate when open is false", () => {
79
+ const onTranslate = vi.fn();
80
+ renderDialog({ open: false, onTranslate });
81
+ expect(onTranslate).not.toHaveBeenCalled();
82
+ expect(screen.queryByTestId("translate-dialog-loading")).toBeNull();
83
+ });
84
+
85
+ it("copies to clipboard and flips the label to 'copied'", async () => {
86
+ const writeText = vi.fn().mockResolvedValue(undefined);
87
+ Object.defineProperty(navigator, "clipboard", {
88
+ configurable: true,
89
+ value: { writeText },
90
+ });
91
+ const onTranslate = vi.fn().mockResolvedValue({ translatedText: "Hello" });
92
+ renderDialog({ open: true, onTranslate });
93
+
94
+ await waitFor(() => {
95
+ expect(screen.getByTestId("translate-dialog-result")).toBeInTheDocument();
96
+ });
97
+
98
+ const copyBtn = screen.getByTestId("translate-dialog-copy");
99
+ await act(async () => {
100
+ fireEvent.click(copyBtn);
101
+ });
102
+ expect(writeText).toHaveBeenCalledWith("Hello");
103
+ await waitFor(() => {
104
+ expect(copyBtn).toHaveTextContent(T.copied);
105
+ });
106
+ });
107
+ });
@@ -0,0 +1,40 @@
1
+ "use client";
2
+
3
+ import type { MouseEvent, ReactNode } from "react";
4
+ import { cn } from "../lib/utils.ts";
5
+
6
+ type Tone = "default" | "success";
7
+
8
+ type ActionIconButtonProps = {
9
+ readonly onClick: (event: MouseEvent<HTMLButtonElement>) => void;
10
+ readonly children: ReactNode;
11
+ readonly title?: string;
12
+ readonly tone?: Tone;
13
+ };
14
+
15
+ const TONE_CLASS: Record<Tone, string> = {
16
+ default: "text-muted-foreground hover:text-foreground [@media(pointer:coarse)]:text-foreground",
17
+ success: "text-primary",
18
+ };
19
+
20
+ export function ActionIconButton({
21
+ onClick,
22
+ children,
23
+ title,
24
+ tone = "default",
25
+ }: ActionIconButtonProps) {
26
+ return (
27
+ <button
28
+ type="button"
29
+ title={title}
30
+ aria-label={title}
31
+ onClick={(event) => {
32
+ event.stopPropagation();
33
+ onClick(event);
34
+ }}
35
+ className={cn("transition-colors [&_svg]:h-3.5 [&_svg]:w-3.5", TONE_CLASS[tone])}
36
+ >
37
+ {children}
38
+ </button>
39
+ );
40
+ }
@@ -243,7 +243,7 @@ function ComboboxChip({
243
243
  );
244
244
  }
245
245
 
246
- function ComboboxChipsInput({ className, children, ...props }: ComboboxPrimitive.Input.Props) {
246
+ function ComboboxChipsInput({ className, ...props }: ComboboxPrimitive.Input.Props) {
247
247
  return (
248
248
  <ComboboxPrimitive.Input
249
249
  data-slot="combobox-chip-input"
@@ -0,0 +1,28 @@
1
+ "use client";
2
+
3
+ import { useState } from "react";
4
+ import { Check, Copy } from "lucide-react";
5
+ import { ActionIconButton } from "./action-icon-button.tsx";
6
+
7
+ type CopyToClipboardButtonProps = {
8
+ readonly value: string;
9
+ readonly title?: string;
10
+ };
11
+
12
+ const COPIED_RESET_MS = 2000;
13
+
14
+ export function CopyToClipboardButton({ value, title }: CopyToClipboardButtonProps) {
15
+ const [copied, setCopied] = useState(false);
16
+
17
+ const handleClick = () => {
18
+ navigator.clipboard.writeText(value);
19
+ setCopied(true);
20
+ setTimeout(() => setCopied(false), COPIED_RESET_MS);
21
+ };
22
+
23
+ return (
24
+ <ActionIconButton onClick={handleClick} title={title} tone={copied ? "success" : "default"}>
25
+ {copied ? <Check /> : <Copy />}
26
+ </ActionIconButton>
27
+ );
28
+ }