-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
143 lines (118 loc) · 4.92 KB
/
index.test.js
File metadata and controls
143 lines (118 loc) · 4.92 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
import { describe, it, expect, beforeEach } from 'vitest'
import { JSDOM } from 'jsdom'
import fs from 'fs'
import path from 'path'
import { initApp } from './ui.js'
describe('index.html structure', () => {
let dom
let document
beforeEach(() => {
const html = fs.readFileSync(path.resolve(__dirname, './index.html'), 'utf8')
dom = new JSDOM(html, { runScripts: 'dangerously' })
document = dom.window.document
global.document = document
global.window = dom.window
global.localStorage = {
getItem: () => null,
setItem: () => {}
}
initApp()
})
it('should have a lang attribute set to "id"', () => {
expect(document.documentElement.getAttribute('lang')).toBe('id')
})
it('should include the Tailwind CDN script', () => {
const scripts = Array.from(document.querySelectorAll('script'))
const hasTailwind = scripts.some(script =>
script.getAttribute('src')?.includes('cdn.tailwindcss.com')
)
expect(hasTailwind).toBe(true)
})
it('should have a title containing "Fidyah"', () => {
expect(document.title).toContain('Fidyah')
})
describe('Calculator Layout', () => {
it('should have an input for "Days Missed"', () => {
const input = document.getElementById('days-missed')
expect(input).not.toBeNull()
expect(input.tagName).toBe('INPUT')
expect(input.type).toBe('number')
})
it('should have an input for "Years Elapsed"', () => {
const input = document.getElementById('years-elapsed')
expect(input).not.toBeNull()
expect(input.tagName).toBe('INPUT')
expect(input.type).toBe('number')
})
it('should have a display area for the total fidyah calculation', () => {
const display = document.getElementById('fidyah-result')
expect(display).not.toBeNull()
})
it('should have a select field for weight conversion (Qoul)', () => {
const select = document.getElementById('mudd-conversion')
expect(select).not.toBeNull()
expect(select.tagName).toBe('SELECT')
})
it('should have a theme toggle button', () => {
const themeToggle = document.getElementById('theme-toggle')
expect(themeToggle).not.toBeNull()
expect(themeToggle.tagName).toBe('BUTTON')
})
it('should have a calculation breakdown section', () => {
const breakdown = document.getElementById('calculation-breakdown')
expect(breakdown).not.toBeNull()
})
})
describe('UI Interaction and Calculation', () => {
it('should update results when days and years are changed', () => {
const daysInput = document.getElementById('days-missed')
const yearsInput = document.getElementById('years-elapsed')
const totalDaysDisplay = document.getElementById('total-days')
const resultArea = document.getElementById('fidyah-result')
// Set values
daysInput.value = '10'
yearsInput.value = '2' // multiplier is 2
// Trigger input event
daysInput.dispatchEvent(new dom.window.Event('input'))
// Should show calculated result: days × years = 10 × 2 = 20
expect(totalDaysDisplay.textContent).toBe('20')
expect(resultArea.classList.contains('hidden')).toBe(false)
})
it('should hide results if days missed is 0 or empty', () => {
const daysInput = document.getElementById('days-missed')
const resultArea = document.getElementById('fidyah-result')
daysInput.value = '0'
daysInput.dispatchEvent(new dom.window.Event('input'))
expect(resultArea.classList.contains('hidden')).toBe(true)
daysInput.value = ''
daysInput.dispatchEvent(new dom.window.Event('input'))
expect(resultArea.classList.contains('hidden')).toBe(true)
})
it('should show step-by-step calculation breakdown', () => {
const daysInput = document.getElementById('days-missed')
const yearsInput = document.getElementById('years-elapsed')
const priceInput = document.getElementById('price-per-kg')
const breakdownEl = document.getElementById('step-formula')
daysInput.value = '10'
yearsInput.value = '2'
priceInput.value = '15000'
daysInput.dispatchEvent(new dom.window.Event('input'))
// Linear: 10 × 2 = 20 mudds, 20 × 0.75 = 15 kg, 15 × 15000 = 225000
expect(breakdownEl.innerHTML).toContain('20 mudd')
expect(breakdownEl.innerHTML).toContain('225.000')
})
})
describe('Theme Toggle', () => {
it('should toggle dark class on body when theme button is clicked', () => {
const themeToggle = document.getElementById('theme-toggle')
const body = document.body
expect(body.classList.contains('dark')).toBe(false)
themeToggle.click()
expect(body.classList.contains('dark')).toBe(true)
expect(themeToggle.getAttribute('aria-pressed')).toBe('true')
themeToggle.click()
expect(body.classList.contains('dark')).toBe(false)
expect(themeToggle.getAttribute('aria-pressed')).toBe('false')
})
})
})