Skip to content

Commit 6df3a49

Browse files
authored
Fix Sonarscan warnings and also achieve 100% test coverage
1 parent 74f384a commit 6df3a49

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function countUpFromTime(countFrom, id) {
1010
const now = new Date();
1111

1212
// Validate date
13-
const isValidDate = !isNaN(countFromDate.getTime());
13+
const isValidDate = !Number.isNaN(countFromDate.getTime());
1414

1515
// Get DOM element
1616
const idEl = document.getElementById(id);

src/style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ h2 {
2525
font-size: 2.5em;
2626
font-weight: 700;
2727
padding: 3rem;
28-
color: #3cbaf2;
28+
color: #2196F3;
2929
background-color: #0ff;
3030
background-color: rgba(0, 0, 0, .5);
3131
text-align: center;
@@ -74,7 +74,7 @@ h2 {
7474
gap: 20px;
7575
text-align: center;
7676
margin: 0 auto;
77-
color: #3cbaf2;
77+
color: #2196F3;
7878
background-color: rgba(0, 0, 0, .5);
7979
padding: 20px;
8080
max-width: 90%;

tests/script.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ describe('countUpFromTime', () => {
169169
expect(Number(mockEl.hours.innerHTML)).toBeGreaterThanOrEqual(0);
170170
});
171171

172+
test('covers for-loop else branch (break) when remainingDays < daysInYear', () => {
173+
// Freeze time to a known date so the loop runs exactly once and hits the else branch
174+
jest.useFakeTimers();
175+
jest.setSystemTime(new Date('Nov 01, 2025 12:00:00'));
176+
const mockEl = createMockDOM();
177+
global.document.getElementById = jest.fn(() => mockEl);
178+
// Choose a date in the previous year but with remaining days less than a full year
179+
script.countUpFromTime('Dec 31, 2024 00:00:00', 'countup1');
180+
// The function should have written numeric values to the DOM
181+
expect(Number(mockEl.years.innerHTML)).toBeGreaterThanOrEqual(0);
182+
expect(Number(mockEl.days.innerHTML)).toBeGreaterThanOrEqual(0);
183+
jest.useRealTimers();
184+
});
185+
172186
test('updates DOM elements with correct values', () => {
173187
jest.useFakeTimers();
174188
jest.setSystemTime(new Date('Sep 13, 2025 11:59:00'));

0 commit comments

Comments
 (0)