-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
pickers-utils.tsx
544 lines (474 loc) · 15.4 KB
/
pickers-utils.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import * as React from 'react';
import { expect } from 'chai';
import sinon from 'sinon';
import {
createRenderer,
screen,
RenderOptions,
userEvent,
getByRole,
act,
fireEvent,
createEvent,
} from '@mui/monorepo/test/utils';
import { CreateRendererOptions } from '@mui/monorepo/test/utils/createRenderer';
import { unstable_useControlled as useControlled } from '@mui/utils';
import { TransitionProps } from '@mui/material/transitions';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
// import { AdapterJsJoda } from '@mui/x-date-pickers/AdapterJsJoda';
import { AdapterMomentHijri } from '@mui/x-date-pickers/AdapterMomentHijri';
import { AdapterMomentJalaali } from '@mui/x-date-pickers/AdapterMomentJalaali';
import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
import { MuiPickersAdapter } from '@mui/x-date-pickers/internals/models';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { CLOCK_WIDTH } from '@mui/x-date-pickers/TimeClock/shared';
export type AdapterName =
| 'date-fns'
| 'dayjs'
| 'luxon'
| 'moment'
| 'moment-hijri'
| 'moment-jalaali'
// | 'js-joda'
| 'date-fns-jalali';
const availableAdapters: { [key in AdapterName]: new (...args: any) => MuiPickersAdapter<any> } = {
'date-fns': AdapterDateFns,
dayjs: AdapterDayjs,
luxon: AdapterLuxon,
moment: AdapterMoment,
'moment-hijri': AdapterMomentHijri,
'moment-jalaali': AdapterMomentJalaali,
'date-fns-jalali': AdapterDateFnsJalali,
// 'js-joda': AdapterJsJoda,
};
let AdapterClassToExtend = availableAdapters['date-fns'];
// Check if we are in unit tests
if (/jsdom/.test(window.navigator.userAgent)) {
// Add parameter `--date-adapter luxon` to use AdapterLuxon for running tests
// adapter available : date-fns (default one), dayjs, luxon, moment
const args = process.argv.slice(2);
const flagIndex = args.findIndex((element) => element === '--date-adapter');
if (flagIndex !== -1 && flagIndex + 1 < args.length) {
const potentialAdapter = args[flagIndex + 1];
if (potentialAdapter) {
if (availableAdapters[potentialAdapter]) {
AdapterClassToExtend = availableAdapters[potentialAdapter];
} else {
const supportedAdapters = Object.keys(availableAdapters);
const message = `Error: Invalid --date-adapter value "${potentialAdapter}". Supported date adapters: ${supportedAdapters
.map((key) => `"${key}"`)
.join(', ')}`;
// eslint-disable-next-line no-console
console.log(message); // log message explicitly, because error message gets swallowed by mocha
throw new Error(message);
}
}
}
}
export class AdapterClassToUse extends AdapterClassToExtend {}
export const adapterToUse = new AdapterClassToUse();
export const FakeTransitionComponent = React.forwardRef<HTMLDivElement, TransitionProps>(
function FakeTransitionComponent({ children }, ref) {
// set tabIndex in case it is used as a child of <TrapFocus />
return (
<div ref={ref} tabIndex={-1}>
{children}
</div>
);
},
);
interface CreatePickerRendererOptions extends CreateRendererOptions {
// Set-up locale with date-fns object. Other are deduced from `locale.code`
locale?: Locale;
adapterName?: AdapterName;
}
export function wrapPickerMount(
mount: (node: React.ReactElement) => import('enzyme').ReactWrapper,
) {
return (node: React.ReactElement) =>
mount(<LocalizationProvider dateAdapter={AdapterClassToUse}>{node}</LocalizationProvider>);
}
export function createPickerRenderer({
locale,
adapterName,
...createRendererOptions
}: CreatePickerRendererOptions = {}) {
const { clock, render: clientRender } = createRenderer(createRendererOptions);
let adapterLocale = [
'date-fns',
'date-fns-jalali',
// 'js-joda'
].includes(adapterName ?? adapterToUse.lib)
? locale
: locale?.code;
if (typeof adapterLocale === 'string' && adapterLocale.length > 2) {
adapterLocale = adapterLocale.slice(0, 2);
}
const adapter = adapterName
? new availableAdapters[adapterName]({ locale: adapterLocale })
: new AdapterClassToUse({ locale: adapterLocale });
function Wrapper({ children }: { children?: React.ReactNode }) {
return (
<LocalizationProvider
adapterLocale={adapterLocale}
dateAdapter={adapterName ? availableAdapters[adapterName] : AdapterClassToUse}
>
{children}
</LocalizationProvider>
);
}
return {
clock,
render(node: React.ReactElement, options?: Omit<RenderOptions, 'wrapper'>) {
return clientRender(node, { ...options, wrapper: Wrapper });
},
adapter,
};
}
export type OpenPickerParams =
| {
type: 'date' | 'date-time' | 'time';
variant: 'mobile' | 'desktop';
}
| {
type: 'date-range';
variant: 'mobile' | 'desktop';
initialFocus: 'start' | 'end';
};
export const openPicker = (params: OpenPickerParams) => {
if (params.type === 'date-range') {
const target = screen.getAllByRole('textbox')[params.initialFocus === 'start' ? 0 : 1];
return userEvent.mousePress(target);
}
if (params.variant === 'mobile') {
return userEvent.mousePress(screen.getByRole('textbox'));
}
const target =
params.type === 'time'
? screen.getByLabelText(/choose time/i)
: screen.getByLabelText(/choose date/i);
return userEvent.mousePress(target);
};
// TODO: Handle dynamic values
export const getClockMouseEvent = (
type: 'mousedown' | 'mousemove' | 'mouseup',
offsetX = 20,
offsetY = 15,
) => {
const event = new window.MouseEvent(type, {
bubbles: true,
cancelable: true,
buttons: 1,
});
Object.defineProperty(event, 'offsetX', { get: () => offsetX });
Object.defineProperty(event, 'offsetY', { get: () => offsetY });
return event;
};
export const getClockTouchEvent = (value: number, view: 'minutes' | '12hours' | '24hours') => {
// TODO: Handle 24 hours clock
if (view === '24hours') {
throw new Error('Do not support 24 hours clock yet');
}
let itemCount: number;
if (view === 'minutes') {
itemCount = 60;
} else {
itemCount = 12;
}
const angle = Math.PI / 2 - (Math.PI * 2 * value) / itemCount;
const clientX = Math.round(((1 + Math.cos(angle)) * CLOCK_WIDTH) / 2);
const clientY = Math.round(((1 - Math.sin(angle)) * CLOCK_WIDTH) / 2);
return {
changedTouches: [
{
clientX,
clientY,
},
],
};
};
export const rangeCalendarDayTouches = {
'2018-01-01': {
clientX: 85,
clientY: 125,
},
'2018-01-02': {
clientX: 125,
clientY: 125,
},
'2018-01-09': {
clientX: 125,
clientY: 165,
},
'2018-01-10': {
clientX: 165,
clientY: 165,
},
'2018-01-11': {
clientX: 205,
clientY: 165,
},
} as const;
export const withPickerControls =
<
TValue,
Props extends { value: TValue; onChange: Function; components?: {}; componentsProps?: {} },
>(
Component: React.ComponentType<Props>,
) =>
<DefaultProps extends Partial<Props>>(defaultProps: DefaultProps) => {
return function WithPickerControls(
props: Omit<
Props,
'value' | 'onChange' | Exclude<keyof DefaultProps, 'components' | 'componentsProps'>
> &
Partial<Omit<DefaultProps, 'components' | 'componentsProps'>> & {
initialValue?: TValue;
value?: TValue;
onChange?: any;
},
) {
const { initialValue, value: inValue, onChange, ...other } = props;
const [value, setValue] = useControlled({
controlled: inValue,
default: initialValue,
name: 'withPickerControls',
state: 'value,',
});
const handleChange = React.useCallback(
(newValue: TValue, keyboardInputValue?: string) => {
setValue(newValue);
onChange?.(newValue, keyboardInputValue);
},
[onChange, setValue],
);
return (
<Component
{...defaultProps}
{...(other as any)}
components={{ ...defaultProps.components, ...(other as any).components }}
componentsProps={{ ...defaultProps.componentsProps, ...(other as any).componentsProps }}
value={value}
onChange={handleChange}
/>
);
};
};
export const stubMatchMedia = (matches = true) =>
sinon.stub().returns({
matches,
addListener: () => {},
removeListener: () => {},
});
export const getPickerDay = (name: string, picker = 'January 2018') =>
getByRole(screen.getByText(picker)?.parentElement?.parentElement!, 'gridcell', { name });
export const cleanText = (text) => text.replace(/\u200e|\u2066|\u2067|\u2068|\u2069/g, '');
export const getCleanedSelectedContent = (input: HTMLInputElement) =>
cleanText(input.value.slice(input.selectionStart ?? 0, input.selectionEnd ?? 0));
export const expectInputValue = (
input: HTMLInputElement,
expectedValue: string,
shouldRemoveDashSpaces = false,
) => {
let value = cleanText(input.value);
if (shouldRemoveDashSpaces) {
value = value.replace(/ \/ /g, '/');
}
return expect(value).to.equal(expectedValue);
};
export const expectInputPlaceholder = (input: HTMLInputElement, placeholder: string) => {
const cleanPlaceholder = cleanText(input.placeholder).replace(/ \/ /g, '/');
return expect(cleanPlaceholder).to.equal(placeholder);
};
interface BuildFieldInteractionsParams<P extends {}> {
// TODO: Export `Clock` from monorepo
clock: ReturnType<typeof createRenderer>['clock'];
render: ReturnType<typeof createRenderer>['render'];
Component: React.FunctionComponent<P>;
}
export interface BuildFieldInteractionsResponse<P extends {}> {
clickOnInput: (
input: HTMLInputElement,
cursorStartPosition: number,
cursorEndPosition?: number,
) => void;
selectSection: (input: HTMLInputElement, activeSectionIndex: number) => void;
testFieldKeyPress: (
params: P & {
key: string;
expectedValue: string;
cursorPosition?: number;
valueToSelect?: string;
},
) => void;
testFieldChange: (
params: P & {
keyStrokes: { value: string; expected: string }[];
cursorPosition?: number;
},
) => void;
}
export const getTextbox = (): HTMLInputElement => screen.getByRole('textbox');
export const buildFieldInteractions = <P extends {}>({
clock,
render,
Component,
}: BuildFieldInteractionsParams<P>): BuildFieldInteractionsResponse<P> => {
const clickOnInput: BuildFieldInteractionsResponse<P>['clickOnInput'] = (
input,
cursorStartPosition,
cursorEndPosition = cursorStartPosition,
) => {
if (document.activeElement !== input) {
act(() => {
input.focus();
});
clock.runToLast();
}
act(() => {
fireEvent.mouseDown(input);
fireEvent.mouseUp(input);
input.setSelectionRange(cursorStartPosition, cursorEndPosition);
fireEvent.click(input);
clock.runToLast();
});
};
const selectSection: BuildFieldInteractionsResponse<P>['selectSection'] = (
input,
activeSectionIndex,
) => {
const value = input.value.replace(':', '/');
// TODO: Improve this logic when we will be able to access state.sections from the outside
let clickPosition: number;
if (activeSectionIndex === 0) {
clickPosition = 0;
} else {
clickPosition =
(value.split('/', activeSectionIndex - 1).join('/').length +
value.split('/', activeSectionIndex).join('/').length) /
2;
}
clickOnInput(input, clickPosition);
};
const testFieldKeyPress: BuildFieldInteractionsResponse<P>['testFieldKeyPress'] = ({
key,
expectedValue,
cursorPosition = 1,
valueToSelect,
...props
}) => {
render(<Component {...(props as any as P)} />);
const input = getTextbox();
// focus input to trigger setting placeholder as value if no value is present
act(() => {
input.focus();
});
// make sure the value of the input is rendered before proceeding
clock.runToLast();
const clickPosition = valueToSelect ? input.value.indexOf(valueToSelect) : cursorPosition;
if (clickPosition === -1) {
throw new Error(
`Failed to find value to select "${valueToSelect}" in input value: ${input.value}`,
);
}
clickOnInput(input, clickPosition);
userEvent.keyPress(input, { key });
expectInputValue(input, expectedValue);
};
const testFieldChange: BuildFieldInteractionsResponse<P>['testFieldChange'] = ({
keyStrokes,
cursorPosition = 1,
...props
}) => {
render(<Component {...(props as any as P)} />);
const input = getTextbox();
clickOnInput(input, cursorPosition);
keyStrokes.forEach((keyStroke) => {
fireEvent.change(input, { target: { value: keyStroke.value } });
expectInputValue(input, keyStroke.expected);
});
};
return { clickOnInput, selectSection, testFieldKeyPress, testFieldChange };
};
export const buildPickerDragInteractions = (getDataTransfer: () => DataTransfer | null) => {
const createDragEvent = (type: DragEventTypes, target: ChildNode) => {
const createdEvent = createEvent[type](target);
Object.defineProperty(createdEvent, 'dataTransfer', {
value: getDataTransfer(),
});
return createdEvent;
};
const executeDateDragWithoutDrop = (startDate: ChildNode, ...otherDates: ChildNode[]) => {
const endDate = otherDates[otherDates.length - 1];
fireEvent(startDate, createDragEvent('dragStart', startDate));
fireEvent(startDate, createDragEvent('dragLeave', startDate));
otherDates.slice(0, otherDates.length - 1).forEach((date) => {
fireEvent(date, createDragEvent('dragEnter', date));
fireEvent(date, createDragEvent('dragOver', date));
fireEvent(date, createDragEvent('dragLeave', date));
});
fireEvent(endDate, createDragEvent('dragEnter', endDate));
fireEvent(endDate, createDragEvent('dragOver', endDate));
};
const executeDateDrag = (startDate: ChildNode, ...otherDates: ChildNode[]) => {
executeDateDragWithoutDrop(startDate, ...otherDates);
const endDate = otherDates[otherDates.length - 1];
fireEvent(endDate, createDragEvent('drop', endDate));
fireEvent(endDate, createDragEvent('dragEnd', endDate));
};
return { executeDateDragWithoutDrop, executeDateDrag };
};
export type DragEventTypes =
| 'dragStart'
| 'dragOver'
| 'dragEnter'
| 'dragLeave'
| 'dragEnd'
| 'drop';
export class MockedDataTransfer implements DataTransfer {
data: Record<string, string>;
dropEffect: 'none' | 'copy' | 'move' | 'link';
effectAllowed:
| 'none'
| 'copy'
| 'copyLink'
| 'copyMove'
| 'link'
| 'linkMove'
| 'move'
| 'all'
| 'uninitialized';
files: FileList;
img?: Element;
items: DataTransferItemList;
types: string[];
xOffset: number;
yOffset: number;
constructor() {
this.data = {};
this.dropEffect = 'none';
this.effectAllowed = 'all';
this.files = [] as unknown as FileList;
this.items = [] as unknown as DataTransferItemList;
this.types = [];
this.xOffset = 0;
this.yOffset = 0;
}
clearData() {
this.data = {};
}
getData(format: string) {
return this.data[format];
}
setData(format: string, data: string) {
this.data[format] = data;
}
setDragImage(img: Element, xOffset: number, yOffset: number) {
this.img = img;
this.xOffset = xOffset;
this.yOffset = yOffset;
}
}