Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomedina248 committed Mar 9, 2022
1 parent e90f8fd commit 2b1922a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { buildNativeFilter } from 'spec/fixtures/mockNativeFilters';
import { act, fireEvent, render, screen } from 'spec/helpers/testing-library';
import FilterConfigPane from './FilterConfigurePane';

const scrollMock = jest.fn();
Element.prototype.scroll = scrollMock;

const defaultProps = {
children: jest.fn(),
getFilterTitle: (id: string) => id,
Expand Down Expand Up @@ -56,6 +59,10 @@ function defaultRender(initialState: any = defaultState, props = defaultProps) {
});
}

beforeEach(() => {
scrollMock.mockClear();
});

test('renders form', async () => {
await act(async () => {
defaultRender();
Expand All @@ -65,7 +72,7 @@ test('renders form', async () => {

test('drag and drop', async () => {
defaultRender();
// Drag the state and contry filter above the product filter
// Drag the state and country filter above the product filter
const [countryStateFilter, productFilter] = document.querySelectorAll(
'div[draggable=true]',
);
Expand Down Expand Up @@ -132,3 +139,41 @@ test('add divider', async () => {
});
expect(defaultProps.onAdd).toHaveBeenCalledWith('DIVIDER');
});

test('filter container should scroll to bottom when adding items', async () => {
const state = {
dashboardInfo: {
metadata: {
native_filter_configuration: new Array(35)
.fill(0)
.map((_, index) =>
buildNativeFilter(`NATIVE_FILTER-${index}`, `filter-${index}`, []),
),
},
},
dashboardLayout,
};
const props = {
...defaultProps,
filters: new Array(35).fill(0).map((_, index) => `NATIVE_FILTER-${index}`),
};

defaultRender(state, props);

const addButton = screen.getByText('Add filters and dividers')!;
fireEvent.mouseOver(addButton);

const addFilterButton = await screen.findByText('Filter');
await act(async () => {
fireEvent(
addFilterButton,
new MouseEvent('click', {
bubbles: true,
cancelable: true,
}),
);
});

const containerElement = screen.getByTestId('filter-title-container');
expect(containerElement.scroll).toHaveBeenCalled();
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const TitlesContainer = styled.div`
border-right: 1px solid ${({ theme }) => theme.colors.grayscale.light2};
`;

const FiltureConfigurePane: React.FC<Props> = ({
const FilterConfigurePane: React.FC<Props> = ({
getFilterTitle,
onChange,
onRemove,
Expand Down Expand Up @@ -98,4 +98,4 @@ const FiltureConfigurePane: React.FC<Props> = ({
);
};

export default FiltureConfigurePane;
export default FilterConfigurePane;
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ const FilterTitleContainer = forwardRef<HTMLDivElement, Props>(
return items;
};

return <Container ref={ref}>{renderFilterGroups()}</Container>;
return (
<Container data-test="filter-title-container" ref={ref}>
{renderFilterGroups()}
</Container>
);
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ const FilterTitlePane: React.FC<Props> = ({
navList.scrollTop = navList.scrollHeight;
}

if (filtersContainerRef?.current) {
filtersContainerRef.current.scroll({
top: filtersContainerRef.current.scrollHeight,
behavior: 'smooth',
});
}
filtersContainerRef?.current?.scroll?.({
top: filtersContainerRef.current.scrollHeight,
behavior: 'smooth',
});
}, 0);
};
const menu = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import ErrorBoundary from 'src/components/ErrorBoundary';
import { StyledModal } from 'src/components/Modal';
import { testWithId } from 'src/utils/testUtils';
import { useFilterConfigMap, useFilterConfiguration } from '../state';
import FiltureConfigurePane from './FilterConfigurePane';
import FilterConfigurePane from './FilterConfigurePane';
import FiltersConfigForm, {
FilterPanels,
} from './FiltersConfigForm/FiltersConfigForm';
Expand Down Expand Up @@ -522,7 +522,7 @@ export function FiltersConfigModal({
onValuesChange={onValuesChange}
layout="vertical"
>
<FiltureConfigurePane
<FilterConfigurePane
erroredFilters={erroredFilters}
onRemove={handleRemoveItem}
onAdd={addFilter}
Expand All @@ -535,7 +535,7 @@ export function FiltersConfigModal({
filters={orderedFilters}
>
{(id: string) => getForm(id)}
</FiltureConfigurePane>
</FilterConfigurePane>
</StyledForm>
</StyledModalBody>
</ErrorBoundary>
Expand Down

0 comments on commit 2b1922a

Please sign in to comment.