|
| 1 | +import React from 'react' |
| 2 | + |
| 3 | +import DateRangePicker from './DateRangePicker' |
| 4 | +import {convertTimeRangeToCustom} from 'src/shared/utils/duration' |
| 5 | +import {pastHourTimeRange} from 'src/shared/constants/timeRanges' |
| 6 | +import {renderWithReduxAndRouter} from 'src/mockState' |
| 7 | + |
| 8 | +import {screen} from '@testing-library/dom' |
| 9 | +import {fireEvent} from '@testing-library/react' |
| 10 | + |
| 11 | +jest.useFakeTimers().setSystemTime(new Date('2022-03-10 20:00:00').getTime()) |
| 12 | + |
| 13 | +describe('Date Range Picker', function() { |
| 14 | + it('should have input values in the correct format after user changes the date selection', () => { |
| 15 | + const {getAllByText} = renderWithReduxAndRouter( |
| 16 | + <DateRangePicker |
| 17 | + timeRange={convertTimeRangeToCustom(pastHourTimeRange)} |
| 18 | + onSetTimeRange={() => {}} |
| 19 | + onClose={() => {}} |
| 20 | + position={{position: 'relative'}} |
| 21 | + /> |
| 22 | + ) |
| 23 | + |
| 24 | + // YYYY-MM-DD HH:mm format by default |
| 25 | + expect(screen.getByTitle('Start')).toHaveValue('2022-03-10 19:00:00') |
| 26 | + expect(screen.getByTitle('Stop')).toHaveValue('2022-03-10 20:00:00') |
| 27 | + |
| 28 | + // change the time and the format should stay the same |
| 29 | + |
| 30 | + const lowerTime = getAllByText('18:00')[0] |
| 31 | + const upperTime = getAllByText('21:00')[1] |
| 32 | + |
| 33 | + fireEvent.click(lowerTime) |
| 34 | + fireEvent.click(upperTime) |
| 35 | + |
| 36 | + expect(screen.getByTitle('Start')).toHaveValue('2022-03-10 18:00:00') |
| 37 | + expect(screen.getByTitle('Stop')).toHaveValue('2022-03-10 21:00:00') |
| 38 | + }) |
| 39 | + |
| 40 | + it('should have the `Apply Time Range` Button disabled on invalid input', () => { |
| 41 | + const {getAllByTestId, getByTestId} = renderWithReduxAndRouter( |
| 42 | + <DateRangePicker |
| 43 | + timeRange={convertTimeRangeToCustom(pastHourTimeRange)} |
| 44 | + onSetTimeRange={() => {}} |
| 45 | + onClose={() => {}} |
| 46 | + position={{position: 'relative'}} |
| 47 | + /> |
| 48 | + ) |
| 49 | + const lowerInput = getAllByTestId('timerange--input')[0] |
| 50 | + |
| 51 | + fireEvent.change(lowerInput, {target: {value: 'iampumpkinthecat;)'}}) |
| 52 | + |
| 53 | + const applyButton = getByTestId('daterange--apply-btn') |
| 54 | + |
| 55 | + expect(applyButton).toBeDisabled() |
| 56 | + }) |
| 57 | +}) |
0 commit comments