-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
62 lines (54 loc) · 2.06 KB
/
test.js
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
/*
Created by Sari I. Younan
04/05/2024 15:13:18
test.js
*/
import { JSDOM } from 'jsdom';
import fetch from 'node-fetch'; // Make sure to install node-fetch if not already installed
const dom = new JSDOM(`
<!DOCTYPE html>
<html lang="">
<body>
<div id="currency"></div>
<form id="budgetForm">
<input id="title" type="text" />
<input id="amount" type="number" />
<input id="description" type="text" />
</form>
<div id="budgetList"></div>
</body>
</html>`);
const { window } = dom;
const { document } = window;
// Simulate DOMContentLoaded event
window.addEventListener('DOMContentLoaded', () => {
console.log('DOM fully loaded');
});
function addBudgetEntry(title, amount, description, currency) {
const entryDiv = document.createElement('div');
entryDiv.textContent = `${title}: ${amount.toFixed(2)} ${currency} : ${description}`;
document.getElementById('budgetList').appendChild(entryDiv);
}
fetch('https://ipapi.co/json/')
.then(response => response.json())
.then(data => {
const currencyCode = data.currency
console.log(currencyCode); // Output full data for debugging
document.getElementById('currency').textContent = determineCurrency(data);
})
.catch(() => {
document.getElementById('currency').textContent = 'USD';
});
document.getElementById('budgetForm').addEventListener('submit', function(event) {
event.preventDefault();
const title = document.getElementById('title').value;
const amount = parseFloat(document.getElementById('amount').value);
const description = document.getElementById('description').value;
const currency = document.getElementById('currency').textContent;
addBudgetEntry(title, amount, description, currency);
document.getElementById('title').value = '';
document.getElementById('amount').value = '';
document.getElementById('description').value = '';
});
// Optionally, print out the HTML to see what it looks like after manipulation
setTimeout(() => console.log(dom.serialize()), 2000); // Delay to allow fetch and other async operations to complete