Merge pull request #1079 from prymitive/fix-pagination

fix(ui): reset pagination if needed
This commit is contained in:
Łukasz Mierzwa
2019-10-27 10:03:54 +00:00
committed by GitHub
2 changed files with 35 additions and 1 deletions

View File

@@ -110,6 +110,10 @@ const Browser = observer(
this.dataSource.silences = result;
this.dataSource.setDone();
this.dataSource.setError(null);
this.pagination.resetIfNeeded(
this.dataSource.silences.length,
this.maxPerPage
);
})
.catch(err => {
console.trace(err);
@@ -129,10 +133,17 @@ const Browser = observer(
activePage: 1,
onPageChange(pageNumber) {
this.activePage = pageNumber;
},
resetIfNeeded(totalItemsCount, maxPerPage) {
const totalPages = Math.ceil(totalItemsCount / maxPerPage);
if (this.activePage > totalPages) {
this.activePage = totalPages;
}
}
},
{
onPageChange: action.bound
onPageChange: action.bound,
resetIfNeeded: action.bound
}
);

View File

@@ -203,6 +203,29 @@ describe("<Browser />", () => {
expect(tree.find("ManagedSilence")).toHaveLength(1);
});
it("resetes pagination to last page on truncation", async () => {
fetch.mockResponseOnce(JSON.stringify(MockSilenceList(11)));
const tree = MountedBrowser();
const instance = tree.instance();
await expect(instance.dataSource.fetch).resolves.toBeUndefined();
tree.update();
expect(instance.pagination.activePage).toBe(1);
const pageLink = tree.find(".page-link").at(3);
pageLink.simulate("click");
tree.update();
expect(tree.find("ManagedSilence")).toHaveLength(1);
expect(instance.pagination.activePage).toBe(3);
fetch.mockResponseOnce(JSON.stringify(MockSilenceList(7)));
instance.onFetch();
await expect(instance.dataSource.fetch).resolves.toBeUndefined();
tree.update();
expect(tree.find("ManagedSilence")).toHaveLength(2);
expect(instance.pagination.activePage).toBe(2);
});
it("renders error after failed fetch", async () => {
jest.spyOn(console, "trace").mockImplementation(() => {});
fetch.mockReject("fake failure");