159 lines
5.5 KiB
JavaScript
159 lines
5.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 { 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]]);
|
|
});
|
|
}
|