Skip to content

Commit

Permalink
fixing pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
flacoman91 committed Nov 7, 2024
1 parent 0141d8b commit 76188d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/components/Dialogs/DataExport/DataExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export const DataExport = () => {
const filtersState = useSelector(selectFiltersRoot);
const tab = useSelector(selectViewTab);
const { data } = useGetAggregations();
const someComplaintsCount = data.total;
const allComplaintsCount = data.doc_count;
const someComplaintsCount = data?.total || 0;
const allComplaintsCount = data?.doc_count || 0;

// can only be full or filtered
const [dataset, setDataset] = useState(DATASET_FULL);
Expand Down
45 changes: 24 additions & 21 deletions src/components/List/Pagination/Pagination.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { queryState } from '../../../reducers/query/querySlice';
import { resultsState } from '../../../reducers/results/resultsSlice';
import {
testRender as render,
screen,
Expand All @@ -8,57 +7,61 @@ import {
import { merge } from '../../../testUtils/functionHelpers';
import { Pagination } from './Pagination';
import * as pagingActions from '../../../reducers/query/querySlice';
import fetchMock from 'jest-fetch-mock';
import { listResponseP1, listResponseP2 } from './fixture';
import { MODE_LIST } from '../../../constants';

describe('Pagination', () => {
const renderComponent = (newQueryState, newResultsState) => {
const renderComponent = (newQueryState) => {
merge(newQueryState, queryState);
merge(newResultsState, resultsState);

const data = {
query: newQueryState,
results: newResultsState,
routes: { queryString: '?sdfsda' },
view: { tab: MODE_LIST },
};

render(<Pagination />, {
preloadedState: data,
});
};

test('nextPageShown dispatched when Next button clicked', () => {
beforeEach(() => {
fetchMock.resetMocks();
});

test('nextPageShown dispatched when Next button clicked', async () => {
const nextPageShownSpy = jest
.spyOn(pagingActions, 'nextPageShown')
.mockImplementation(() => jest.fn());
const newQueryState = {
fetchMock.mockResponseOnce(JSON.stringify(listResponseP1));
renderComponent({
page: 1,
totalPages: 5,
};

renderComponent(newQueryState, { items: [1, 3, 4, 5] });
});
await screen.findByText('Page 1');
expect(screen.getByRole('button', { name: /Previous/ })).toBeDisabled();
fireEvent.click(screen.getByRole('button', { name: /Next/ }));

expect(nextPageShownSpy).toBeCalledTimes(1);
});

test('prevPageShown dispatched when Previous button clicked', () => {
test('prevPageShown dispatched when Previous button clicked', async () => {
const prevPageShownSpy = jest
.spyOn(pagingActions, 'prevPageShown')
.mockImplementation(() => jest.fn());
const newQueryState = {
fetchMock.mockResponseOnce(JSON.stringify(listResponseP2));

renderComponent({
page: 2,
totalPages: 5,
};
});

await screen.findByText('Page 2');

renderComponent(newQueryState, { items: [1, 3, 4, 5] });
fireEvent.click(screen.getByRole('button', { name: /Previous/ }));

expect(prevPageShownSpy).toBeCalledTimes(1);
});

test('hides when there are no results', () => {
const newQueryState = { page: 1, totalPages: 1 };

renderComponent(newQueryState, { items: [] });

renderComponent({ page: 1 });
expect(screen.queryByRole('button', { name: /Next/ })).toBeNull();
expect(screen.queryByRole('button', { name: /Previous/ })).toBeNull();
});
Expand Down
2 changes: 2 additions & 0 deletions src/components/List/Pagination/fixture.js

Large diffs are not rendered by default.

0 comments on commit 76188d4

Please sign in to comment.