Files
monkey-ai-web/tests/users-pagination.test.js
T

215 lines
7.5 KiB
JavaScript

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { paginationEvents } from '../src/components/ui/pagination.js';
import test from 'node:test';
import { runInNewContext } from 'node:vm';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import * as Ant from 'antd';
import * as pagination from '../src/components/ui/pagination.js';
import { transformWithEsbuild } from 'vite';
async function compile(relativePath) {
const filename = new URL(relativePath, import.meta.url);
const { code } = await transformWithEsbuild(
await readFile(filename, 'utf8'),
filename.pathname,
{ loader: 'jsx', format: 'cjs', target: 'node22', jsx: 'transform' },
);
return (modules) => {
const module = { exports: {} };
runInNewContext(code, {
module,
exports: module.exports,
window: { matchMedia: () => ({ matches: false }) },
require(name) {
assert.ok(Object.hasOwn(modules, name), `Unexpected import: ${name}`);
return modules[name];
},
});
return module.exports;
};
}
const loadUtils = await compile('../src/helpers/utils.jsx');
const { createCardProPagination } = loadUtils({
react: React,
'@/components/ui': { Pagination: 'antd-pagination' },
'../constants': { toastConstants: {} },
i18next: {},
'../constants/playground.constants': {},
'../hooks/common/useIsMobile': { MOBILE_BREAKPOINT: 768 },
'./officialPrice': {},
'./data': {},
'./tableCompactMode.js': {},
});
const loadUsersPage = await compile('../src/components/table/users/index.jsx');
function paginationProps(element) {
const pagination = React.Children.toArray(element.props.children).find(
(child) => child.type === 'antd-pagination',
);
assert.ok(pagination, 'The helper must render Ant Design Pagination');
return pagination.props;
}
function foundationFor(props) {
const events = paginationEvents(props);
return {
changePageSize: size => { events.onShowSizeChange(props.currentPage, size); events.onChange(props.currentPage, size); },
goPage: page => events.onChange(page, props.pageSize),
};
}
for (const option of [{}, { preventPageChangeOnPageSizeChange: false }]) {
const mode = Object.hasOwn(option, 'preventPageChangeOnPageSizeChange')
? 'explicit false'
: 'default';
test(`other tables retain existing resize page callback: ${mode}`, () => {
const calls = [];
const props = paginationProps(
createCardProPagination({
currentPage: 3,
pageSize: 10,
total: 100,
onPageSizeChange: (size) => calls.push(['size', size]),
onPageChange: (page) => calls.push(['page', page]),
...option,
}),
);
foundationFor(props).changePageSize(20);
assert.deepEqual(calls, [
['size', 20],
['page', 2],
]);
assert.equal(props.preventPageChangeOnPageSizeChange, false);
});
}
for (const isMobile of [false, true]) {
test(`users resize emits only the size callback; navigation still works (${isMobile ? 'mobile' : 'desktop'})`, () => {
const calls = [];
const usersData = {
activePage: 3,
pageSize: 10,
userCount: 100,
handlePageSizeChange: (size) => calls.push(['size', size]),
handlePageChange: (page) => calls.push(['page', page]),
t: (key) => key,
};
const { default: UsersPage } = loadUsersPage({
react: React,
'../../common/ui/CardPro': 'card-pro',
'./UsersTable': 'users-table',
'./UsersActions': 'users-actions',
'./UsersFilters': 'users-filters',
'./UsersDescription': 'users-description',
'./modals/AddUserModal': 'add-user-modal',
'./modals/EditUserModal': 'edit-user-modal',
'../../../hooks/users/useUsersData': { useUsersData: () => usersData },
'../../../hooks/common/useIsMobile': { useIsMobile: () => isMobile },
'../../../helpers/utils': { createCardProPagination },
});
const renderPagination = () => {
const card = React.Children.toArray(UsersPage().props.children).find(
(child) => child.type === 'card-pro',
);
assert.ok(card, 'UsersPage must pass pagination to CardPro');
return paginationProps(card.props.paginationArea);
};
const props = renderPagination();
foundationFor(props).changePageSize(20);
assert.deepEqual(calls, [['size', 20]]);
assert.equal(props.preventPageChangeOnPageSizeChange, true);
assert.equal(props.onPageSizeChange, usersData.handlePageSizeChange);
assert.equal(props.onPageChange, usersData.handlePageChange);
assert.equal(props.size, isMobile ? 'small' : 'default');
// The hook owns the reset; verify navigation after its next render.
usersData.activePage = 1;
usersData.pageSize = 20;
calls.length = 0;
const nextProps = renderPagination();
assert.equal(nextProps.currentPage, 1);
assert.equal(nextProps.pageSize, 20);
foundationFor(nextProps).goPage(2);
assert.deepEqual(calls, [['page', 2]]);
});
}
const loadUi = await compile('../src/components/ui/index.jsx');
const { Table, Pagination } = loadUi({
react: React,
antd: Ant,
'./controls': { controlSize: (size) => size },
'./pagination': pagination,
'./overlayPolicy': {},
'./form': {},
'./chat': {},
});
for (const [name, render] of [
[
'Table',
(props) =>
Table({
columns: [{ title: 'Model', dataIndex: 'model' }],
dataSource: [{ key: '1', model: 'test-model' }],
pagination: props,
}),
],
['Pagination', (props) => Pagination(props)],
]) {
test(`${name} renders legacy boolean showTotal without crashing`, () => {
const element = render({ total: 25, pageSize: 10, showTotal: true });
const html = renderToStaticMarkup(element);
assert.match(html, /class="ant-pagination-total-text">25<\/li>/);
});
test(`${name} preserves a custom total renderer and its range arguments`, () => {
const showTotal = (total, range) => `${range.join('-')} of ${total}`;
const element = render({ total: 25, pageSize: 10, showTotal });
const props = name === 'Table' ? element.props.pagination : element.props;
assert.equal(props.showTotal, showTotal);
assert.match(renderToStaticMarkup(element), /1-10 of 25/);
});
for (const showTotal of [false, undefined]) {
test(`${name} hides the total when showTotal is ${showTotal}`, () => {
const html = renderToStaticMarkup(
render({ total: 25, pageSize: 10, showTotal }),
);
assert.doesNotMatch(html, /ant-pagination-total-text/);
});
}
}
test('Table keeps pagination disabled for settings tables without pagination', () => {
const element = Table({ columns: [], dataSource: [], pagination: false });
assert.equal(element.props.pagination, false);
assert.doesNotMatch(renderToStaticMarkup(element), /ant-pagination/);
});