Fix Cancel button behavior on TopFilterAndSort bar (#106)
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import { fireEvent, render } from '@testing-library/react';
|
import { fireEvent, render, waitFor } from '@testing-library/react';
|
||||||
|
|
||||||
import { EditableDateStory } from '../__stories__/EditableDate.stories';
|
import { EditableDateStory } from '../__stories__/EditableDate.stories';
|
||||||
import { act } from 'react-dom/test-utils';
|
import { act } from 'react-dom/test-utils';
|
||||||
@ -19,19 +19,18 @@ it('Checks the EditableDate editing event bubbles up', async () => {
|
|||||||
if (!wrapper) {
|
if (!wrapper) {
|
||||||
throw new Error('Cell Wrapper not found');
|
throw new Error('Cell Wrapper not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
act(() => {
|
act(() => {
|
||||||
fireEvent.click(wrapper);
|
fireEvent.click(wrapper);
|
||||||
|
const dateDisplay = parent.querySelector('div');
|
||||||
|
if (!dateDisplay) {
|
||||||
|
throw new Error('Editable input not found');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
waitFor(() => {
|
||||||
|
expect(getByText('March 2021')).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
const dateDisplay = parent.querySelector('div');
|
fireEvent.click(getByText('5'));
|
||||||
|
|
||||||
if (!dateDisplay) {
|
|
||||||
throw new Error('Editable input not found');
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(getByText('March 2021')).toBeInTheDocument();
|
|
||||||
act(() => {
|
|
||||||
fireEvent.click(getByText('5'));
|
|
||||||
});
|
|
||||||
expect(changeHandler).toHaveBeenCalledWith(new Date('2021-03-05'));
|
expect(changeHandler).toHaveBeenCalledWith(new Date('2021-03-05'));
|
||||||
});
|
});
|
||||||
|
|||||||
@ -10,6 +10,7 @@ type OwnProps<SortField, FilterProperties> = {
|
|||||||
onRemoveFilter: (
|
onRemoveFilter: (
|
||||||
filterId: SelectedFilterType<FilterProperties>['key'],
|
filterId: SelectedFilterType<FilterProperties>['key'],
|
||||||
) => void;
|
) => void;
|
||||||
|
onCancelClick: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const StyledBar = styled.div`
|
const StyledBar = styled.div`
|
||||||
@ -46,6 +47,7 @@ function SortAndFilterBar<SortField, FilterProperties>({
|
|||||||
onRemoveSort,
|
onRemoveSort,
|
||||||
filters,
|
filters,
|
||||||
onRemoveFilter,
|
onRemoveFilter,
|
||||||
|
onCancelClick,
|
||||||
}: OwnProps<SortField, FilterProperties>) {
|
}: OwnProps<SortField, FilterProperties>) {
|
||||||
return (
|
return (
|
||||||
<StyledBar>
|
<StyledBar>
|
||||||
@ -75,10 +77,7 @@ function SortAndFilterBar<SortField, FilterProperties>({
|
|||||||
{filters.length + sorts.length > 0 && (
|
{filters.length + sorts.length > 0 && (
|
||||||
<StyledCancelButton
|
<StyledCancelButton
|
||||||
data-testid={'cancel-button'}
|
data-testid={'cancel-button'}
|
||||||
onClick={() => {
|
onClick={onCancelClick}
|
||||||
sorts.forEach((i) => onRemoveSort(i.key));
|
|
||||||
filters.forEach((i) => onRemoveFilter(i.key));
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
</StyledCancelButton>
|
</StyledCancelButton>
|
||||||
|
|||||||
@ -157,6 +157,12 @@ function TableHeader<SortField, FilterProperties>({
|
|||||||
filters={filters}
|
filters={filters}
|
||||||
onRemoveSort={sortUnselect}
|
onRemoveSort={sortUnselect}
|
||||||
onRemoveFilter={filterUnselect}
|
onRemoveFilter={filterUnselect}
|
||||||
|
onCancelClick={() => {
|
||||||
|
innerSetFilters([]);
|
||||||
|
onFiltersUpdate && onFiltersUpdate([]);
|
||||||
|
innerSetSorts([]);
|
||||||
|
onSortsUpdate && onSortsUpdate([]);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</StyledContainer>
|
</StyledContainer>
|
||||||
|
|||||||
@ -13,9 +13,13 @@ export default component;
|
|||||||
|
|
||||||
type OwnProps = {
|
type OwnProps = {
|
||||||
removeFunction: () => void;
|
removeFunction: () => void;
|
||||||
|
cancelFunction: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const RegularSortAndFilterBar = ({ removeFunction }: OwnProps) => {
|
export const RegularSortAndFilterBar = ({
|
||||||
|
removeFunction,
|
||||||
|
cancelFunction,
|
||||||
|
}: OwnProps) => {
|
||||||
return (
|
return (
|
||||||
<ThemeProvider theme={lightTheme}>
|
<ThemeProvider theme={lightTheme}>
|
||||||
<SortAndFilterBar
|
<SortAndFilterBar
|
||||||
@ -37,6 +41,7 @@ export const RegularSortAndFilterBar = ({ removeFunction }: OwnProps) => {
|
|||||||
]}
|
]}
|
||||||
onRemoveSort={removeFunction}
|
onRemoveSort={removeFunction}
|
||||||
onRemoveFilter={removeFunction}
|
onRemoveFilter={removeFunction}
|
||||||
|
onCancelClick={cancelFunction}
|
||||||
filters={[
|
filters={[
|
||||||
{
|
{
|
||||||
label: 'People',
|
label: 'People',
|
||||||
|
|||||||
@ -4,8 +4,13 @@ import { RegularSortAndFilterBar } from '../__stories__/SortAndFilterBar.stories
|
|||||||
|
|
||||||
it('Checks the SortAndFilterBar renders', async () => {
|
it('Checks the SortAndFilterBar renders', async () => {
|
||||||
const removeFunction = jest.fn();
|
const removeFunction = jest.fn();
|
||||||
|
const cancelFunction = jest.fn();
|
||||||
|
|
||||||
const { getByText, getByTestId } = render(
|
const { getByText, getByTestId } = render(
|
||||||
<RegularSortAndFilterBar removeFunction={removeFunction} />,
|
<RegularSortAndFilterBar
|
||||||
|
removeFunction={removeFunction}
|
||||||
|
cancelFunction={cancelFunction}
|
||||||
|
/>,
|
||||||
);
|
);
|
||||||
expect(getByText('Test sort')).toBeDefined();
|
expect(getByText('Test sort')).toBeDefined();
|
||||||
|
|
||||||
@ -17,11 +22,15 @@ it('Checks the SortAndFilterBar renders', async () => {
|
|||||||
|
|
||||||
it('Removes sorts when cancel is pressed', async () => {
|
it('Removes sorts when cancel is pressed', async () => {
|
||||||
const removeFunction = jest.fn();
|
const removeFunction = jest.fn();
|
||||||
|
const cancelFunction = jest.fn();
|
||||||
const { getByTestId } = render(
|
const { getByTestId } = render(
|
||||||
<RegularSortAndFilterBar removeFunction={removeFunction} />,
|
<RegularSortAndFilterBar
|
||||||
|
removeFunction={removeFunction}
|
||||||
|
cancelFunction={cancelFunction}
|
||||||
|
/>,
|
||||||
);
|
);
|
||||||
const cancel = getByTestId('cancel-button');
|
const cancel = getByTestId('cancel-button');
|
||||||
fireEvent.click(cancel);
|
fireEvent.click(cancel);
|
||||||
|
|
||||||
expect(removeFunction).toHaveBeenCalled();
|
expect(cancelFunction).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user