Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Fix issue of not resetting to first page when applying filters (#115)
Browse files Browse the repository at this point in the history
* Reset to first page when filter change

* Add test case
  • Loading branch information
ohltyler authored May 8, 2020
1 parent 253da70 commit 3875087
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
9 changes: 7 additions & 2 deletions public/pages/DetectorsList/List/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export const DetectorList = (props: ListProps) => {
const searchText = e.target.value;
setState({
...state,
page: 0,
queryParams: {
...state.queryParams,
search: searchText,
Expand All @@ -181,6 +182,10 @@ export const DetectorList = (props: ListProps) => {
const sanitizedQuery = sanitizeSearchText(searchValue);
setIndexQuery(sanitizedQuery);
await dispatch(getPrioritizedIndices(sanitizedQuery));
setState(state => ({
...state,
page: 0,
}));
}
}, 300);

Expand All @@ -195,6 +200,7 @@ export const DetectorList = (props: ListProps) => {
: options.map(option => option.label as DETECTOR_STATE);
setState(state => ({
...state,
page: 0,
selectedDetectorStates: states,
}));
};
Expand All @@ -209,6 +215,7 @@ export const DetectorList = (props: ListProps) => {

setState({
...state,
page: 0,
selectedIndices: indices,
});
};
Expand All @@ -234,8 +241,6 @@ export const DetectorList = (props: ListProps) => {
state.selectedDetectorStates,
state.queryParams.sortField,
state.queryParams.sortDirection,
state.queryParams.size,
state.page
);

// get detectors to display based on this page
Expand Down
41 changes: 41 additions & 0 deletions public/pages/DetectorsList/List/__tests__/List.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,47 @@ describe('<ListControls /> spec', () => {
expect(queryByText('detector_name_0')).toBeNull();
expect(queryByText('index_0')).toBeNull();
});
test('should reset to first page when filtering', async () => {
const randomDetectors = new Array(40).fill(null).map((_, index) => {
const hasAnomaly = Math.random() > 0.5;
return {
id: `detector_id_${index}`,
indices: [`index_${index}`],
name: `detector_name_${index}`,
totalAnomalies: hasAnomaly ? Math.floor(Math.random() * 10) : 0,
lastActiveAnomaly: hasAnomaly ? Date.now() + index : 0,
};
});
httpClientMock.get = jest.fn().mockResolvedValue({
data: {
ok: true,
response: {
detectorList: randomDetectors,
totalDetectors: randomDetectors.length,
},
},
});

const { getByText, getByPlaceholderText, queryByText, getAllByTestId } = renderWithRouter(
{
...initialDetectorsState,
requesting: true,
}
);
// Initial load, only first 20 items
await wait();
getByText('detector_name_0')

// Go to next page
userEvent.click(getAllByTestId('pagination-button-next')[0]);
await wait();

// Search for detector which is on prev page
userEvent.type(getByPlaceholderText('Search'), 'detector_name_0');
await wait();
getByText('detector_name_0');
expect(queryByText('detector_name_30')).toBeNull();
});
test('should display rows if there are data', async () => {
const tempAnomalyTime = moment('2019-10-19T09:00:00');
const tempLastUpdateTime = moment('2019-10-19T07:00:00');
Expand Down
4 changes: 1 addition & 3 deletions public/pages/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ export const filterAndSortDetectors = (
selectedIndices: string[],
selectedDetectorStates: DETECTOR_STATE[],
sortField: string,
sortDirection: string,
size: number,
page: number
sortDirection: string
) => {
let filteredBySearch =
search == ''
Expand Down

0 comments on commit 3875087

Please sign in to comment.