|
| 1 | +import * as React from 'react'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { screen } from '@mui/internal-test-utils'; |
| 4 | +import { PickersTimezone, PickerValidDate } from '@mui/x-date-pickers/models'; |
| 5 | +import { createPickerRenderer } from 'test/utils/pickers'; |
| 6 | +import { useValueWithTimezone } from './useValueWithTimezone'; |
| 7 | +import { singleItemValueManager } from '../utils/valueManagers'; |
| 8 | + |
| 9 | +describe('useValueWithTimezone', () => { |
| 10 | + const { render, adapter } = createPickerRenderer({ |
| 11 | + clock: 'fake', |
| 12 | + adapterName: 'luxon', |
| 13 | + }); |
| 14 | + |
| 15 | + function runTest(params: { |
| 16 | + timezone: PickersTimezone | undefined; |
| 17 | + value: PickerValidDate | null | undefined; |
| 18 | + defaultValue: PickerValidDate | null | undefined; |
| 19 | + referenceDate: PickerValidDate | undefined; |
| 20 | + expectedTimezone: PickersTimezone; |
| 21 | + }) { |
| 22 | + const { expectedTimezone, ...other } = params; |
| 23 | + |
| 24 | + function TestComponent(props: typeof other) { |
| 25 | + const { timezone } = useValueWithTimezone({ |
| 26 | + ...props, |
| 27 | + valueManager: singleItemValueManager, |
| 28 | + onChange: () => {}, |
| 29 | + }); |
| 30 | + |
| 31 | + return <div data-testid="result">{timezone}</div>; |
| 32 | + } |
| 33 | + |
| 34 | + render(<TestComponent {...other} />); |
| 35 | + |
| 36 | + expect(screen.getByTestId('result').textContent).to.equal(expectedTimezone); |
| 37 | + } |
| 38 | + |
| 39 | + it('should use the timezone parameter when provided', () => { |
| 40 | + runTest({ |
| 41 | + timezone: 'America/New_York', |
| 42 | + value: undefined, |
| 43 | + defaultValue: undefined, |
| 44 | + referenceDate: undefined, |
| 45 | + expectedTimezone: 'America/New_York', |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should use the timezone parameter over the value parameter when both are provided', () => { |
| 50 | + runTest({ |
| 51 | + timezone: 'America/New_York', |
| 52 | + value: adapter.date(undefined, 'Europe/Paris'), |
| 53 | + defaultValue: undefined, |
| 54 | + referenceDate: undefined, |
| 55 | + expectedTimezone: 'America/New_York', |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should use the value parameter when provided', () => { |
| 60 | + runTest({ |
| 61 | + timezone: undefined, |
| 62 | + value: adapter.date(undefined, 'America/New_York'), |
| 63 | + defaultValue: undefined, |
| 64 | + referenceDate: undefined, |
| 65 | + expectedTimezone: 'America/New_York', |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should use the value parameter over the defaultValue parameter when both are provided', () => { |
| 70 | + runTest({ |
| 71 | + timezone: undefined, |
| 72 | + value: adapter.date(undefined, 'America/New_York'), |
| 73 | + defaultValue: adapter.date(undefined, 'Europe/Paris'), |
| 74 | + referenceDate: undefined, |
| 75 | + expectedTimezone: 'America/New_York', |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should use the defaultValue parameter when provided', () => { |
| 80 | + runTest({ |
| 81 | + timezone: undefined, |
| 82 | + value: undefined, |
| 83 | + defaultValue: adapter.date(undefined, 'America/New_York'), |
| 84 | + referenceDate: undefined, |
| 85 | + expectedTimezone: 'America/New_York', |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should use the referenceDate parameter when provided', () => { |
| 90 | + runTest({ |
| 91 | + timezone: undefined, |
| 92 | + value: undefined, |
| 93 | + defaultValue: undefined, |
| 94 | + referenceDate: adapter.date(undefined, 'America/New_York'), |
| 95 | + expectedTimezone: 'America/New_York', |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should use the "default" timezone is there is no way to deduce the user timezone', () => { |
| 100 | + runTest({ |
| 101 | + timezone: undefined, |
| 102 | + value: undefined, |
| 103 | + defaultValue: undefined, |
| 104 | + referenceDate: undefined, |
| 105 | + expectedTimezone: 'default', |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments