@truedat/df 8.13.5 → 8.13.7
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.
- package/package.json +4 -4
- package/src/components/DynamicForm.js +3 -0
- package/src/components/DynamicFormWithTranslations.js +3 -0
- package/src/components/FieldGroupSegment.js +3 -0
- package/src/components/FieldGroupWithTranslations.js +13 -0
- package/src/components/SelectDynamicFormWithTranslations.js +7 -1
- package/src/components/SelectableDynamicForm.js +7 -1
- package/src/components/__tests__/__snapshots__/SelectableDynamicForm.spec.js.snap +18 -1
- package/src/components/__tests__/originalContentFlow.spec.js +131 -0
- package/src/components/widgets/DateField.js +18 -9
- package/src/components/widgets/DateTimeField.js +26 -9
- package/src/components/widgets/DynamicField.js +54 -1
- package/src/components/widgets/DynamicTableField.js +41 -7
- package/src/components/widgets/FieldByWidget.js +36 -6
- package/src/components/widgets/__tests__/dateBoundsByWidget.spec.js +133 -0
- package/src/components/widgets/__tests__/dateBoundsEndToEnd.spec.js +83 -0
- package/src/components/widgets/__tests__/datePickerBoundaries.spec.js +124 -0
- package/src/components/widgets/__tests__/dateValueValidation.spec.js +215 -0
- package/src/components/widgets/__tests__/existingDateRestrictions.spec.js +354 -0
- package/src/templates/components/templateForm/DateRestrictionsForm.js +217 -0
- package/src/templates/components/templateForm/DefaultValue.js +79 -11
- package/src/templates/components/templateForm/FieldDefinition.js +15 -1
- package/src/templates/components/templateForm/TemplateForm.js +5 -0
- package/src/templates/components/templateForm/ValuesField.js +7 -0
- package/src/templates/components/templateForm/__tests__/FieldDefinition.spec.js +48 -0
- package/src/templates/components/templateForm/__tests__/TemplateForm.spec.js +107 -0
- package/src/templates/components/templateForm/__tests__/dateRestrictionsForm.spec.js +429 -0
- package/src/templates/components/templateForm/__tests__/dateRestrictionsValidation.spec.js +166 -0
- package/src/templates/components/templateForm/__tests__/defaultValueBounds.spec.js +191 -0
- package/src/templates/components/templateForm/__tests__/defaultValueRestrictions.spec.js +175 -0
- package/src/templates/components/templateForm/contentValidation.js +184 -0
- package/src/templates/utils/__tests__/dateRestrictions.spec.js +578 -0
- package/src/templates/utils/__tests__/originalTableRows.spec.js +129 -0
- package/src/templates/utils/__tests__/validateContentDateRestrictions.spec.js +519 -0
- package/src/templates/utils/applyTemplate.js +27 -20
- package/src/templates/utils/dateRestrictions.js +497 -0
- package/src/templates/utils/dateValueUnchanged.js +12 -0
- package/src/templates/utils/originalTableRows.js +41 -0
- package/src/templates/utils/validateContent.js +75 -4
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
import { getValues } from "./valueDefinitions";
|
|
14
14
|
|
|
15
15
|
const defaultType = (values) => _.flow(_.map("value"), _.first)(values);
|
|
16
|
+
const isDateType = (type) => type === "date" || type === "datetime";
|
|
16
17
|
|
|
17
18
|
const FieldDefinition = ({
|
|
18
19
|
allowedTypes = [],
|
|
@@ -37,8 +38,18 @@ const FieldDefinition = ({
|
|
|
37
38
|
value: { [value]: null },
|
|
38
39
|
});
|
|
39
40
|
|
|
41
|
+
const clearRestrictionsForType = (e, nextType) => {
|
|
42
|
+
if (isDateType(type) && !isDateType(nextType)) {
|
|
43
|
+
onChange(e, {
|
|
44
|
+
name: `${fieldNamePrefix}.restrictions`,
|
|
45
|
+
value: null,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
40
50
|
const handleTypeChange = (e, data) => {
|
|
41
51
|
const { value } = data;
|
|
52
|
+
clearRestrictionsForType(e, value);
|
|
42
53
|
changeValue(valueName, defaultType(getValues(widget, value)) || "null");
|
|
43
54
|
onChange(e, {
|
|
44
55
|
name: defaultField,
|
|
@@ -59,6 +70,7 @@ const FieldDefinition = ({
|
|
|
59
70
|
const { value } = data;
|
|
60
71
|
const types = _.find({ value })(WIDGETS).types;
|
|
61
72
|
const updatedType = _.indexOf(type)(types) < 0 ? _.head(types) : type;
|
|
73
|
+
clearRestrictionsForType(e, updatedType);
|
|
62
74
|
changeValue(
|
|
63
75
|
valueName,
|
|
64
76
|
defaultType(getValues(value, updatedType)) || "null",
|
|
@@ -72,11 +84,13 @@ const FieldDefinition = ({
|
|
|
72
84
|
};
|
|
73
85
|
|
|
74
86
|
const typeCheckOnChange = (widgetOption) => {
|
|
75
|
-
if (widgetOption && _.indexOf(type)(widgetOption.types) < 0)
|
|
87
|
+
if (widgetOption && _.indexOf(type)(widgetOption.types) < 0) {
|
|
88
|
+
clearRestrictionsForType(null, widgetOption.types[0]);
|
|
76
89
|
onChange(null, {
|
|
77
90
|
name: `${fieldNamePrefix}.type`,
|
|
78
91
|
value: widgetOption.types[0],
|
|
79
92
|
});
|
|
93
|
+
}
|
|
80
94
|
};
|
|
81
95
|
|
|
82
96
|
const cardinalityCheckOnChange = (widgetOption) => {
|
|
@@ -98,6 +98,10 @@ export const TemplateForm = ({ loading, template, onSubmit }) => {
|
|
|
98
98
|
"searchable",
|
|
99
99
|
"ai_suggestion",
|
|
100
100
|
];
|
|
101
|
+
const restrictionProperties =
|
|
102
|
+
field?.type === "date" || field?.type === "datetime"
|
|
103
|
+
? ["restrictions"]
|
|
104
|
+
: [];
|
|
101
105
|
|
|
102
106
|
const dependsProperty = _.isEmpty(_.path("depends.on")(field))
|
|
103
107
|
? []
|
|
@@ -106,6 +110,7 @@ export const TemplateForm = ({ loading, template, onSubmit }) => {
|
|
|
106
110
|
? []
|
|
107
111
|
: ["mandatory"];
|
|
108
112
|
const allFields = _.flow(
|
|
113
|
+
_.concat(restrictionProperties),
|
|
109
114
|
_.concat(dependsProperty),
|
|
110
115
|
_.concat(mandatoryProperty),
|
|
111
116
|
)(fieldProperties);
|
|
@@ -5,6 +5,7 @@ import { Form, Segment } from "semantic-ui-react";
|
|
|
5
5
|
import { useIntl } from "react-intl";
|
|
6
6
|
import { hasAiSuggestions, valueSegment, getKeyType } from "./valueDefinitions";
|
|
7
7
|
import DefaultValue from "./DefaultValue";
|
|
8
|
+
import DateRestrictionsForm from "./DateRestrictionsForm";
|
|
8
9
|
import ValuesSelector from "./ValuesSelector";
|
|
9
10
|
import ValuesConfiguration from "./ValuesConfiguration";
|
|
10
11
|
|
|
@@ -100,6 +101,12 @@ export const ValuesField = ({
|
|
|
100
101
|
onChange={onChange}
|
|
101
102
|
type={keyType}
|
|
102
103
|
/>
|
|
104
|
+
<DateRestrictionsForm
|
|
105
|
+
field={field}
|
|
106
|
+
errors={field?.errors?.restrictions}
|
|
107
|
+
fieldNamePrefix={fieldNamePrefix}
|
|
108
|
+
onChange={onChange}
|
|
109
|
+
/>
|
|
103
110
|
</Segment>
|
|
104
111
|
);
|
|
105
112
|
};
|
|
@@ -153,6 +153,54 @@ describe("FieldDefinition", () => {
|
|
|
153
153
|
);
|
|
154
154
|
});
|
|
155
155
|
|
|
156
|
+
test("clears date restrictions when a widget change switches to a non-date type", async () => {
|
|
157
|
+
const onChange = jest.fn();
|
|
158
|
+
const rendered = render(
|
|
159
|
+
<FieldDefinition
|
|
160
|
+
{...baseProps}
|
|
161
|
+
widget="date"
|
|
162
|
+
type="date"
|
|
163
|
+
onChange={onChange}
|
|
164
|
+
/>,
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const widgetDropdown = rendered.getAllByRole("listbox")[0];
|
|
168
|
+
await user.click(
|
|
169
|
+
within(widgetDropdown).getByRole("option", { name: "Text Input" }),
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
expect(onChange).toHaveBeenNthCalledWith(1, expect.anything(), {
|
|
173
|
+
name: "field.restrictions",
|
|
174
|
+
value: null,
|
|
175
|
+
});
|
|
176
|
+
expect(onChange).toHaveBeenLastCalledWith(
|
|
177
|
+
expect.anything(),
|
|
178
|
+
expect.objectContaining({ name: "field.widget", value: "string" }),
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("preserves date restrictions when switching between date widgets", async () => {
|
|
183
|
+
const onChange = jest.fn();
|
|
184
|
+
const rendered = render(
|
|
185
|
+
<FieldDefinition
|
|
186
|
+
{...baseProps}
|
|
187
|
+
widget="datetime"
|
|
188
|
+
type="datetime"
|
|
189
|
+
onChange={onChange}
|
|
190
|
+
/>,
|
|
191
|
+
);
|
|
192
|
+
const widgetDropdown = rendered.getAllByRole("listbox")[0];
|
|
193
|
+
|
|
194
|
+
await user.click(
|
|
195
|
+
within(widgetDropdown).getByRole("option", { name: "Date Time" }),
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
expect(onChange).not.toHaveBeenCalledWith(null, {
|
|
199
|
+
name: "field.restrictions",
|
|
200
|
+
value: null,
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
156
204
|
test("useEffect: auto-fixes invalid type/cardinality for a widget (real WIDGETS)", () => {
|
|
157
205
|
const onChange = jest.fn();
|
|
158
206
|
|
|
@@ -122,6 +122,113 @@ describe("<TemplateForm />", () => {
|
|
|
122
122
|
]);
|
|
123
123
|
});
|
|
124
124
|
|
|
125
|
+
it("shows restriction errors and prevents saving an incomplete date range", async () => {
|
|
126
|
+
const onSubmit = jest.fn();
|
|
127
|
+
const invalidTemplate = {
|
|
128
|
+
...validTemplate,
|
|
129
|
+
content: [
|
|
130
|
+
{
|
|
131
|
+
name: "Group1",
|
|
132
|
+
fields: [
|
|
133
|
+
{
|
|
134
|
+
name: "field1",
|
|
135
|
+
label: "Field 1",
|
|
136
|
+
type: "date",
|
|
137
|
+
widget: "date",
|
|
138
|
+
cardinality: "?",
|
|
139
|
+
values: {},
|
|
140
|
+
restrictions: {
|
|
141
|
+
between: { mode: "static", start: "", end: "2026-12-31" },
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
};
|
|
148
|
+
const rendered = render(
|
|
149
|
+
<TemplateForm
|
|
150
|
+
template={invalidTemplate}
|
|
151
|
+
onSubmit={onSubmit}
|
|
152
|
+
loading={false}
|
|
153
|
+
/>,
|
|
154
|
+
{
|
|
155
|
+
state: {
|
|
156
|
+
templateDeleting: false,
|
|
157
|
+
templateSaving: false,
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
);
|
|
161
|
+
await waitForLoad(rendered);
|
|
162
|
+
|
|
163
|
+
expect(
|
|
164
|
+
rendered.getByText("template.field.restrictions.validation.required"),
|
|
165
|
+
).toBeInTheDocument();
|
|
166
|
+
expect(rendered.getByRole("button", { name: /save/i })).toBeDisabled();
|
|
167
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("keeps restrictions for date fields and excludes stale restrictions from other types on save", async () => {
|
|
171
|
+
const onSubmit = jest.fn();
|
|
172
|
+
const templateWithRestrictions = {
|
|
173
|
+
...validTemplate,
|
|
174
|
+
content: [
|
|
175
|
+
{
|
|
176
|
+
name: "Group1",
|
|
177
|
+
fields: [
|
|
178
|
+
{
|
|
179
|
+
name: "date_field",
|
|
180
|
+
label: "Date Field",
|
|
181
|
+
type: "date",
|
|
182
|
+
widget: "date",
|
|
183
|
+
cardinality: "?",
|
|
184
|
+
values: {},
|
|
185
|
+
restrictions: {
|
|
186
|
+
max_date: { mode: "static", value: "2026-12-31" },
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
name: "text_field",
|
|
191
|
+
label: "Text Field",
|
|
192
|
+
type: "string",
|
|
193
|
+
widget: "string",
|
|
194
|
+
cardinality: "?",
|
|
195
|
+
values: {},
|
|
196
|
+
restrictions: {
|
|
197
|
+
max_date: { mode: "static", value: "2026-12-31" },
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
};
|
|
204
|
+
const rendered = render(
|
|
205
|
+
<TemplateForm
|
|
206
|
+
template={templateWithRestrictions}
|
|
207
|
+
onSubmit={onSubmit}
|
|
208
|
+
loading={false}
|
|
209
|
+
/>,
|
|
210
|
+
{
|
|
211
|
+
state: {
|
|
212
|
+
templateDeleting: false,
|
|
213
|
+
templateSaving: false,
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
);
|
|
217
|
+
await waitForLoad(rendered);
|
|
218
|
+
|
|
219
|
+
const user = userEvent.setup({ delay: null });
|
|
220
|
+
await user.click(rendered.getByRole("button", { name: /save/i }));
|
|
221
|
+
|
|
222
|
+
await waitFor(() => expect(onSubmit).toHaveBeenCalled());
|
|
223
|
+
const submittedFields =
|
|
224
|
+
onSubmit.mock.calls[0][0].template.content[0].fields;
|
|
225
|
+
|
|
226
|
+
expect(submittedFields[0].restrictions).toEqual({
|
|
227
|
+
max_date: { mode: "static", value: "2026-12-31" },
|
|
228
|
+
});
|
|
229
|
+
expect(submittedFields[1]).not.toHaveProperty("restrictions");
|
|
230
|
+
});
|
|
231
|
+
|
|
125
232
|
it("initializes template_resource_relations from template when provided", async () => {
|
|
126
233
|
const onSubmit = jest.fn();
|
|
127
234
|
const initialRelations = [
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { screen, fireEvent } from "@testing-library/react";
|
|
3
|
+
import { renderWithIntl as render } from "@truedat/test/renderWithIntl";
|
|
4
|
+
import { DateRestrictionsForm } from "../DateRestrictionsForm";
|
|
5
|
+
|
|
6
|
+
const PREFIX = "content[0].fields[0]";
|
|
7
|
+
|
|
8
|
+
const applyTo = (field, name, value) => {
|
|
9
|
+
const head = `${PREFIX}.restrictions`;
|
|
10
|
+
if (!name.startsWith(head)) return field;
|
|
11
|
+
|
|
12
|
+
const segments = name.slice(head.length).split(".").filter(Boolean);
|
|
13
|
+
|
|
14
|
+
if (segments.length === 0) return { ...field, restrictions: value };
|
|
15
|
+
|
|
16
|
+
const next = { ...(field.restrictions || {}) };
|
|
17
|
+
let cursor = next;
|
|
18
|
+
|
|
19
|
+
segments.forEach((segment, index) => {
|
|
20
|
+
if (index === segments.length - 1) {
|
|
21
|
+
cursor[segment] = value;
|
|
22
|
+
} else {
|
|
23
|
+
cursor[segment] = { ...(cursor[segment] || {}) };
|
|
24
|
+
cursor = cursor[segment];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return { ...field, restrictions: next };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const Harness = ({ initial, onChangeSpy }) => {
|
|
32
|
+
const [field, setField] = useState(initial);
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<DateRestrictionsForm
|
|
36
|
+
field={field}
|
|
37
|
+
errors={field.errors?.restrictions}
|
|
38
|
+
fieldNamePrefix={PREFIX}
|
|
39
|
+
onChange={(_, data) => {
|
|
40
|
+
onChangeSpy?.(data);
|
|
41
|
+
setField(applyTo(field, data.name, data.value));
|
|
42
|
+
}}
|
|
43
|
+
/>
|
|
44
|
+
);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const mount = (initial, onChangeSpy) =>
|
|
48
|
+
render(<Harness initial={initial} onChangeSpy={onChangeSpy} />);
|
|
49
|
+
|
|
50
|
+
const dateField = (restrictions, type = "date") => ({
|
|
51
|
+
name: "fecha",
|
|
52
|
+
label: "Fecha",
|
|
53
|
+
type,
|
|
54
|
+
widget: type,
|
|
55
|
+
restrictions,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const dropdownsIn = (container) => container.querySelectorAll(".ui.dropdown");
|
|
59
|
+
|
|
60
|
+
const openOptions = () =>
|
|
61
|
+
Array.from(document.querySelectorAll(".ui.dropdown .menu > .item"));
|
|
62
|
+
|
|
63
|
+
const choose = (container, occurrence, optionText) => {
|
|
64
|
+
fireEvent.click(dropdownsIn(container)[occurrence]);
|
|
65
|
+
fireEvent.click(
|
|
66
|
+
openOptions().find((item) => item.textContent === optionText),
|
|
67
|
+
);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const amountFor = (container, suffix) =>
|
|
71
|
+
container.querySelector(`input[name="${PREFIX}.restrictions.${suffix}"]`);
|
|
72
|
+
|
|
73
|
+
const calendarFor = (container, suffix) =>
|
|
74
|
+
container.querySelector(`input[name="${PREFIX}.restrictions.${suffix}"]`);
|
|
75
|
+
|
|
76
|
+
const labelsIn = (container) =>
|
|
77
|
+
Array.from(container.querySelectorAll("label")).map(
|
|
78
|
+
(label) => label.textContent,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
describe("choosing which restriction to apply", () => {
|
|
82
|
+
it("offers none, a maximum, a minimum or a range", () => {
|
|
83
|
+
const { container } = mount(dateField(undefined));
|
|
84
|
+
|
|
85
|
+
fireEvent.click(dropdownsIn(container)[0]);
|
|
86
|
+
|
|
87
|
+
expect(openOptions().map((item) => item.textContent)).toEqual([
|
|
88
|
+
"template.field.restrictions.none",
|
|
89
|
+
"template.field.restrictions.max_date",
|
|
90
|
+
"template.field.restrictions.min_date",
|
|
91
|
+
"template.field.restrictions.between",
|
|
92
|
+
]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("none clears an existing restriction rather than leaving a stale bound", () => {
|
|
96
|
+
const spy = jest.fn();
|
|
97
|
+
const { container } = mount(
|
|
98
|
+
dateField({ max_date: { mode: "static", value: "2026-10-05" } }),
|
|
99
|
+
spy,
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
choose(container, 0, "template.field.restrictions.none");
|
|
103
|
+
|
|
104
|
+
expect(spy).toHaveBeenLastCalledWith(
|
|
105
|
+
expect.objectContaining({
|
|
106
|
+
name: `${PREFIX}.restrictions`,
|
|
107
|
+
value: {},
|
|
108
|
+
}),
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("choosing a type reveals its mode and its bound", () => {
|
|
113
|
+
const { container } = mount(dateField(undefined));
|
|
114
|
+
|
|
115
|
+
choose(container, 0, "template.field.restrictions.max_date");
|
|
116
|
+
|
|
117
|
+
expect(labelsIn(container)).toEqual([
|
|
118
|
+
"template.field.restrictions.label",
|
|
119
|
+
"template.field.restrictions.mode",
|
|
120
|
+
"template.field.restrictions.value",
|
|
121
|
+
]);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("layout", () => {
|
|
126
|
+
it("puts the type, the mode and the bound on one line", () => {
|
|
127
|
+
const { container } = mount(dateField(undefined));
|
|
128
|
+
|
|
129
|
+
choose(container, 0, "template.field.restrictions.max_date");
|
|
130
|
+
|
|
131
|
+
const group = container.querySelector(".equal.width.fields");
|
|
132
|
+
expect(group).not.toBeNull();
|
|
133
|
+
expect(group.querySelectorAll(":scope > .field")).toHaveLength(3);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("keeps a relative range on one line too: four bound fields", () => {
|
|
137
|
+
const { container } = mount(
|
|
138
|
+
dateField({
|
|
139
|
+
between: {
|
|
140
|
+
mode: "dynamic",
|
|
141
|
+
from: { amount: -1, unit: "months" },
|
|
142
|
+
to: { amount: 1, unit: "years" },
|
|
143
|
+
},
|
|
144
|
+
}),
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
const group = container.querySelector(".equal.width.fields");
|
|
148
|
+
expect(group.querySelectorAll(":scope > .field")).toHaveLength(6);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("a fixed (static) maximum", () => {
|
|
153
|
+
it("offers a calendar and writes the chosen date", () => {
|
|
154
|
+
const spy = jest.fn();
|
|
155
|
+
const { container } = mount(dateField(undefined), spy);
|
|
156
|
+
|
|
157
|
+
choose(container, 0, "template.field.restrictions.max_date");
|
|
158
|
+
choose(container, 1, "template.field.restrictions.mode.static");
|
|
159
|
+
|
|
160
|
+
fireEvent.change(calendarFor(container, "max_date.value"), {
|
|
161
|
+
target: { value: "2026-10-05" },
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
expect(spy).toHaveBeenLastCalledWith({
|
|
165
|
+
name: `${PREFIX}.restrictions.max_date.value`,
|
|
166
|
+
value: "2026-10-05",
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("shows the stored date", () => {
|
|
171
|
+
const { container } = mount(
|
|
172
|
+
dateField({ max_date: { mode: "static", value: "2026-10-05" } }),
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
expect(calendarFor(container, "max_date.value")).toHaveValue("2026-10-05");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("defaults to static mode", () => {
|
|
179
|
+
const { container } = mount(dateField(undefined));
|
|
180
|
+
|
|
181
|
+
choose(container, 0, "template.field.restrictions.max_date");
|
|
182
|
+
|
|
183
|
+
expect(
|
|
184
|
+
screen.getAllByText("template.field.restrictions.mode.static")[0],
|
|
185
|
+
).toBeInTheDocument();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("shows a validation error beside the invalid bound", () => {
|
|
189
|
+
const { container } = mount({
|
|
190
|
+
...dateField({ max_date: { mode: "static", value: "" } }),
|
|
191
|
+
errors: {
|
|
192
|
+
restrictions: {
|
|
193
|
+
"max_date.value": "template.field.restrictions.validation.required",
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
expect(
|
|
199
|
+
container.querySelector('input[name$="max_date.value"]'),
|
|
200
|
+
).toHaveAttribute("aria-invalid", "true");
|
|
201
|
+
expect(
|
|
202
|
+
screen.getByText("template.field.restrictions.validation.required"),
|
|
203
|
+
).toBeInTheDocument();
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe("a relative maximum", () => {
|
|
208
|
+
const relative = { max_date: { mode: "dynamic", amount: -6, unit: "days" } };
|
|
209
|
+
|
|
210
|
+
it("offers an amount and a unit instead of a calendar", () => {
|
|
211
|
+
const { container } = mount(dateField(relative));
|
|
212
|
+
|
|
213
|
+
expect(amountFor(container, "max_date.amount")).toHaveValue(-6);
|
|
214
|
+
expect(
|
|
215
|
+
screen.getAllByText("template.field.restrictions.units.days")[0],
|
|
216
|
+
).toBeInTheDocument();
|
|
217
|
+
expect(container.querySelector('input[type="date"]')).toBeNull();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("writes the unit that was chosen", () => {
|
|
221
|
+
const spy = jest.fn();
|
|
222
|
+
const { container } = mount(dateField(relative), spy);
|
|
223
|
+
|
|
224
|
+
fireEvent.click(dropdownsIn(container)[1]);
|
|
225
|
+
fireEvent.click(
|
|
226
|
+
openOptions().find(
|
|
227
|
+
(i) => i.textContent === "template.field.restrictions.units.months",
|
|
228
|
+
),
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
expect(spy).toHaveBeenLastCalledWith({
|
|
232
|
+
name: `${PREFIX}.restrictions.max_date.unit`,
|
|
233
|
+
value: "months",
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("shows a positive amount for a minimum relative to the past", () => {
|
|
238
|
+
const { container } = mount(
|
|
239
|
+
dateField({ min_date: { mode: "dynamic", amount: 3, unit: "weeks" } }),
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
expect(amountFor(container, "min_date.amount")).toHaveValue(3);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("keeps a negative minimum amount visible when editing existing data", () => {
|
|
246
|
+
const { container } = mount(
|
|
247
|
+
dateField({ min_date: { mode: "dynamic", amount: -3, unit: "weeks" } }),
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
expect(amountFor(container, "min_date.amount")).toHaveValue(-3);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("accepts and saves a negative amount entered for a minimum", () => {
|
|
254
|
+
const spy = jest.fn();
|
|
255
|
+
const { container } = mount(
|
|
256
|
+
dateField({ min_date: { mode: "dynamic", amount: 3, unit: "weeks" } }),
|
|
257
|
+
spy,
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
fireEvent.change(amountFor(container, "min_date.amount"), {
|
|
261
|
+
target: { value: "-3" },
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
expect(spy).toHaveBeenLastCalledWith({
|
|
265
|
+
name: `${PREFIX}.restrictions.min_date.amount`,
|
|
266
|
+
value: "-3",
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("writes the signed amount as a number", () => {
|
|
271
|
+
const spy = jest.fn();
|
|
272
|
+
mount(dateField(relative), spy);
|
|
273
|
+
|
|
274
|
+
fireEvent.change(amountFor(document, "max_date.amount"), {
|
|
275
|
+
target: { value: "-2" },
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
expect(spy).toHaveBeenLastCalledWith({
|
|
279
|
+
name: `${PREFIX}.restrictions.max_date.amount`,
|
|
280
|
+
value: "-2",
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("preserves the sign of a relative amount entered for a maximum", () => {
|
|
285
|
+
const spy = jest.fn();
|
|
286
|
+
mount(
|
|
287
|
+
dateField({ max_date: { mode: "dynamic", amount: 2, unit: "days" } }),
|
|
288
|
+
spy,
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
fireEvent.change(amountFor(document, "max_date.amount"), {
|
|
292
|
+
target: { value: "3" },
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
expect(spy).toHaveBeenLastCalledWith({
|
|
296
|
+
name: `${PREFIX}.restrictions.max_date.amount`,
|
|
297
|
+
value: "3",
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
describe("a range", () => {
|
|
303
|
+
it("fixed offers two calendars, From and To", () => {
|
|
304
|
+
const { container } = mount(
|
|
305
|
+
dateField({
|
|
306
|
+
between: { mode: "static", start: "2026-01-01", end: "2026-12-31" },
|
|
307
|
+
}),
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
expect(calendarFor(container, "between.start")).toHaveValue("2026-01-01");
|
|
311
|
+
expect(calendarFor(container, "between.end")).toHaveValue("2026-12-31");
|
|
312
|
+
expect(labelsIn(container)).toEqual(
|
|
313
|
+
expect.arrayContaining([
|
|
314
|
+
"template.field.restrictions.label",
|
|
315
|
+
"template.field.restrictions.mode",
|
|
316
|
+
"template.field.restrictions.between.from",
|
|
317
|
+
"template.field.restrictions.between.to",
|
|
318
|
+
]),
|
|
319
|
+
);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it("relative offers two amounts and two units", () => {
|
|
323
|
+
const { container } = mount(
|
|
324
|
+
dateField({
|
|
325
|
+
between: {
|
|
326
|
+
mode: "dynamic",
|
|
327
|
+
from: { amount: -1, unit: "months" },
|
|
328
|
+
to: { amount: 1, unit: "years" },
|
|
329
|
+
},
|
|
330
|
+
}),
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
expect(amountFor(container, "between.from.amount")).toHaveValue(-1);
|
|
334
|
+
expect(amountFor(container, "between.to.amount")).toHaveValue(1);
|
|
335
|
+
|
|
336
|
+
expect(
|
|
337
|
+
screen.getAllByText("template.field.restrictions.units.months")[0],
|
|
338
|
+
).toBeInTheDocument();
|
|
339
|
+
expect(
|
|
340
|
+
screen.getAllByText("template.field.restrictions.units.years")[0],
|
|
341
|
+
).toBeInTheDocument();
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it("mode lives on the parent and never on each side", () => {
|
|
345
|
+
const spy = jest.fn();
|
|
346
|
+
const { container } = mount(dateField(undefined), spy);
|
|
347
|
+
|
|
348
|
+
choose(container, 0, "template.field.restrictions.between");
|
|
349
|
+
choose(container, 1, "template.field.restrictions.mode.dynamic");
|
|
350
|
+
|
|
351
|
+
const names = spy.mock.calls.map(([data]) => data.name);
|
|
352
|
+
|
|
353
|
+
expect(names).toContain(`${PREFIX}.restrictions`);
|
|
354
|
+
|
|
355
|
+
const modeWrites = names.filter((name) => name.endsWith(".mode"));
|
|
356
|
+
for (const name of modeWrites) {
|
|
357
|
+
expect(name).toBe(`${PREFIX}.restrictions.between.mode`);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const sides = spy.mock.calls
|
|
361
|
+
.map(([data]) => data)
|
|
362
|
+
.filter(
|
|
363
|
+
(data) => data.name.includes(".from.") || data.name.includes(".to."),
|
|
364
|
+
);
|
|
365
|
+
for (const side of sides) {
|
|
366
|
+
expect(side.name).not.toContain("mode");
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it("the end of a range writes under `end`, not `to`", () => {
|
|
371
|
+
const spy = jest.fn();
|
|
372
|
+
const { container } = mount(
|
|
373
|
+
dateField({
|
|
374
|
+
between: { mode: "static", start: "2026-01-01", end: "2026-12-31" },
|
|
375
|
+
}),
|
|
376
|
+
spy,
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
fireEvent.change(calendarFor(container, "between.end"), {
|
|
380
|
+
target: { value: "2026-06-30" },
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
expect(spy).toHaveBeenLastCalledWith({
|
|
384
|
+
name: `${PREFIX}.restrictions.between.end`,
|
|
385
|
+
value: "2026-06-30",
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
describe("a datetime field", () => {
|
|
391
|
+
it("uses a date-time calendar, not a date one", () => {
|
|
392
|
+
const { container } = mount(
|
|
393
|
+
dateField(
|
|
394
|
+
{ max_date: { mode: "static", value: "2026-10-05T18:30" } },
|
|
395
|
+
"datetime",
|
|
396
|
+
),
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
expect(calendarFor(container, "max_date.value")).toHaveValue(
|
|
400
|
+
"2026-10-05T18:30",
|
|
401
|
+
);
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
describe("the legacy flat range", () => {
|
|
406
|
+
it("is read as a range rather than ignored", () => {
|
|
407
|
+
const { container } = mount(
|
|
408
|
+
dateField({
|
|
409
|
+
between_start: "2026-01-01",
|
|
410
|
+
between_end: "2026-12-31",
|
|
411
|
+
}),
|
|
412
|
+
);
|
|
413
|
+
|
|
414
|
+
expect(dropdownsIn(container)).toHaveLength(2);
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
describe("fields that cannot carry a restriction", () => {
|
|
419
|
+
it("a string field renders nothing", () => {
|
|
420
|
+
const { container } = render(
|
|
421
|
+
<Harness
|
|
422
|
+
initial={{ name: "s", type: "string", widget: "string" }}
|
|
423
|
+
onChangeSpy={jest.fn()}
|
|
424
|
+
/>,
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
expect(container).toBeEmptyDOMElement();
|
|
428
|
+
});
|
|
429
|
+
});
|