forked from Vesting-Vault/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-pagination.js
More file actions
125 lines (111 loc) · 4.47 KB
/
test-pagination.js
File metadata and controls
125 lines (111 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Test script for pagination endpoint
const http = require('http');
// Test default pagination (page 1, limit 20)
const testDefaultPagination = () => {
return new Promise((resolve, reject) => {
const options = {
hostname: 'localhost',
port: 3000,
path: '/api/vaults',
method: 'GET',
headers: { 'Content-Type': 'application/json' }
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
try {
const response = JSON.parse(data);
console.log('✅ Default Pagination Test:');
console.log('Page:', response.pagination.current_page);
console.log('Limit:', response.pagination.per_page);
console.log('Total Vaults:', response.pagination.total_vaults);
console.log('Vaults returned:', response.vaults.length);
console.log('Has next page:', response.pagination.has_next_page);
// Verify acceptance criteria
if (response.pagination.current_page === 1 &&
response.pagination.per_page === 20 &&
response.vaults.length <= 20) {
console.log('🎉 SUCCESS: Default pagination works!');
resolve(true);
} else {
console.log('❌ FAILED: Default pagination test failed');
resolve(false);
}
} catch (error) {
console.log('❌ ERROR parsing response:', error.message);
resolve(false);
}
});
});
req.on('error', (error) => {
console.log('❌ ERROR making request:', error.message);
resolve(false);
});
req.end();
});
};
// Test page 2 with limit 10
const testPage2Limit10 = () => {
return new Promise((resolve, reject) => {
const options = {
hostname: 'localhost',
port: 3000,
path: '/api/vaults?page=2&limit=10',
method: 'GET',
headers: { 'Content-Type': 'application/json' }
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
try {
const response = JSON.parse(data);
console.log('\n✅ Page 2 Limit 10 Test:');
console.log('Page:', response.pagination.current_page);
console.log('Limit:', response.pagination.per_page);
console.log('Vaults returned:', response.vaults.length);
console.log('Has prev page:', response.pagination.has_prev_page);
// Verify acceptance criteria
if (response.pagination.current_page === 2 &&
response.pagination.per_page === 10 &&
response.vaults.length <= 10) {
console.log('🎉 SUCCESS: Custom pagination works!');
resolve(true);
} else {
console.log('❌ FAILED: Custom pagination test failed');
resolve(false);
}
} catch (error) {
console.log('❌ ERROR parsing response:', error.message);
resolve(false);
}
});
});
req.on('error', (error) => {
console.log('❌ ERROR making request:', error.message);
resolve(false);
});
req.end();
});
};
// Run tests and exit with proper code
const runTests = async () => {
console.log('🧪 Testing Pagination for Issue #18\n');
try {
const test1 = await testDefaultPagination();
const test2 = await testPage2Limit10();
if (test1 && test2) {
console.log('\n🎉 ALL TESTS PASSED!');
process.exit(0);
} else {
console.log('\n❌ SOME TESTS FAILED!');
process.exit(1);
}
} catch (error) {
console.log('\n❌ TEST ERROR:', error.message);
process.exit(1);
}
};
// Run tests
runTests();