fix(mongodb): preserve infinite-scroll append state
This commit is contained in:
1 parent
0aafdb0238
commit
94949eb1c7
4 files changed
+496
-48
No files matched your search
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, nextTick, watch, onMounted, onBeforeUnmount } from "vue";
|
||||
import { computed, ref, shallowRef, nextTick, watch, onMounted, onBeforeUnmount } from "vue";
|
||||
import { uuid } from "@/lib/common/utils";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { RefreshCw, Trash2, Plus, Save, ChevronDown, ChevronLeft, ChevronRight, Table2, Braces, X, Search, Wrench, Filter, Columns3Cog, SquareDashed, Minus, Rows3, AlignLeft, AlignRight, EyeOff, Palette } from "@lucide/vue";
|
||||
@@ -116,6 +116,13 @@ const DYNAMODB_DEFAULT_EXPORT_ROW_LIMIT = 10_000;
|
||||
|
||||
const documents = ref<JsonRecord[]>([]);
|
||||
const copyDocuments = ref<JsonRecord[]>([]);
|
||||
const gridRows = shallowRef<QueryResult["rows"]>([]);
|
||||
// Set only when the most recent load() appended a continuation segment onto
|
||||
// the existing documents (infinite scroll); undefined for a full replace.
|
||||
// Mirrors QueryResult.appended_from_row_count so DataGrid's infinite-scroll
|
||||
// bookkeeping (see appendQueryResultSegment in queryStore.ts) can tell a
|
||||
// genuine append apart from a stale/failed one.
|
||||
const appendedFromRowCount = ref<number | undefined>(undefined);
|
||||
const mongoCopyDocumentsAvailable = ref(false);
|
||||
const lastGridColumns = ref<string[]>([]);
|
||||
const lastGridColumnTypes = ref<string[]>([]);
|
||||
@@ -354,6 +361,57 @@ const deleteDetails = computed(() => {
|
||||
return t("dangerDialog.mongoFieldDetails", { field: pending.name || t("mongo.field") });
|
||||
});
|
||||
|
||||
function documentGridColumns(documentsToRender: JsonRecord[]): string[] {
|
||||
const keySet = new Set<string>();
|
||||
keySet.add("_id");
|
||||
for (const doc of documentsToRender) {
|
||||
for (const key of Object.keys(doc)) {
|
||||
if (key !== "_id") keySet.add(key);
|
||||
}
|
||||
}
|
||||
return [...keySet];
|
||||
}
|
||||
|
||||
function documentGridRow(doc: JsonRecord, columns: string[], kind: DocumentStoreKind): QueryResult["rows"][number] {
|
||||
return columns.map((column) => {
|
||||
const value = mongoDocumentDisplayValue(doc[column]);
|
||||
if (value === undefined || value === null) return null;
|
||||
if (column === "_id") return kind === "mongodb" ? mongoDocumentIdForGrid(value) : documentStoreValueForGrid(value, kind);
|
||||
if (typeof value === "object") return documentStoreValueForGrid(value, kind);
|
||||
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
|
||||
return String(value);
|
||||
});
|
||||
}
|
||||
|
||||
function sameGridColumns(left: string[], right: string[]): boolean {
|
||||
return left.length === right.length && left.every((column, index) => column === right[index]);
|
||||
}
|
||||
|
||||
function commitLoadedDocuments(nextDocuments: JsonRecord[], nextCopyDocuments: JsonRecord[], hasTypePreservingCopyDocuments: boolean, append: boolean, kind: DocumentStoreKind) {
|
||||
const previousDocumentCount = documents.value.length;
|
||||
const combinedDocuments = append ? [...documents.value, ...nextDocuments] : nextDocuments;
|
||||
const nextColumns = combinedDocuments.length > 0 ? documentGridColumns(combinedDocuments) : lastGridColumns.value;
|
||||
const canAppendGridRows = append && gridRows.value.length === previousDocumentCount && sameGridColumns(lastGridColumns.value, nextColumns);
|
||||
|
||||
documents.value = combinedDocuments;
|
||||
copyDocuments.value = append ? [...copyDocuments.value, ...nextCopyDocuments] : nextCopyDocuments;
|
||||
mongoCopyDocumentsAvailable.value = append ? mongoCopyDocumentsAvailable.value && hasTypePreservingCopyDocuments : hasTypePreservingCopyDocuments;
|
||||
|
||||
if (combinedDocuments.length > 0) {
|
||||
lastGridColumns.value = nextColumns;
|
||||
lastGridColumnTypes.value = kind === "mongodb" ? mongoDocumentGridColumnTypes(combinedDocuments, nextColumns) : [];
|
||||
}
|
||||
|
||||
if (canAppendGridRows) {
|
||||
appendedFromRowCount.value = previousDocumentCount;
|
||||
gridRows.value = [...gridRows.value, ...nextDocuments.map((document) => documentGridRow(document, nextColumns, kind))];
|
||||
return;
|
||||
}
|
||||
|
||||
appendedFromRowCount.value = undefined;
|
||||
gridRows.value = combinedDocuments.map((document) => documentGridRow(document, nextColumns, kind));
|
||||
}
|
||||
|
||||
const gridResult = computed<QueryResult>(() => {
|
||||
const docs = documents.value;
|
||||
if (!docs.length) {
|
||||
@@ -367,28 +425,17 @@ const gridResult = computed<QueryResult>(() => {
|
||||
};
|
||||
}
|
||||
|
||||
const keySet = new Set<string>();
|
||||
keySet.add("_id");
|
||||
for (const doc of docs) {
|
||||
for (const key of Object.keys(doc)) {
|
||||
if (key !== "_id") keySet.add(key);
|
||||
}
|
||||
}
|
||||
const columns = [...keySet];
|
||||
const columnTypes = documentStoreProvider.value.kind === "mongodb" ? mongoDocumentGridColumnTypes(docs, columns) : undefined;
|
||||
|
||||
const rows = docs.map((doc) =>
|
||||
columns.map((col) => {
|
||||
const val = mongoDocumentDisplayValue(doc[col]);
|
||||
if (val === undefined || val === null) return null;
|
||||
if (col === "_id") return documentStoreProvider.value.kind === "mongodb" ? mongoDocumentIdForGrid(val) : documentStoreValueForGrid(val, documentStoreProvider.value.kind);
|
||||
if (typeof val === "object") return documentStoreValueForGrid(val, documentStoreProvider.value.kind);
|
||||
if (typeof val === "string" || typeof val === "number" || typeof val === "boolean") return val;
|
||||
return String(val);
|
||||
}),
|
||||
);
|
||||
|
||||
return { columns, column_types: columnTypes, rows, mongo_documents: docs, mongo_copy_documents: copyDocuments.value, affected_rows: 0, execution_time_ms: 0, truncated: false };
|
||||
return {
|
||||
columns: lastGridColumns.value,
|
||||
column_types: lastGridColumnTypes.value,
|
||||
rows: gridRows.value,
|
||||
mongo_documents: docs,
|
||||
mongo_copy_documents: copyDocuments.value,
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
truncated: false,
|
||||
appended_from_row_count: appendedFromRowCount.value,
|
||||
};
|
||||
});
|
||||
|
||||
async function exportAllDocumentStoreDocuments(onProgress?: (info: { rowsExported: number; totalRows: number | null }) => void): Promise<QueryResult | undefined> {
|
||||
@@ -922,7 +969,7 @@ async function gridSave(changes: DocumentGridChanges) {
|
||||
}
|
||||
|
||||
await api.documentSaveMeilisearchBatch(props.connectionId, props.collection, updates, deleteIds, inserts);
|
||||
await load();
|
||||
await reloadDocumentsAfterMutationOrRefresh();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -994,7 +1041,7 @@ async function gridSave(changes: DocumentGridChanges) {
|
||||
page.value = 0;
|
||||
resetDynamoDbPagination();
|
||||
}
|
||||
await load();
|
||||
await reloadDocumentsAfterMutationOrRefresh();
|
||||
}
|
||||
|
||||
function buildPathIdentityInsertDocument(row: MongoInputValue[], columns: string[], kind: Exclude<DocumentStoreKind, "mongodb">): JsonRecord {
|
||||
@@ -1275,7 +1322,7 @@ function applyElasticsearchSearchTotal(searchTotal: number, isExact: boolean, fi
|
||||
startElasticsearchExactCount(filter);
|
||||
}
|
||||
|
||||
async function load(options: { page?: number } = {}) {
|
||||
async function load(options: { page?: number; append?: boolean; offset?: number; limit?: number } = {}) {
|
||||
if (documentLoadExecutionId.value) void api.cancelQuery(documentLoadExecutionId.value);
|
||||
const requestGeneration = ++documentRequestGeneration;
|
||||
const executionId = uuid();
|
||||
@@ -1309,8 +1356,9 @@ async function load(options: { page?: number } = {}) {
|
||||
if (storeKind === "dynamodb" && requestPage > 0 && !cursor) {
|
||||
throw new Error(t("dynamodb.pageCursorUnavailable"));
|
||||
}
|
||||
const skip = storeKind === "dynamodb" ? 0 : requestPage * pageSize.value;
|
||||
const result = await api.documentFindDocuments(connectionId, database, collection, skip, documentRequestLimit.value, filter, undefined, sort, undefined, executionId, cursor);
|
||||
const skip = storeKind === "dynamodb" ? 0 : (options.offset ?? requestPage * pageSize.value);
|
||||
const requestLimit = options.limit ?? (storeKind === "elasticsearch" && paginationTotal.value !== undefined ? documentPageRequestLimit(requestPage, pageSize.value, paginationTotal.value) : pageSize.value);
|
||||
const result = await api.documentFindDocuments(connectionId, database, collection, skip, requestLimit, filter, undefined, sort, undefined, executionId, cursor);
|
||||
if (documentLoadExecutionId.value !== executionId) return;
|
||||
if (connectionId !== props.connectionId || database !== props.database || collection !== props.collection || storeKind !== documentStoreProvider.value.kind) return;
|
||||
const nextDocuments =
|
||||
@@ -1327,9 +1375,7 @@ async function load(options: { page?: number } = {}) {
|
||||
const nextCopyDocuments = hasTypePreservingCopyDocuments ? result.extended_documents!.map(asRecord) : nextDocuments;
|
||||
// Commit page + rows together so stale rows never briefly show last-page indexes.
|
||||
if (options.page !== undefined) page.value = options.page;
|
||||
documents.value = nextDocuments;
|
||||
copyDocuments.value = nextCopyDocuments;
|
||||
mongoCopyDocumentsAvailable.value = hasTypePreservingCopyDocuments;
|
||||
commitLoadedDocuments(nextDocuments, nextCopyDocuments, hasTypePreservingCopyDocuments, options.append === true, storeKind);
|
||||
loadedDocumentQueryTotalCountRequest = countRequest;
|
||||
if (storeKind === "dynamodb") {
|
||||
const nextCursors = dynamodbPageCursors.value.slice(0, requestPage + 1);
|
||||
@@ -1337,17 +1383,6 @@ async function load(options: { page?: number } = {}) {
|
||||
dynamodbPageCursors.value = nextCursors;
|
||||
dynamodbHasNextCursor.value = !!result.next_cursor;
|
||||
}
|
||||
if (nextDocuments.length > 0) {
|
||||
const keySet = new Set<string>();
|
||||
keySet.add("_id");
|
||||
for (const doc of nextDocuments) {
|
||||
for (const key of Object.keys(doc)) {
|
||||
if (key !== "_id") keySet.add(key);
|
||||
}
|
||||
}
|
||||
lastGridColumns.value = [...keySet];
|
||||
lastGridColumnTypes.value = storeKind === "mongodb" ? mongoDocumentGridColumnTypes(nextDocuments, lastGridColumns.value) : [];
|
||||
}
|
||||
if (storeKind === "elasticsearch") {
|
||||
applyElasticsearchSearchTotal(result.total, result.total_is_exact !== false, filter);
|
||||
} else if (storeKind === "dynamodb") {
|
||||
@@ -1416,7 +1451,17 @@ async function countExactDocumentTotal(): Promise<number | undefined> {
|
||||
async function refreshDocuments() {
|
||||
if (documentStoreProvider.value.kind === "elasticsearch") resetElasticsearchTotals({ preservePaginationTotal: true });
|
||||
if (documentStoreProvider.value.kind === "dynamodb") resetDynamoDbExactCount();
|
||||
await load();
|
||||
await reloadDocumentsAfterMutationOrRefresh();
|
||||
}
|
||||
|
||||
async function reloadDocumentsAfterMutationOrRefresh() {
|
||||
if (!settingsStore.editorSettings.infiniteScroll) {
|
||||
await load();
|
||||
return;
|
||||
}
|
||||
page.value = 0;
|
||||
dataGridRef.value?.resetInfiniteScrollState?.();
|
||||
await load({ page: 0, offset: 0 });
|
||||
}
|
||||
|
||||
async function cancelDocumentLoad() {
|
||||
@@ -1443,7 +1488,14 @@ function applyFilter() {
|
||||
}
|
||||
|
||||
async function paginate(offset: number, limit: number) {
|
||||
const normalizedOffset = Math.max(0, Math.trunc(offset));
|
||||
const normalizedLimit = normalizeResultPageSize(limit, pageSize.value);
|
||||
if (documentStoreProvider.value.kind !== "dynamodb" && settingsStore.editorSettings.infiniteScroll && normalizedOffset > 0 && normalizedOffset === documents.value.length) {
|
||||
const requestedPage = Math.floor(normalizedOffset / pageSize.value);
|
||||
const nextPage = clampDocumentPage(requestedPage, pageSize.value, paginationTotal.value);
|
||||
await load({ page: nextPage, append: true, offset: normalizedOffset, limit: normalizedLimit });
|
||||
return;
|
||||
}
|
||||
const pageSizeChanged = normalizedLimit !== pageSize.value;
|
||||
pageSize.value = normalizedLimit;
|
||||
if (documentStoreProvider.value.kind === "dynamodb" && pageSizeChanged) {
|
||||
@@ -1452,10 +1504,10 @@ async function paginate(offset: number, limit: number) {
|
||||
await load({ page: 0 });
|
||||
return;
|
||||
}
|
||||
const requestedPage = Math.floor(Math.max(0, offset) / normalizedLimit);
|
||||
const requestedPage = Math.floor(normalizedOffset / normalizedLimit);
|
||||
const nextPage = clampDocumentPage(requestedPage, normalizedLimit, paginationTotal.value);
|
||||
if (documentStoreProvider.value.kind !== "dynamodb") {
|
||||
await load({ page: nextPage });
|
||||
await load({ page: nextPage, offset: nextPage * normalizedLimit, limit: normalizedLimit });
|
||||
return;
|
||||
}
|
||||
for (let cursorPage = 0; cursorPage <= nextPage; cursorPage += 1) {
|
||||
@@ -1854,7 +1906,7 @@ async function saveDoc() {
|
||||
page.value = 0;
|
||||
resetDynamoDbPagination();
|
||||
}
|
||||
await load();
|
||||
await reloadDocumentsAfterMutationOrRefresh();
|
||||
if (selectedIdx.value !== null && documents.value[selectedIdx.value]) {
|
||||
editJson.value = stringifyDocumentStoreValue(documents.value[selectedIdx.value], documentStoreProvider.value.kind, 2);
|
||||
}
|
||||
|
||||
+394
@@ -0,0 +1,394 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createApp, nextTick, type App } from "vue";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const backend = vi.hoisted(() => ({
|
||||
documentFindDocuments: vi.fn(),
|
||||
documentCountDocuments: vi.fn(),
|
||||
cancelQuery: vi.fn(),
|
||||
ensureConnected: vi.fn(),
|
||||
documentInsertDocument: vi.fn(),
|
||||
documentUpdateDocument: vi.fn(),
|
||||
documentDeleteDocument: vi.fn(),
|
||||
documentSaveMeilisearchBatch: vi.fn(),
|
||||
}));
|
||||
|
||||
const dataGrid = vi.hoisted(() => ({
|
||||
paginate: undefined as ((offset: number, limit: number) => Promise<void>) | undefined,
|
||||
reload: undefined as (() => Promise<void>) | undefined,
|
||||
save: undefined as ((changes: { dirtyRows: Map<number, Map<number, unknown>>; deletedRows: Set<number>; newRows: unknown[][]; newRowMeta: unknown[]; columns: string[]; rows: unknown[][] }) => Promise<void>) | undefined,
|
||||
resetInfiniteScrollState: undefined as ReturnType<typeof vi.fn> | undefined,
|
||||
previousResult: undefined as { rows: readonly unknown[]; appended_from_row_count?: number } | undefined,
|
||||
rows: [] as unknown[],
|
||||
columns: [] as string[],
|
||||
appendedFromRowCount: undefined as number | undefined,
|
||||
pageOffset: undefined as number | undefined,
|
||||
pageLimit: undefined as number | undefined,
|
||||
selectionActive: false,
|
||||
editActive: false,
|
||||
fullReplaceCount: 0,
|
||||
}));
|
||||
|
||||
const settings = vi.hoisted(() => ({
|
||||
editorSettings: {
|
||||
pageSize: 2,
|
||||
infiniteScroll: true,
|
||||
mongoViewMode: "table" as "document" | "table",
|
||||
columnWidthDensity: "standard" as "compact" | "standard" | "comfortable",
|
||||
dataGridRenderMode: "canvas" as "canvas" | "dom",
|
||||
tableFontFamily: "system-ui",
|
||||
tableFontSize: 12,
|
||||
numericColumnRightAlign: true,
|
||||
confirmDangerousSqlExecution: true,
|
||||
exportBatchSize: 2,
|
||||
exportRowLimitEnabled: false,
|
||||
exportRowLimit: 100_000,
|
||||
},
|
||||
updateEditorSettings: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("vue-i18n", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("vue-i18n")>()),
|
||||
useI18n: () => ({ t: (key: string) => key }),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/backend/api", () => ({
|
||||
getColumns: vi.fn(),
|
||||
documentFindDocuments: backend.documentFindDocuments,
|
||||
documentCountDocuments: backend.documentCountDocuments,
|
||||
dynamodbDescribeTable: vi.fn(),
|
||||
cancelQuery: backend.cancelQuery,
|
||||
documentInsertDocument: backend.documentInsertDocument,
|
||||
documentUpdateDocument: backend.documentUpdateDocument,
|
||||
documentDeleteDocument: backend.documentDeleteDocument,
|
||||
documentSaveMeilisearchBatch: backend.documentSaveMeilisearchBatch,
|
||||
}));
|
||||
|
||||
vi.mock("@/stores/connectionStore", () => ({
|
||||
useConnectionStore: () => ({ ensureConnected: backend.ensureConnected }),
|
||||
}));
|
||||
|
||||
vi.mock("@/stores/settingsStore", () => ({
|
||||
TABLE_FONT_SIZE_MIN: 8,
|
||||
TABLE_FONT_SIZE_MAX: 16,
|
||||
useSettingsStore: () => settings,
|
||||
}));
|
||||
|
||||
vi.mock("@/components/grid/DataGrid.vue", async () => {
|
||||
const { defineComponent, h } = await import("vue");
|
||||
const { isDataGridPrefixAppend } = await import("@/lib/dataGrid/dataGridInfiniteScroll");
|
||||
return {
|
||||
default: defineComponent({
|
||||
name: "DataGridStub",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
result: { type: Object, required: true },
|
||||
customSaveHandler: { type: Object, required: false },
|
||||
pageOffset: { type: Number, required: false },
|
||||
pageLimit: { type: Number, required: false },
|
||||
},
|
||||
setup(props, { attrs, expose }) {
|
||||
dataGrid.paginate = attrs.onPaginate as typeof dataGrid.paginate;
|
||||
dataGrid.reload = attrs.onReload as typeof dataGrid.reload;
|
||||
dataGrid.save = (props.customSaveHandler as { save?: typeof dataGrid.save } | undefined)?.save;
|
||||
const resetInfiniteScrollState = vi.fn();
|
||||
dataGrid.resetInfiniteScrollState = resetInfiniteScrollState;
|
||||
expose({
|
||||
visibleColumnCount: 2,
|
||||
displayableColumnCount: 2,
|
||||
hiddenColumnCount: 0,
|
||||
orderedColumnLayoutOptions: [],
|
||||
filteredColumnLayoutOptions: () => [],
|
||||
toggleColumnVisibility: vi.fn(),
|
||||
showAllColumns: vi.fn(),
|
||||
invertColumnVisibility: vi.fn(),
|
||||
hasCustomColumnOrder: false,
|
||||
moveDisplayableColumn: vi.fn(),
|
||||
resetColumnOrder: vi.fn(),
|
||||
nullColumnsHidden: false,
|
||||
canToggleAllNullColumns: false,
|
||||
allNullColumnCount: 0,
|
||||
toggleAllNullColumns: vi.fn(),
|
||||
multiRowTranspose: false,
|
||||
setMultiRowTranspose: vi.fn(),
|
||||
resetInfiniteScrollState,
|
||||
});
|
||||
return () => {
|
||||
const result = props.result as { rows: unknown[]; columns?: string[]; appended_from_row_count?: number };
|
||||
if (dataGrid.previousResult && dataGrid.previousResult !== result && !isDataGridPrefixAppend(dataGrid.previousResult, result)) {
|
||||
dataGrid.selectionActive = false;
|
||||
dataGrid.editActive = false;
|
||||
dataGrid.fullReplaceCount += 1;
|
||||
}
|
||||
dataGrid.previousResult = result;
|
||||
dataGrid.rows = result.rows;
|
||||
dataGrid.columns = result.columns ?? [];
|
||||
dataGrid.appendedFromRowCount = result.appended_from_row_count;
|
||||
dataGrid.pageOffset = props.pageOffset;
|
||||
dataGrid.pageLimit = props.pageLimit;
|
||||
return h("div", { "data-testid": "data-grid" });
|
||||
};
|
||||
},
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@/components/redis/RedisJsonEditor.vue", async () => {
|
||||
const { defineComponent, h } = await import("vue");
|
||||
return {
|
||||
default: defineComponent({
|
||||
props: { modelValue: { type: String, required: true } },
|
||||
setup(props) {
|
||||
return () => h("div", {}, props.modelValue);
|
||||
},
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@/components/ui/popover", async () => {
|
||||
const { defineComponent, h } = await import("vue");
|
||||
const passthrough = (name: string) =>
|
||||
defineComponent({
|
||||
name,
|
||||
setup(_, { slots }) {
|
||||
return () => h("div", slots.default?.());
|
||||
},
|
||||
});
|
||||
return { Popover: passthrough("PopoverStub"), PopoverTrigger: passthrough("PopoverTriggerStub"), PopoverContent: passthrough("PopoverContentStub") };
|
||||
});
|
||||
|
||||
vi.mock("@/components/ui/select", async () => {
|
||||
const { defineComponent, h } = await import("vue");
|
||||
const passthrough = (name: string) =>
|
||||
defineComponent({
|
||||
name,
|
||||
setup(_, { slots }) {
|
||||
return () => h("div", slots.default?.());
|
||||
},
|
||||
});
|
||||
return { Select: passthrough("SelectStub"), SelectContent: passthrough("SelectContentStub"), SelectItem: passthrough("SelectItemStub"), SelectTrigger: passthrough("SelectTriggerStub"), SelectValue: passthrough("SelectValueStub") };
|
||||
});
|
||||
|
||||
import DocumentBrowser from "@/components/document/DocumentBrowser.vue";
|
||||
|
||||
let app: App<Element> | null = null;
|
||||
let root: HTMLDivElement | null = null;
|
||||
|
||||
async function flushUi() {
|
||||
for (let index = 0; index < 4; index++) {
|
||||
await Promise.resolve();
|
||||
await nextTick();
|
||||
}
|
||||
}
|
||||
|
||||
async function mountBrowser() {
|
||||
app = createApp(DocumentBrowser, {
|
||||
connectionId: "mongo-1",
|
||||
database: "test",
|
||||
collection: "docs",
|
||||
databaseType: "mongodb",
|
||||
});
|
||||
app.mount(root!);
|
||||
await flushUi();
|
||||
}
|
||||
|
||||
function documentResult(start: number, count: number, total: number, extra: Record<string, unknown> = {}) {
|
||||
return {
|
||||
documents: Array.from({ length: count }, (_, index) => {
|
||||
const id = start + index;
|
||||
return { _id: String(id), name: `row_${id}`, ...extra };
|
||||
}),
|
||||
total,
|
||||
total_is_exact: true,
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.stubGlobal("localStorage", {
|
||||
getItem: () => null,
|
||||
setItem: () => undefined,
|
||||
removeItem: () => undefined,
|
||||
});
|
||||
backend.documentFindDocuments.mockReset();
|
||||
backend.documentCountDocuments.mockReset();
|
||||
backend.cancelQuery.mockReset();
|
||||
backend.ensureConnected.mockReset();
|
||||
backend.documentInsertDocument.mockReset();
|
||||
backend.documentUpdateDocument.mockReset();
|
||||
backend.documentDeleteDocument.mockReset();
|
||||
backend.documentSaveMeilisearchBatch.mockReset();
|
||||
backend.ensureConnected.mockResolvedValue(undefined);
|
||||
backend.documentCountDocuments.mockResolvedValue(0);
|
||||
dataGrid.paginate = undefined;
|
||||
dataGrid.reload = undefined;
|
||||
dataGrid.save = undefined;
|
||||
dataGrid.resetInfiniteScrollState = undefined;
|
||||
dataGrid.previousResult = undefined;
|
||||
dataGrid.rows = [];
|
||||
dataGrid.columns = [];
|
||||
dataGrid.appendedFromRowCount = undefined;
|
||||
dataGrid.pageOffset = undefined;
|
||||
dataGrid.pageLimit = undefined;
|
||||
dataGrid.selectionActive = false;
|
||||
dataGrid.editActive = false;
|
||||
dataGrid.fullReplaceCount = 0;
|
||||
settings.editorSettings.pageSize = 2;
|
||||
settings.editorSettings.infiniteScroll = true;
|
||||
|
||||
root = document.createElement("div");
|
||||
document.body.appendChild(root);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
app?.unmount();
|
||||
root?.remove();
|
||||
app = null;
|
||||
root = null;
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("DocumentBrowser infinite scroll (issue #6455)", () => {
|
||||
it("preserves existing row identity and DataGrid selection/edit state for same-column appends", async () => {
|
||||
backend.documentFindDocuments.mockResolvedValueOnce(documentResult(1, 2, 4)).mockResolvedValueOnce(documentResult(3, 2, 4));
|
||||
|
||||
await mountBrowser();
|
||||
|
||||
expect(dataGrid.rows).toHaveLength(2);
|
||||
expect(dataGrid.appendedFromRowCount).toBeUndefined();
|
||||
const firstRows = [...dataGrid.rows];
|
||||
dataGrid.selectionActive = true;
|
||||
dataGrid.editActive = true;
|
||||
|
||||
expect(dataGrid.paginate).toBeTypeOf("function");
|
||||
await dataGrid.paginate!(2, 2);
|
||||
await flushUi();
|
||||
|
||||
expect(backend.documentFindDocuments.mock.calls[1]!.slice(0, 5)).toEqual(["mongo-1", "test", "docs", 2, 2]);
|
||||
// The second page must be grafted onto the first, not replace it —
|
||||
// otherwise scrolling past the first batch silently discards it (#6455).
|
||||
expect(dataGrid.rows).toHaveLength(4);
|
||||
expect(dataGrid.rows.map((row) => (row as unknown[])[1])).toEqual(["row_1", "row_2", "row_3", "row_4"]);
|
||||
expect(dataGrid.appendedFromRowCount).toBe(2);
|
||||
expect(dataGrid.rows[0]).toBe(firstRows[0]);
|
||||
expect(dataGrid.rows[1]).toBe(firstRows[1]);
|
||||
expect(dataGrid.selectionActive).toBe(true);
|
||||
expect(dataGrid.editActive).toBe(true);
|
||||
});
|
||||
|
||||
it("performs a full row rebuild when an appended segment introduces a new column", async () => {
|
||||
backend.documentFindDocuments.mockResolvedValueOnce(documentResult(1, 2, 4)).mockResolvedValueOnce(documentResult(3, 2, 4, { status: "new" }));
|
||||
|
||||
await mountBrowser();
|
||||
const firstRow = dataGrid.rows[0];
|
||||
dataGrid.selectionActive = true;
|
||||
dataGrid.editActive = true;
|
||||
|
||||
await dataGrid.paginate!(2, 2);
|
||||
await flushUi();
|
||||
|
||||
expect(dataGrid.columns).toEqual(["_id", "name", "status"]);
|
||||
expect(dataGrid.appendedFromRowCount).toBeUndefined();
|
||||
expect(dataGrid.rows[0]).not.toBe(firstRow);
|
||||
expect(dataGrid.selectionActive).toBe(false);
|
||||
expect(dataGrid.editActive).toBe(false);
|
||||
});
|
||||
|
||||
it("reloads from offset zero after three segments are refreshed", async () => {
|
||||
backend.documentFindDocuments
|
||||
.mockResolvedValueOnce(documentResult(1, 2, 8))
|
||||
.mockResolvedValueOnce(documentResult(3, 2, 8))
|
||||
.mockResolvedValueOnce(documentResult(5, 2, 8))
|
||||
.mockResolvedValueOnce(documentResult(1, 2, 8, { refreshed: true }));
|
||||
|
||||
await mountBrowser();
|
||||
await dataGrid.paginate!(2, 2);
|
||||
await dataGrid.paginate!(4, 2);
|
||||
await flushUi();
|
||||
expect(dataGrid.rows).toHaveLength(6);
|
||||
|
||||
await dataGrid.reload!();
|
||||
await flushUi();
|
||||
|
||||
expect(backend.documentFindDocuments.mock.calls[3]!.slice(0, 5)).toEqual(["mongo-1", "test", "docs", 0, 2]);
|
||||
expect(dataGrid.rows).toHaveLength(2);
|
||||
expect(dataGrid.appendedFromRowCount).toBeUndefined();
|
||||
expect(dataGrid.pageOffset).toBe(0);
|
||||
expect(dataGrid.pageLimit).toBe(2);
|
||||
expect(dataGrid.resetInfiniteScrollState).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("reloads from offset zero after saving with three segments loaded", async () => {
|
||||
backend.documentFindDocuments
|
||||
.mockResolvedValueOnce(documentResult(1, 2, 8))
|
||||
.mockResolvedValueOnce(documentResult(3, 2, 8))
|
||||
.mockResolvedValueOnce(documentResult(5, 2, 8))
|
||||
.mockResolvedValueOnce(documentResult(1, 2, 8, { saved: true }));
|
||||
|
||||
await mountBrowser();
|
||||
await dataGrid.paginate!(2, 2);
|
||||
await dataGrid.paginate!(4, 2);
|
||||
await flushUi();
|
||||
|
||||
await dataGrid.save!({
|
||||
dirtyRows: new Map([[0, new Map([[1, "row_1_updated"]])]]),
|
||||
deletedRows: new Set(),
|
||||
newRows: [],
|
||||
newRowMeta: [],
|
||||
columns: [...dataGrid.columns],
|
||||
rows: dataGrid.rows as unknown[][],
|
||||
});
|
||||
await flushUi();
|
||||
|
||||
expect(backend.documentUpdateDocument).toHaveBeenCalledTimes(1);
|
||||
expect(backend.documentFindDocuments.mock.calls[3]!.slice(0, 5)).toEqual(["mongo-1", "test", "docs", 0, 2]);
|
||||
expect(dataGrid.rows).toHaveLength(2);
|
||||
expect(dataGrid.appendedFromRowCount).toBeUndefined();
|
||||
expect(dataGrid.pageOffset).toBe(0);
|
||||
expect(dataGrid.pageLimit).toBe(2);
|
||||
expect(dataGrid.resetInfiniteScrollState).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("keeps the configured page size when the final capped segment has a smaller limit", async () => {
|
||||
settings.editorSettings.pageSize = 3;
|
||||
backend.documentFindDocuments
|
||||
.mockResolvedValueOnce(documentResult(1, 3, 11))
|
||||
.mockResolvedValueOnce(documentResult(4, 3, 11))
|
||||
.mockResolvedValueOnce(documentResult(7, 3, 11))
|
||||
.mockResolvedValueOnce(documentResult(10, 2, 11));
|
||||
|
||||
await mountBrowser();
|
||||
const firstRow = dataGrid.rows[0];
|
||||
await dataGrid.paginate!(3, 3);
|
||||
await dataGrid.paginate!(6, 3);
|
||||
await dataGrid.paginate!(9, 2);
|
||||
await flushUi();
|
||||
|
||||
expect(backend.documentFindDocuments.mock.calls.map((call) => call.slice(3, 5))).toEqual([
|
||||
[0, 3],
|
||||
[3, 3],
|
||||
[6, 3],
|
||||
[9, 2],
|
||||
]);
|
||||
expect(dataGrid.rows).toHaveLength(11);
|
||||
expect(dataGrid.rows[0]).toBe(firstRow);
|
||||
expect(dataGrid.appendedFromRowCount).toBe(9);
|
||||
expect(dataGrid.pageLimit).toBe(3);
|
||||
expect(dataGrid.pageOffset).toBe(9);
|
||||
});
|
||||
|
||||
it("replaces the documents instead of appending when infinite scroll is disabled", async () => {
|
||||
settings.editorSettings.infiniteScroll = false;
|
||||
backend.documentFindDocuments.mockResolvedValueOnce(documentResult(1, 2, 4)).mockResolvedValueOnce(documentResult(3, 2, 4));
|
||||
|
||||
await mountBrowser();
|
||||
|
||||
await dataGrid.paginate!(2, 2);
|
||||
await flushUi();
|
||||
|
||||
// Classic next-page navigation must keep replacing the page, not accumulate rows.
|
||||
expect(dataGrid.rows).toHaveLength(2);
|
||||
expect(dataGrid.rows.map((row) => (row as unknown[])[1])).toEqual(["row_3", "row_4"]);
|
||||
expect(dataGrid.appendedFromRowCount).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user