@dhis2-ui/calendar 10.18.0 → 10.19.1

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.
@@ -28,6 +28,62 @@ describe('Calendar Input', () => {
28
28
  }));
29
29
  jest.useRealTimers();
30
30
  });
31
+ it('allow selection of a date through the calendar widget (ethiopic)', async () => {
32
+ jest.useFakeTimers();
33
+ jest.setSystemTime(new Date('2024-10-22T09:05:00.000Z'));
34
+ const onDateSelectMock = jest.fn();
35
+ const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
36
+ calendar: "ethiopian",
37
+ onDateSelect: onDateSelectMock
38
+ }));
39
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
40
+ fireEvent.focus(dateInput);
41
+ const calendar = await screen.findByTestId('calendar');
42
+ expect(calendar).toBeInTheDocument();
43
+ const todayString = '2017-02-12';
44
+ const today = within(calendar).getByTestId(todayString);
45
+
46
+ // the visible day-cell text must be the bare day number, not the
47
+ // full date string used for data-test - regressed silently during
48
+ // the multi-calendar-dates 2.2.0-alpha.1 upgrade (see CLAUDE.md)
49
+ expect(within(today).getByText('12')).toBeInTheDocument();
50
+ fireEvent.click(today);
51
+ await waitFor(() => {
52
+ expect(calendar).not.toBeInTheDocument();
53
+ });
54
+ expect(onDateSelectMock).toHaveBeenCalledWith(expect.objectContaining({
55
+ calendarDateString: todayString
56
+ }));
57
+ jest.useRealTimers();
58
+ });
59
+ it('allow selection of a date through the calendar widget (nepali)', async () => {
60
+ jest.useFakeTimers();
61
+ jest.setSystemTime(new Date('2024-10-22T09:05:00.000Z'));
62
+ const onDateSelectMock = jest.fn();
63
+ const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
64
+ calendar: "nepali",
65
+ onDateSelect: onDateSelectMock
66
+ }));
67
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
68
+ fireEvent.focus(dateInput);
69
+ const calendar = await screen.findByTestId('calendar');
70
+ expect(calendar).toBeInTheDocument();
71
+ const todayString = '2081-07-06';
72
+ const today = within(calendar).getByTestId(todayString);
73
+
74
+ // the visible day-cell text must be the bare day number, not the
75
+ // full date string used for data-test - regressed silently during
76
+ // the multi-calendar-dates 2.2.0-alpha.1 upgrade (see CLAUDE.md)
77
+ expect(within(today).getByText('6')).toBeInTheDocument();
78
+ fireEvent.click(today);
79
+ await waitFor(() => {
80
+ expect(calendar).not.toBeInTheDocument();
81
+ });
82
+ expect(onDateSelectMock).toHaveBeenCalledWith(expect.objectContaining({
83
+ calendarDateString: todayString
84
+ }));
85
+ jest.useRealTimers();
86
+ });
31
87
  it('allow selection of a date through the input', async () => {
32
88
  const onDateSelectMock = jest.fn();
33
89
  const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
@@ -46,7 +102,215 @@ describe('Calendar Input', () => {
46
102
  calendarDateString: dateInputString
47
103
  }));
48
104
  });
