node-blog/test_pagination_component.js
2025-06-24 11:42:12 +08:00

71 lines
2.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const ejs = require('ejs');
const path = require('path');
const fs = require('fs');
// 测试分页组件是否能正确渲染
function testPaginationComponent() {
console.log('测试分页组件...\n');
try {
// 读取分页组件文件
const paginationPath = path.join(__dirname, 'views', 'components', 'pagination.ejs');
if (fs.existsSync(paginationPath)) {
console.log('✅ 分页组件文件存在');
// 读取组件内容
const componentContent = fs.readFileSync(paginationPath, 'utf8');
console.log('✅ 分页组件文件读取成功');
// 测试数据
const testData = {
currentPage: 3,
totalPages: 10,
baseUrl: '/admin/posts',
query: { search: 'test' }
};
// 尝试渲染组件
const rendered = ejs.render(componentContent, testData);
console.log('✅ 分页组件渲染成功');
// 检查渲染结果
if (rendered.includes('pagination')) {
console.log('✅ 分页组件包含正确的HTML结构');
}
console.log('\n分页组件测试通过');
} else {
console.log('❌ 分页组件文件不存在');
}
} catch (error) {
console.error('❌ 测试失败:', error.message);
}
}
// 测试路径解析
function testPathResolution() {
console.log('\n测试路径解析...\n');
const paths = [
'views/components/pagination.ejs',
'views/admin/users/list.ejs',
'views/admin/posts/list.ejs',
'views/admin/comments/list.ejs'
];
paths.forEach(filePath => {
const fullPath = path.join(__dirname, filePath);
if (fs.existsSync(fullPath)) {
console.log(`${filePath} 存在`);
} else {
console.log(`${filePath} 不存在`);
}
});
}
// 运行测试
testPaginationComponent();
testPathResolution();