/* 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 . For commercial licensing, please contact support@quantumnous.com */ import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { createRequire } from 'node:module'; import test from 'node:test'; import { runInNewContext } from 'node:vm'; import React from 'react'; import { transformWithEsbuild } from 'vite'; const require = createRequire(import.meta.url); const PaginationFoundation = require('@douyinfe/semi-foundation/lib/cjs/pagination/foundation.js').default; 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, '@douyinfe/semi-ui': { Pagination: 'semi-pagination' }, '../constants': { toastConstants: {} }, 'react-toastify': {}, 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 === 'semi-pagination', ); assert.ok(pagination, 'The helper must render Semi Pagination'); return pagination.props; } function foundationFor(props) { const state = { currentPage: props.currentPage, pageSize: props.pageSize, total: props.total, }; // Keep the render snapshot during the event, as with React's batched updates. // Only view-state setters are stubbed; Semi's callback decisions run unchanged. return new PaginationFoundation({ getProps: () => props, getStates: () => state, getState: (key) => state[key], updatePageSize: () => {}, updateTotal: () => {}, setCurrentPage: () => {}, setDisabled: () => {}, setPageList: () => {}, notifyPageSizeChange: (size) => props.onPageSizeChange(size), notifyPageChange: (page) => props.onPageChange(page), notifyChange: () => {}, }); } for (const option of [{}, { preventPageChangeOnPageSizeChange: false }]) { const mode = Object.hasOwn(option, 'preventPageChangeOnPageSizeChange') ? 'explicit false' : 'default'; test(`other tables retain Semi's 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]]); }); }