diff --git a/js/test/util/current_date.test.js b/js/test/util/current_date.test.js new file mode 100644 index 000000000..b324ad148 --- /dev/null +++ b/js/test/util/current_date.test.js @@ -0,0 +1,35 @@ +import { expect } from 'chai'; +import sinon from 'sinon'; + +import currentDate from '../../util/current_date'; + +describe('current_date', () => { + const { getCurrentDate } = currentDate; + + let clock; + + afterEach(() => { + if (clock) { + clock.restore(); + clock = null; + } + }); + + it('zero-pads a single-digit day of month', () => { + // June 7, 2026 + clock = sinon.useFakeTimers(new Date(2026, 5, 7).getTime()); + expect(getCurrentDate('-')).to.equal('2026-06-07'); + }); + + it('zero-pads single-digit month and day together', () => { + // January 5, 2026 + clock = sinon.useFakeTimers(new Date(2026, 0, 5).getTime()); + expect(getCurrentDate('-')).to.equal('2026-01-05'); + }); + + it('leaves a two-digit day unchanged', () => { + // June 15, 2026 + clock = sinon.useFakeTimers(new Date(2026, 5, 15).getTime()); + expect(getCurrentDate('-')).to.equal('2026-06-15'); + }); +}); diff --git a/js/util/current_date.js b/js/util/current_date.js index f8843549b..8e083c8fd 100644 --- a/js/util/current_date.js +++ b/js/util/current_date.js @@ -4,7 +4,7 @@ function getCurrentDate(separator = '') { const month = newDate.getMonth() + 1; const year = newDate.getFullYear(); - return `${year}${separator}${month < 10 ? `0${month}` : `${month}`}${separator}${date}`; + return `${year}${separator}${month < 10 ? `0${month}` : `${month}`}${separator}${date < 10 ? `0${date}` : `${date}`}`; } export default {