Files
dbx/packages/app-tests/dataGridPagination.test.ts
T
Abeautifulsnow 0901f176b0 fix(grid): preserve current page on data refresh instead of resetting to page 1
* @
fix(grid): preserve current page on data refresh instead of resetting to page 1

When refreshing table data (toolbar button, Ctrl+R, or auto-refresh), the
offset was hardcoded to 0, causing pagination to always jump back to the
first page. This was frustrating for users viewing later pages.

Use the current page offset (currentPage - 1) * pageSize so the user
stays on the same page after refresh. In infinite-scroll mode, the
offset still evaluates to 0 because resetInfiniteScrollState() sets
currentPage = 1, so behavior is unchanged.
@

* @
fix(grid): auto-redirect to last page when refreshed page no longer exists

Extends the refresh pagination fix with a safety net: when data is deleted
while viewing a later page (e.g. page 5 with only 2 pages remaining), the
grid auto-navigates to the last available page instead of showing an empty
page.

Uses a transient isRefreshingData flag to distinguish refresh/rollback
from normal pagination navigation, avoiding false triggers.
@

* Revert "@"

This reverts commit ff0bf0bb31.

* @
fix(grid): auto-redirect to last page when refreshed page no longer exists

When data is deleted while viewing a later page, the grid now auto-navigates
to the last available page instead of showing an empty page.

Uses a transient isRefreshingData flag to distinguish refresh/rollback
from normal pagination, and a loading-transition watcher (true->false)
placed after displayedTotalRowCount declaration to avoid TDZ errors.
@

* @
test(grid): add pagination offset-preservation and auto-redirect tests

Cover the two new behaviors added to DataGrid.vue:

1. dataGridPagination.test.ts — auto-redirect page calculation:
   - page beyond last page after data deletion → triggers redirect
   - page still valid → no redirect
   - fewer rows than one page → redirects to page 1
   - total=0 / total=undefined → guard prevents redirect

2. useDataGridActions.test.ts — offset preserved on reload:
   - onReloadData(offset=400) passes offset=400 to build-table-select-sql
   - resultPageOffset stored as 400 (not reset to 0)
@
2026-07-08 13:28:36 +08:00

94 lines
3.2 KiB
TypeScript
Vendored

import { strict as assert } from "node:assert";
import { test } from "vitest";
import { canGoNextDataGridPage } from "../../apps/desktop/src/lib/dataGrid/dataGridPagination.ts";
test("known total disables next page at the last exact page", () => {
assert.equal(
canGoNextDataGridPage({
rowCount: 1,
pageSize: 1,
pageOffset: 8,
totalRowCount: 9,
}),
false,
);
});
test("known total allows next page before the last page", () => {
assert.equal(
canGoNextDataGridPage({
rowCount: 1,
pageSize: 1,
pageOffset: 7,
totalRowCount: 9,
}),
true,
);
});
test("backend hasMore takes precedence over a stale known total", () => {
assert.equal(
canGoNextDataGridPage({
hasMore: true,
rowCount: 1,
pageSize: 1,
pageOffset: 8,
totalRowCount: 9,
}),
true,
);
});
test("unknown total falls back to full-page heuristic", () => {
assert.equal(canGoNextDataGridPage({ rowCount: 1, pageSize: 1 }), true);
assert.equal(canGoNextDataGridPage({ rowCount: 0, pageSize: 1 }), false);
});
// --- auto-redirect page calculation after refresh ---
// These tests document the math used in DataGrid.vue's loading watcher:
// lastPageNum = Math.max(1, Math.ceil(total / pageSize))
// redirect when currentPage > lastPageNum
test("auto-redirect: current page beyond last page after data deletion — should redirect", () => {
// user on page 5, data shrinks to 200 rows, pageSize=100 → last page=2
const total = 200;
const pageSize = 100;
const currentPage = 5;
const lastPageNum = Math.max(1, Math.ceil(total / pageSize));
assert.equal(lastPageNum, 2);
assert.equal(currentPage > lastPageNum, true, "redirect should be triggered");
assert.equal((lastPageNum - 1) * pageSize, 100, "paginate offset for last page should be 100");
});
test("auto-redirect: current page still valid after partial deletion — no redirect", () => {
// user on page 5, data still has 500 rows → last page stays 5
const total = 500;
const pageSize = 100;
const currentPage = 5;
const lastPageNum = Math.max(1, Math.ceil(total / pageSize));
assert.equal(lastPageNum, 5);
assert.equal(currentPage > lastPageNum, false, "no redirect should be triggered");
});
test("auto-redirect: fewer rows than one page — redirects to page 1", () => {
// user on page 3, data shrinks to 30 rows, pageSize=100 → last page=1
const total = 30;
const pageSize = 100;
const currentPage = 3;
const lastPageNum = Math.max(1, Math.ceil(total / pageSize));
assert.equal(lastPageNum, 1, "ceil(30/100)=1, max(1,1)=1");
assert.equal(currentPage > lastPageNum, true, "redirect should be triggered");
assert.equal((lastPageNum - 1) * pageSize, 0, "paginate offset for page 1 should be 0");
});
test("auto-redirect: total is zero — guard prevents redirect attempt", () => {
// When total=0, the '!total || total <= 0' guard fires and skips the redirect
const total = 0;
assert.equal(!total || total <= 0, true, "guard should prevent redirect when total is 0");
});
test("auto-redirect: total is undefined — guard prevents redirect attempt", () => {
const total = undefined;
assert.equal(!total || (total as any) <= 0, true, "guard should prevent redirect when total is unknown");
});