-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bc42bf
commit 657fe35
Showing
14 changed files
with
440 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 14 additions & 14 deletions
28
src/views/domain-workflows/config/domain-workflows-archival-filters.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
...rkflows-archival-filters-dates/__tests__/domain-workflows-archival-filters-dates.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import React from 'react'; | ||
|
||
import { render, screen, act, fireEvent } from '@/test-utils/rtl'; | ||
|
||
import { | ||
mockDomainWorkflowsQueryParamsValues, | ||
mockDateOverridesArchival, | ||
} from '../../__fixtures__/domain-workflows-query-params'; | ||
import DomainWorkflowsArchivalFiltersDates from '../domain-workflows-archival-filters-dates'; | ||
import { type DomainWorkflowsArchivalFiltersDatesValue } from '../domain-workflows-archival-filters-dates.types'; | ||
|
||
jest.useFakeTimers().setSystemTime(new Date('2023-05-25')); | ||
|
||
jest.mock( | ||
'../../domain-workflows-filters-dates/domain-workflows-filters-dates.constants', | ||
() => ({ | ||
...jest.requireActual( | ||
'../../domain-workflows-filters-dates/domain-workflows-filters-dates.constants' | ||
), | ||
DATE_FORMAT: 'dd MMM yyyy, HH:mm x', | ||
}) | ||
); | ||
|
||
describe('DomainWorkflowsArchivalFiltersDates', () => { | ||
it('displays the date picker component', () => { | ||
setup({}); | ||
expect( | ||
screen.getByPlaceholderText('Select time range') | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders without errors when dates are already provided in query params', () => { | ||
setup({ | ||
overrides: mockDateOverridesArchival, | ||
}); | ||
expect( | ||
// TODO - set timezone config for unit tests to UTC | ||
screen.getByDisplayValue( | ||
'23 May 2023, 00:00 +00 – 24 May 2023, 00:00 +00' | ||
) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('sets query params when date is set', () => { | ||
const { mockSetValue } = setup({}); | ||
const datePicker = screen.getByPlaceholderText('Select time range'); | ||
act(() => { | ||
fireEvent.change(datePicker, { | ||
target: { value: '13 May 2023, 00:00 +00 – 14 May 2023, 00:00 +00' }, | ||
}); | ||
}); | ||
|
||
expect(mockSetValue).toHaveBeenCalledWith({ | ||
timeRangeStartArchival: new Date('2023-05-13T00:00:00.000Z'), | ||
timeRangeEndArchival: new Date('2023-05-14T00:00:00.000Z'), | ||
}); | ||
}); | ||
|
||
it('resets to previous date when one date is selected and then the modal is closed', () => { | ||
const { mockSetValue } = setup({ | ||
overrides: mockDateOverridesArchival, | ||
}); | ||
const datePicker = screen.getByPlaceholderText('Select time range'); | ||
|
||
act(() => { | ||
fireEvent.focus(datePicker); | ||
}); | ||
|
||
const timeRangeStartLabel = screen.getByLabelText( | ||
"Choose Saturday, May 13th 2023. It's available." | ||
); | ||
|
||
act(() => { | ||
fireEvent.click(timeRangeStartLabel); | ||
}); | ||
|
||
screen.getByText( | ||
'Selected date is 13 May 2023, 00:00 +00. Select the second date.' | ||
); | ||
|
||
act(() => { | ||
fireEvent.keyDown(datePicker, { keyCode: 9 }); | ||
}); | ||
|
||
expect(datePicker).toHaveValue( | ||
'23 May 2023, 00:00 +00 – 24 May 2023, 00:00 +00' | ||
); | ||
expect(mockSetValue).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('resets to empty state when one date is selected and then the modal is closed', () => { | ||
const { mockSetValue } = setup({}); | ||
const datePicker = screen.getByPlaceholderText('Select time range'); | ||
|
||
act(() => { | ||
fireEvent.focus(datePicker); | ||
}); | ||
|
||
const timeRangeStartLabel = screen.getByLabelText( | ||
"Choose Saturday, May 13th 2023. It's available." | ||
); | ||
|
||
act(() => { | ||
fireEvent.click(timeRangeStartLabel); | ||
}); | ||
|
||
screen.getByText( | ||
'Selected date is 13 May 2023, 00:00 +00. Select the second date.' | ||
); | ||
|
||
act(() => { | ||
fireEvent.keyDown(datePicker, { keyCode: 9 }); | ||
}); | ||
|
||
expect(datePicker).toHaveValue(''); | ||
expect(mockSetValue).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('clears the date when the clear button is clicked', () => { | ||
const { mockSetValue } = setup({ | ||
overrides: mockDateOverridesArchival, | ||
}); | ||
const clearButton = screen.getByLabelText('Clear value'); | ||
act(() => { | ||
fireEvent.click(clearButton); | ||
}); | ||
|
||
expect(mockSetValue).toHaveBeenCalledWith({ | ||
timeRangeStartArchival: undefined, | ||
timeRangeEndArchival: undefined, | ||
}); | ||
}); | ||
}); | ||
|
||
function setup({ | ||
overrides, | ||
}: { | ||
overrides?: DomainWorkflowsArchivalFiltersDatesValue; | ||
}) { | ||
const mockSetValue = jest.fn(); | ||
render( | ||
<DomainWorkflowsArchivalFiltersDates | ||
value={{ | ||
timeRangeStartArchival: | ||
mockDomainWorkflowsQueryParamsValues.timeRangeStart, | ||
timeRangeEndArchival: mockDomainWorkflowsQueryParamsValues.timeRangeEnd, | ||
...overrides, | ||
}} | ||
setValue={mockSetValue} | ||
/> | ||
); | ||
|
||
return { mockSetValue }; | ||
} |
18 changes: 18 additions & 0 deletions
18
...domain-workflows-archival-filters-dates/domain-workflows-archival-filters-dates.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { type Theme } from 'baseui'; | ||
import type { FormControlOverrides } from 'baseui/form-control/types'; | ||
import { type StyleObject } from 'styletron-react'; | ||
|
||
export const overrides = { | ||
dateFormControl: { | ||
Label: { | ||
style: ({ $theme }: { $theme: Theme }): StyleObject => ({ | ||
...$theme.typography.LabelXSmall, | ||
}), | ||
}, | ||
ControlContainer: { | ||
style: (): StyleObject => ({ | ||
margin: '0px', | ||
}), | ||
}, | ||
} satisfies FormControlOverrides, | ||
}; |
76 changes: 76 additions & 0 deletions
76
...flows/domain-workflows-archival-filters-dates/domain-workflows-archival-filters-dates.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
|
||
import { DatePicker } from 'baseui/datepicker'; | ||
import { FormControl } from 'baseui/form-control'; | ||
import { SIZE } from 'baseui/input'; | ||
|
||
import { type PageFilterComponentProps } from '@/components/page-filters/page-filters.types'; | ||
|
||
import { DATE_FORMAT } from '../domain-workflows-filters-dates/domain-workflows-filters-dates.constants'; | ||
|
||
import { overrides } from './domain-workflows-archival-filters-dates.styles'; | ||
import { type DomainWorkflowsArchivalFiltersDatesValue } from './domain-workflows-archival-filters-dates.types'; | ||
|
||
export default function DomainWorkflowsArchivalFiltersDates({ | ||
value, | ||
setValue, | ||
}: PageFilterComponentProps<DomainWorkflowsArchivalFiltersDatesValue>) { | ||
const [dates, setDates] = React.useState<Array<Date | null | undefined>>([]); | ||
|
||
React.useEffect(() => { | ||
setDates( | ||
Boolean(value.timeRangeStartArchival) && | ||
Boolean(value.timeRangeEndArchival) | ||
? [value.timeRangeStartArchival, value.timeRangeEndArchival] | ||
: [] | ||
); | ||
}, [value]); | ||
|
||
return ( | ||
<FormControl label="Dates" overrides={overrides.dateFormControl}> | ||
<DatePicker | ||
value={dates} | ||
onChange={({ date }) => { | ||
if (!date || !Array.isArray(date)) { | ||
return; | ||
} | ||
setDates(date); | ||
if (date.length === 0) { | ||
setValue({ | ||
timeRangeStartArchival: undefined, | ||
timeRangeEndArchival: undefined, | ||
}); | ||
} else if (date.length === 2) { | ||
const [start, end] = date; | ||
if (!start || !end) { | ||
return; | ||
} | ||
setValue({ | ||
timeRangeStartArchival: start, | ||
timeRangeEndArchival: end, | ||
}); | ||
} | ||
}} | ||
onClose={() => { | ||
if (dates.length !== 2 || dates.some((date) => !date)) { | ||
setDates( | ||
Boolean(value.timeRangeStartArchival) && | ||
Boolean(value.timeRangeEndArchival) | ||
? [value.timeRangeStartArchival, value.timeRangeEndArchival] | ||
: [] | ||
); | ||
} | ||
}} | ||
placeholder="Select time range" | ||
formatString={DATE_FORMAT} | ||
size={SIZE.compact} | ||
quickSelect | ||
range | ||
clearable | ||
timeSelectStart | ||
timeSelectEnd | ||
/> | ||
</FormControl> | ||
); | ||
} |
4 changes: 4 additions & 0 deletions
4
.../domain-workflows-archival-filters-dates/domain-workflows-archival-filters-dates.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type DomainWorkflowsArchivalFiltersDatesValue = { | ||
timeRangeStartArchival: Date | undefined; | ||
timeRangeEndArchival: Date | undefined; | ||
}; |
Oops, something went wrong.