105
+ it('rejects input whose detected shape does not match the format prop', () => {
106
+ // "2024-12-25" is unambiguously YYYY-MM-DD shaped (4-digit group
107
+ // first). With format="DD-MM-YYYY" that mismatch is rejected
108
+ // rather than reinterpreted.
109
+ const screen = render(/*#__PURE__*/React.createElement(CalendarWithValidation, {
110
+ calendar: "gregory",
111
+ format: "DD-MM-YYYY"
112
+ }));
113
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
114
+ fireEvent.change(dateInput, {
115
+ target: {
116
+ value: '2024-12-25'
117
+ }
118
+ });
119
+ fireEvent.blur(dateInput);
120
+ expect(screen.getByText('Date string format does not match the specified format. Expected DD-MM-YYYY but got YYYY-MM-DD.')).toBeInTheDocument();
121
+ });
122
+ it('accepts the same typed input without the format prop', () => {
123
+ const screen = render(/*#__PURE__*/React.createElement(CalendarWithValidation, {
124
+ calendar: "gregory"
125
+ }));
126
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
127
+ fireEvent.change(dateInput, {
128
+ target: {
129
+ value: '2024-12-25'
130
+ }
131
+ });
132
+ fireEvent.blur(dateInput);
133
+ expect(screen.queryByText(/does not match the specified format/)).not.toBeInTheDocument();
134
+ });
135
+ it('limits the year selector to the current year when pastOnly is set', async () => {
136
+ jest.useFakeTimers();
137
+ jest.setSystemTime(new Date('2024-10-22T09:05:00.000Z'));
138
+ const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
139
+ calendar: "gregory",
140
+ pastOnly: true,
141
+ onDateSelect: jest.fn()
142
+ }));
143
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
144
+ fireEvent.focus(dateInput);
145
+ const yearSelect = await screen.findByTestId('calendar-year-select');
146
+ const years = within(yearSelect).getAllByRole('option').map(option => Number(option.value));
147
+ expect(Math.max(...years)).toBe(2024);
148
+ jest.useRealTimers();
149
+ });
150
+ it('includes future years in the year selector without pastOnly', async () => {
151
+ jest.useFakeTimers();
152
+ jest.setSystemTime(new Date('2024-10-22T09:05:00.000Z'));
153
+ const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
154
+ calendar: "gregory",
155
+ onDateSelect: jest.fn()
156
+ }));
157
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
158
+ fireEvent.focus(dateInput);
159
+ const yearSelect = await screen.findByTestId('calendar-year-select');
160
+ const years = within(yearSelect).getAllByRole('option').map(option => Number(option.value));
161
+ expect(Math.max(...years)).toBeGreaterThan(2024);
162
+ jest.useRealTimers();
163
+ });
164
+ it('sets the calendar wrapper direction from the dir prop', async () => {
165
+ const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
166
+ calendar: "gregory",
167
+ dir: "rtl",
168
+ onDateSelect: jest.fn()
169
+ }));
170
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
171
+ fireEvent.focus(dateInput);
172
+ const calendar = await screen.findByTestId('calendar');
173
+ expect(calendar).toHaveAttribute('dir', 'rtl');
174
+ });
175
+ it('renders day labels using the numberingSystem prop', async () => {
176
+ jest.useFakeTimers();
177
+ jest.setSystemTime(new Date('2024-10-22T09:05:00.000Z'));
178
+ const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
179
+ calendar: "gregory",
180
+ numberingSystem: "arab",
181
+ onDateSelect: jest.fn()
182
+ }));
183
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
184
+ fireEvent.focus(dateInput);
185
+ const calendar = await screen.findByTestId('calendar');
186
+ const today = within(calendar).getByTestId('2024-10-22');
187
+ expect(today).toHaveTextContent('٢٢');
188
+ jest.useRealTimers();
189
+ });
190
+ it('renders weekday headers using the weekDayFormat prop', async () => {
191
+ const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
192
+ calendar: "gregory",
193
+ weekDayFormat: "long",
194
+ onDateSelect: jest.fn()
195
+ }));
196
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
197
+ fireEvent.focus(dateInput);
198
+ const calendar = await screen.findByTestId('calendar');
199
+ expect(within(calendar).getByText('Monday')).toBeInTheDocument();
200
+ });
201
+
202
+ // Regression coverage for LIBS-763: useDatePicker (in @dhis2/multi-calendar-dates)
203
+ // used to rebuild the year/month navigation lists and the day-cell grid on every
204
+ // render - including every keystroke while typing here, with the calendar widget
205
+ // open - regardless of whether the visible month/calendar/locale had actually
206
+ // changed. Fixed upstream in @dhis2/multi-calendar-dates@3.0.0-alpha.1.
207
+ describe('performance (LIBS-763)', () => {
208
+ // For gregory/ethiopic (native-ICU calendars) the broken memoization showed
209
+ // up as ~500 redundant `new Intl.DateTimeFormat(...)` constructions for a
210
+ // single keystroke; count is a deterministic, machine-speed-independent
211
+ // signal, unlike timing it directly.
212
+ const countDateTimeFormatConstructions = fn => {
213
+ const realResolvedOptions = Intl.DateTimeFormat.prototype.resolvedOptions;
214
+ let count = 0;
215
+ Intl.DateTimeFormat.prototype.resolvedOptions = function (...args) {
216
+ count++;
217
+ return realResolvedOptions.apply(this, args);
218
+ };
219
+ try {
220
+ fn();
221
+ } finally {
222
+ Intl.DateTimeFormat.prototype.resolvedOptions = realResolvedOptions;
223
+ }
224
+ return count;
225
+ };
226
+ it('does not reconstruct Intl.DateTimeFormat instances while typing (gregory)', async () => {
227
+ const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
228
+ calendar: "gregory",
229
+ onDateSelect: jest.fn()
230
+ }));
231
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
232
+ fireEvent.focus(dateInput);
233
+ await screen.findByTestId('calendar');
234
+ const constructions = countDateTimeFormatConstructions(() => {
235
+ fireEvent.change(dateInput, {
236
+ target: {
237
+ value: '2024-10-1'
238
+ }
239
+ });
240
+ });
241
+ expect(constructions).toBeLessThan(10);
242
+ });
243
+ it('does not reconstruct Intl.DateTimeFormat instances while typing (ethiopic)', async () => {
244
+ const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
245
+ calendar: "ethiopian",
246
+ onDateSelect: jest.fn()
247
+ }));
248
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
249
+ fireEvent.focus(dateInput);
250
+ await screen.findByTestId('calendar');
251
+ const constructions = countDateTimeFormatConstructions(() => {
252
+ fireEvent.change(dateInput, {
253
+ target: {
254
+ value: '2017-02-0'
255
+ }
256
+ });
257
+ });
258
+ expect(constructions).toBeLessThan(10);
259
+ });
260
+
261
+ // Nepali is a custom (non-ICU) calendar, so it never goes through
262
+ // Intl.DateTimeFormat - the same broken memoization instead showed up as
263
+ // redundant custom-calendar date-conversion work, so time it directly.
264
+ // 300ms for 10 keystrokes is ~25x the fixed baseline (~13ms) and ~2.5x
265
+ // below the broken one (~760ms), leaving headroom in both directions.
266
+ // Skipped in CI: unlike the count-based tests above, this asserts on
267
+ // wall-clock time, which can be flaky on slower/shared CI runners.
268
+ // Uncomment to run locally when touching useDatePicker/useNavigation.
269
+ it.skip('keeps typing fast (nepali)', async () => {
270
+ const screen = render(/*#__PURE__*/React.createElement(CalendarInput, {
271
+ calendar: "nepali",
272
+ onDateSelect: jest.fn()
273
+ }));
274
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
275
+ fireEvent.focus(dateInput);
276
+ await screen.findByTestId('calendar');
277
+ const chars = '2081-07-01'.split('');
278
+ const progressive = chars.map((_, i) => chars.slice(0, i + 1).join(''));
279
+ const start = process.hrtime.bigint();
280
+ progressive.forEach(value => fireEvent.change(dateInput, {
281
+ target: {
282
+ value
283
+ }
284
+ }));
285
+ const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6;
286
+ expect(elapsedMs).toBeLessThan(300);
287
+ });
288
+ });
49
289
  describe('validation', () => {
290
+ it('should show a warning instead of an error when strictValidation is false', () => {
291
+ const onDateSelectMock = jest.fn();
292
+ const screen = render(/*#__PURE__*/React.createElement(CalendarWithValidation, {
293
+ calendar: "gregory",
294
+ minDate: "2024-01-01",
295
+ strictValidation: false,
296
+ onDateSelect: onDateSelectMock
297
+ }));
298
+ const dateInputString = '2023-10-12';
299
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
300
+ fireEvent.change(dateInput, {
301
+ target: {
302
+ value: dateInputString
303
+ }
304
+ });
305
+ fireEvent.blur(dateInput);
306
+ expect(screen.getByText('Date 2023-10-12 is less than the minimum allowed date 2024-01-01.')).toBeInTheDocument();
307
+ expect(onDateSelectMock).toHaveBeenCalledWith(expect.objectContaining({
308
+ validation: expect.objectContaining({
309
+ warning: true,
310
+ valid: true
311
+ })
312
+ }));
313
+ });
50
314
  it('should validate minimum date', () => {
51
315
  const onDateSelectMock = jest.fn();
52
316
  const screen = render(/*#__PURE__*/React.createElement(CalendarWithValidation, {
@@ -65,6 +329,60 @@ describe('Calendar Input', () => {
65
329
  expect(screen.getByText('Date 2023-10-12 is less than the minimum allowed date 2024-01-01.')).toBeInTheDocument();
66
330
  expect(onDateSelectMock).toHaveBeenCalledTimes(1);
67
331
  });
332
+
333
+ // Regression test: minDate/maxDate used to reach validateDateString
334
+ // (as minDateString/maxDateString) but not useDatePicker itself, due to a
335
+ // key mismatch - so in strict mode the input rejected an out-of-range
336
+ // date while the widget still selected it. Warning mode is unaffected
337
+ // (see the test below) since useDatePicker only falls back to today when
338
+ // strictValidation produces an `error`.
339
+ it('calendar widget falls back to today for a date the input has flagged as below the minimum (strict mode)', async () => {
340
+ jest.useFakeTimers();
341
+ jest.setSystemTime(new Date('2024-10-22T09:05:00.000Z'));
342
+ const screen = render(/*#__PURE__*/React.createElement(CalendarWithValidation, {
343
+ calendar: "gregory",
344
+ minDate: "2024-01-01"
345
+ }));
346
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
347
+ fireEvent.change(dateInput, {
348
+ target: {
349
+ value: '2020-01-15'
350
+ }
351
+ });
352
+ fireEvent.blur(dateInput);
353
+ expect(screen.getByText('Date 2020-01-15 is less than the minimum allowed date 2024-01-01.')).toBeInTheDocument();
354
+ fireEvent.focus(dateInput);
355
+ const calendar = await screen.findByTestId('calendar');
356
+
357
+ // expected: the widget falls back to today, like useDatePicker's own
358
+ // getInvalidDateResult does for a date it recognizes as invalid
359
+ expect(within(calendar).getByTestId('2024-10-22').querySelector('button')).toHaveClass('isToday');
360
+ jest.useRealTimers();
361
+ });
362
+ it('calendar widget correctly still selects an out-of-range date in warning mode', async () => {
363
+ jest.useFakeTimers();
364
+ jest.setSystemTime(new Date('2024-10-22T09:05:00.000Z'));
365
+ const screen = render(/*#__PURE__*/React.createElement(CalendarWithValidation, {
366
+ calendar: "gregory",
367
+ minDate: "2024-01-01",
368
+ strictValidation: false
369
+ }));
370
+ const dateInput = within(screen.getByTestId('dhis2-uicore-input')).getByRole('textbox');
371
+ fireEvent.change(dateInput, {
372
+ target: {
373
+ value: '2020-01-15'
374
+ }
375
+ });
376
+ fireEvent.blur(dateInput);
377
+ expect(screen.getByText('Date 2020-01-15 is less than the minimum allowed date 2024-01-01.')).toBeInTheDocument();
378
+ fireEvent.focus(dateInput);
379
+ const calendar = await screen.findByTestId('calendar');
380
+
381
+ // warning mode means "flag it, but still let them pick it" - the
382
+ // widget showing the out-of-range date as selected is intentional
383
+ expect(within(calendar).getByTestId('2020-01-15').querySelector('button')).toHaveClass('isSelected');
384
+ jest.useRealTimers();
385
+ });
68
386
  it('should validate maximum date', () => {
69
387
  const {
70
388
  getByTestId,
@@ -83,8 +401,7 @@ describe('Calendar Input', () => {
83
401
  fireEvent.blur(dateInput);
84
402
  expect(getByText('Date 2024-10-12 is greater than the maximum allowed date 2024-01-01.')).toBeInTheDocument();
85
403
  });
86
- // Temporarily disabled
87
- it.skip('should validate date in ethiopic calendar', () => {
404
+ it('should validate date in ethiopic calendar', () => {
88
405
  const onDateSelectMock = jest.fn();
89
406
  const {
90
407
  getByTestId,
@@ -122,7 +439,6 @@ describe('Calendar Input', () => {
122
439
  fireEvent.blur(dateInput);
123
440
  expect(getByText('Invalid date in specified calendar')).toBeInTheDocument();
124
441
  });
125
- // ToDo: these scenarios seem to work but they timeout on CI sporadically - ticket: https://dhis2.atlassian.net/browse/LIBS-763
126
442
  it('should validate date in nepali calendar', () => {
127
443
  const onDateSelectMock = jest.fn();
128
444
  const {
@@ -78,7 +78,10 @@ export const CalendarInput = ({
78
78
  setOpen(false);
79
79
  },
80
80
  date,
81
- ...validationOptions,
81
+ minDate,
82
+ maxDate,
83
+ format,
84
+ strictValidation,
82
85
  options: useDatePickerOptions
83
86
  });
84
87
  const handleChange = e => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhis2-ui/calendar",
3
- "version": "10.18.0",
3
+ "version": "10.19.1",
4
4
  "description": "UI Calendar",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,15 +33,15 @@
33
33
  "styled-jsx": "^4"
34
34
  },
35
35
  "dependencies": {
36
- "@dhis2-ui/button": "10.18.0",
37
- "@dhis2-ui/card": "10.18.0",
38
- "@dhis2-ui/input": "10.18.0",
39
- "@dhis2-ui/layer": "10.18.0",
40
- "@dhis2-ui/popper": "10.18.0",
41
- "@dhis2/multi-calendar-dates": "2.1.2",
36
+ "@dhis2-ui/button": "10.19.1",
37
+ "@dhis2-ui/card": "10.19.1",
38
+ "@dhis2-ui/input": "10.19.1",
39
+ "@dhis2-ui/layer": "10.19.1",
40
+ "@dhis2-ui/popper": "10.19.1",
41
+ "@dhis2/multi-calendar-dates": "3.0.0",
42
42
  "@dhis2/prop-types": "^3.1.2",
43
- "@dhis2/ui-constants": "10.18.0",
44
- "@dhis2/ui-icons": "10.18.0",
43
+ "@dhis2/ui-constants": "10.19.1",
44
+ "@dhis2/ui-icons": "10.19.1",
45
45
  "classnames": "^2.3.1",
46
46
  "prop-types": "^15.7.2"
47
47
  },
@@ -0,0 +1,25 @@
1
+ # CalendarInput
2
+
3
+ `CalendarInput` wraps the `Calendar` widget (`../calendar/`) with a text input, validation, and a clear button. Both get their date logic — parsing, localization, navigation — from `@dhis2/multi-calendar-dates`'s `useDatePicker`/`useResolvedDirection`/`validateDateString`; this directory has no date-math of its own.
4
+
5
+ ## Data flow
6
+
7
+ Dates cross the public API as calendar-native `YYYY-MM-DD` strings (in whatever `calendar` prop is set — not necessarily Gregorian ISO), never as `Temporal` objects. Treat any change that would leak a `Temporal`/date-engine object through `onDateSelect`, `date`, or similar props as a breaking change. `CalendarInput`'s `onDateSelect` payload also carries a `validation` object that `CalendarInput` computes itself via `validateDateString` — it is _not_ part of `multi-calendar-dates`'s types, so it's declared separately in `../../types/index.d.ts` (`CalendarInputValidation` / `CalendarInputOnDateSelectPayload`). If you change how validation is computed or shaped, update that file too.
8
+
9
+ ## Testing
10
+
11
+ Two surfaces, used for different things:
12
+
13
+ - **Jest** (`__tests__/calendar-input.test.js`, run via `yarn test`) — component behavior and logic: prop handling, validation, callbacks.
14
+ - **Cypress + Cucumber e2e** (`../features/*.feature` + step defs, driven off `../__e2e__/calendar-input.e2e.stories.js`, run via `yarn cy:test` at the repo root) — real rendered output and localization across calendars. Heavier; not run by `yarn test`.
15
+
16
+ When adding a prop or behavior, add a Jest test. If it's about rendering or localized text for a specific calendar/locale, check whether a `.feature` scenario should exist too.
17
+
18
+ Coverage decisions in this component are scoped to the three calendars the project actually supports in practice: **Gregorian, Ethiopic, Nepali**. Ethiopic and Nepali behave differently enough from Gregorian (Ethiopic is a native-ICU calendar with its own era handling; Nepali is a custom, non-ICU calendar implemented in `multi-calendar-dates` via a lookup table, not `Intl`) that "tested for Gregorian" should not be assumed to mean "tested for the other two" — verify explicitly rather than assuming parity.
19
+
20
+ ## Gotchas
21
+
22
+ - **Day-cell tests must assert on visible text, not just the `data-test` attribute.** `data-test` on a day cell is always the full date string (e.g. `2024-10-17`); the _rendered label_ is a separate value from `multi-calendar-dates` and can silently regress into something else (it has, historically) without any `data-test`-based assertion catching it. Assert `within(cell).getByText('17')` (or the calendar-appropriate bare day number) in addition to clicking by `data-test`.
23
+ - **`useDatePicker` recomputes on every render of `CalendarInput`, regardless of whether the calendar dropdown is open.** Its output must stay properly memoized inside `multi-calendar-dates` — if you find yourself adding expensive work anywhere in this render path (here or upstream), verify it doesn't run on every keystroke. The `performance (LIBS-763)` describe block in the test file guards against this regressing again; keep it green.
24
+ - **`inputWidth` is deliberately untested.** It's a passthrough to a `styled-jsx` CSS width — purely cosmetic, and `styled-jsx` output is fragile to assert on in jsdom. Don't assume this is an oversight.
25
+ - **`minDate`/`maxDate`/`format`/`strictValidation` get passed to two different functions with different expectations**: `validateDateString` (via `validationOptions`, keyed `minDateString`/`maxDateString`) drives the visible error text, while `useDatePicker` takes the same values under its own key names (`minDate`/`maxDate`) to decide whether to fall back to today internally in strict mode. These used to silently disagree (the `validationOptions` object, keyed for `validateDateString`, was spread straight into `useDatePicker` too) — the widget would keep an out-of-range date selected in strict mode even though the input correctly rejected it. Pass `useDatePicker` its own correctly-named fields explicitly; don't assume an options object built for one of these functions is safe to reuse for the other.