Repository navigation
Expand file tree
/
Copy pathtests.html
More file actions
198 lines (175 loc) · 8.33 KB
/
Copy pathtests.html
File metadata and controls
198 lines (175 loc) · 8.33 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html>
<html>
<head>
<title>Argus-Sheet - Test Suite</title>
<style>
body { font-family: sans-serif; margin: 20px; }
#results { border-collapse: collapse; width: 100%; }
#results th, #results td { border: 1px solid #ddd; padding: 8px; }
#results th { background-color: #f2f2f2; }
.pass { color: green; }
.fail { color: red; }
</style>
</head>
<body>
<h1>Argus-Sheet - Test Suite</h1>
<table id="results">
<thead>
<tr>
<th>Test Case</th>
<th>Status</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<!-- Test results will be inserted here -->
</tbody>
</table>
<!-- DOM elements required by the functions under test -->
<div id="requests-list-container" style="display:none;"></div>
<div id="pagination-container" style="display:none;"></div>
<div id="error-display" style="display:none;"></div>
<script>
// =================================================================================
// MOCKS & HELPERS
// =================================================================================
// Mock functions that are dependencies of the function being tested.
function buildPaginationControls(totalPages, totalRecords) {
// This is a mock; we just need it to exist.
console.log(`Mock buildPaginationControls called with: ${totalPages}, ${totalRecords}`);
}
function showError(error) {
// In a test environment, we want to see the error clearly.
console.error("showError called:", error.message);
const resultsBody = document.querySelector("#results tbody");
const row = resultsBody.insertRow();
row.innerHTML = `
<td>showError call</td>
<td class="fail">FAIL</td>
<td>${error.message}</td>
`;
}
function formatCurrency(value) {
if (typeof value !== 'number') return String(value || '');
return value.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' });
}
function formatDate(dateValue, includeTime = true) {
if (!dateValue) return '';
const d = new Date(dateValue);
if (isNaN(d.getTime())) return String(dateValue);
return d.toLocaleDateString('pt-BR');
}
// =================================================================================
// FUNCTIONS UNDER TEST (Copied from main.ts.html)
// =================================================================================
function handleResponse(jsonString) {
try {
if (!jsonString) { throw new Error("O servidor retornou uma resposta vazia."); }
const response = JSON.parse(jsonString);
if (response.error) { throw new Error(response.error); }
// This is the fixed logic we are testing
if (response.requests) {
displayRequests(response.requests);
buildPaginationControls(response.totalPages, response.totalRecords);
const paginationContainer = document.getElementById('pagination-container');
if (paginationContainer) paginationContainer.style.display = 'flex';
}
} catch (e) {
showError(e);
}
}
function displayRequests(requests) {
const listContainer = document.getElementById('requests-list-container');
listContainer.innerHTML = '';
if (!requests || requests.length === 0) {
listContainer.innerHTML = '<li class="text-center text-secondary py-5">Nenhuma solicitação encontrada para os filtros aplicados.</li>';
return;
}
const statusMap = {
'PENDENTE DE CADASTRO SAP': { text: 'Pendente', class: 'pending' },
'CADASTRO EFETUADO': { text: 'Aprovado', class: 'approved' },
};
const fragment = document.createDocumentFragment();
requests.forEach(req => {
const card = document.createElement('li');
card.className = 'request-card';
let itemStatus = (req.STATUS_GERAL || 'PENDENTE DE CADASTRO SAP').trim().toUpperCase();
let statusInfo = statusMap[itemStatus] || { text: 'Pendente', class: 'pending' };
card.innerHTML = `<div>SKU: ${req.MATERIAL || 'N/A'}</div>`;
fragment.appendChild(card);
});
listContainer.appendChild(fragment);
}
// =================================================================================
// TEST RUNNER
// =================================================================================
const tests = [
function testHandleResponse_WithCorrectData_ShouldRenderItems() {
// Arrange
const listContainer = document.getElementById('requests-list-container');
listContainer.innerHTML = ''; // Reset DOM state
const mockResponse = {
requests: [
{ "MATERIAL": "SKU001", "STATUS_GERAL": "PENDENTE DE CADASTRO SAP" },
{ "MATERIAL": "SKU002", "STATUS_GERAL": "CADASTRO EFETUADO" }
],
totalPages: 1,
totalRecords: 2
};
const jsonString = JSON.stringify(mockResponse);
// Act
handleResponse(jsonString);
// Assert
const renderedItems = listContainer.querySelectorAll('.request-card');
if (renderedItems.length !== 2) {
throw new Error(`Expected 2 items to be rendered, but found ${renderedItems.length}.`);
}
if (listContainer.innerHTML.includes("SKU001") === false) {
throw new Error("Expected to find SKU001 in the output.");
}
},
function testHandleResponse_WithError_ShouldCallShowError() {
// This test is designed to fail to demonstrate the test runner works.
// We'll simulate this by checking if showError was called.
// For a real test, we'd need to spy on showError. For this simple runner,
// we'll just check for a specific error message.
// Arrange
const listContainer = document.getElementById('requests-list-container');
const errorDisplay = document.getElementById('error-display');
listContainer.innerHTML = '';
errorDisplay.innerText = '';
const jsonString = JSON.stringify({ error: "Backend database is down" });
// Act
handleResponse(jsonString);
// Assert
// The showError mock will have populated the table, so we check there.
const resultsBody = document.querySelector("#results tbody");
const lastRow = resultsBody.rows[resultsBody.rows.length - 1];
if (!lastRow || !lastRow.cells[2].innerText.includes("Backend database is down")) {
throw new Error("showError was not called with the expected error message.");
}
}
];
window.onload = () => {
const resultsBody = document.querySelector("#results tbody");
tests.forEach(test => {
const row = resultsBody.insertRow();
const nameCell = row.insertCell();
const statusCell = row.insertCell();
const detailsCell = row.insertCell();
nameCell.textContent = test.name;
try {
test();
statusCell.textContent = 'PASS';
statusCell.className = 'pass';
} catch (e) {
statusCell.textContent = 'FAIL';
statusCell.className = 'fail';
detailsCell.textContent = e.message;
console.error(`Test failed: ${test.name}`, e);
}
});
};
</script>
</body>
</html>