@truedat/df 8.2.3 → 8.3.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.
@@ -0,0 +1,94 @@
1
+ import React from "react";
2
+ import userEvent from "@testing-library/user-event";
3
+ import { waitFor } from "@testing-library/react";
4
+ import { render, waitForLoad } from "@truedat/test/render";
5
+ import TemplateRelationsForm from "../TemplateRelationsForm";
6
+
7
+ jest.mock("@truedat/dd/hooks/useSystems", () => ({
8
+ useGrantableSystems: jest.fn(() => ({
9
+ data: [
10
+ { id: 1, name: "System One" },
11
+ { id: 2, name: "System Two" },
12
+ ],
13
+ })),
14
+ }));
15
+
16
+ const mockStableResource = [];
17
+ jest.mock("@truedat/df/hooks/useTemplateRelations", () => ({
18
+ useTemplateRelations: jest.fn(() => ({
19
+ default: { template_id: 999 },
20
+ resource: mockStableResource,
21
+ error: null,
22
+ loading: false,
23
+ mutate: jest.fn(),
24
+ })),
25
+ }));
26
+
27
+ describe("<TemplateRelationsForm />", () => {
28
+ it("matches snapshot when scope is gr", async () => {
29
+ const rendered = render(<TemplateRelationsForm scope="gr" />);
30
+ await waitForLoad(rendered);
31
+ expect(rendered.container).toMatchSnapshot();
32
+ });
33
+
34
+ it("calls onRelationsChange with empty array when scope is gr and no default or systems selected", async () => {
35
+ const onRelationsChange = jest.fn();
36
+ const rendered = render(
37
+ <TemplateRelationsForm scope="gr" onRelationsChange={onRelationsChange} />
38
+ );
39
+ await waitForLoad(rendered);
40
+
41
+ await waitFor(() => {
42
+ expect(onRelationsChange).toHaveBeenCalledWith([]);
43
+ });
44
+ });
45
+
46
+ it("calls onRelationsChange with default relation when use as default is checked", async () => {
47
+ const onRelationsChange = jest.fn();
48
+ const rendered = render(
49
+ <TemplateRelationsForm scope="gr" onRelationsChange={onRelationsChange} />
50
+ );
51
+ await waitForLoad(rendered);
52
+
53
+ const user = userEvent.setup({ delay: null });
54
+ await user.click(rendered.getByLabelText(/template\.relations\.default/i));
55
+
56
+ await waitFor(() => {
57
+ expect(onRelationsChange).toHaveBeenCalledWith([
58
+ { resource_type: "system", resource_id: null },
59
+ ]);
60
+ });
61
+ });
62
+
63
+ it("renders related systems dropdown with placeholder when scope is gr", async () => {
64
+ const rendered = render(<TemplateRelationsForm scope="gr" />);
65
+ await waitForLoad(rendered);
66
+
67
+ expect(rendered.getByText(/template\.relations\.related/i)).toBeInTheDocument();
68
+ expect(rendered.getByText(/template\.relations\.systems\.placeholder/i)).toBeInTheDocument();
69
+ });
70
+
71
+ it("does not call onRelationsChange when scope has no resource type mapping", async () => {
72
+ const onRelationsChange = jest.fn();
73
+ const rendered = render(
74
+ <TemplateRelationsForm scope="dd" onRelationsChange={onRelationsChange} />
75
+ );
76
+ await waitForLoad(rendered);
77
+
78
+ await waitFor(() => {
79
+ expect(onRelationsChange).not.toHaveBeenCalled();
80
+ });
81
+ });
82
+
83
+ it("does not throw when onRelationsChange is not provided", async () => {
84
+ const rendered = render(<TemplateRelationsForm scope="gr" />);
85
+ await waitForLoad(rendered);
86
+
87
+ const user = userEvent.setup({ delay: null });
88
+ await user.click(rendered.getByLabelText(/template\.relations\.default/i));
89
+
90
+ await waitFor(() => {
91
+ expect(rendered.getByText(/template\.relations\.header/i)).toBeInTheDocument();
92
+ });
93
+ });
94
+ });