Fix use of Date.now() in test

This commit is contained in:
Fedor Indutny
2025-12-01 10:58:32 -08:00
committed by GitHub
parent 7d253817f8
commit 7dae047d53
2 changed files with 16 additions and 14 deletions

View File

@@ -284,9 +284,18 @@ describe('DonationsCardForm', () => {
}); });
describe('parseCardExpiration', () => { describe('parseCardExpiration', () => {
const now = new Date('2025-02-03T04:05:06.078Z');
const currMonth = 2;
const currYear = 2025;
const lastMonth = 1;
const lastYear = 2024;
const nextMonth = 3;
const nextYear = 2026;
function checkValid(input: string, month: string, year: string) { function checkValid(input: string, month: string, year: string) {
it(`${JSON.stringify(input)} -> ${month}/${year} (valid)`, () => { it(`${JSON.stringify(input)} -> ${month}/${year} (valid)`, () => {
assert.deepEqual(parseCardExpiration(input), { month, year }); assert.deepEqual(parseCardExpiration(input, now), { month, year });
}); });
} }
@@ -297,7 +306,7 @@ describe('DonationsCardForm', () => {
year: string year: string
) { ) {
it(`${JSON.stringify(input)} -> ${error} (error with values)`, () => { it(`${JSON.stringify(input)} -> ${error} (error with values)`, () => {
assert.deepEqual(parseCardExpiration(input), { assert.deepEqual(parseCardExpiration(input, now), {
error, error,
month, month,
year, year,
@@ -307,19 +316,10 @@ describe('DonationsCardForm', () => {
function checkError(input: string, error: CardExpirationError) { function checkError(input: string, error: CardExpirationError) {
it(`${JSON.stringify(input)} -> ${error} (error)`, () => { it(`${JSON.stringify(input)} -> ${error} (error)`, () => {
assert.deepEqual(parseCardExpiration(input), { error }); assert.deepEqual(parseCardExpiration(input, now), { error });
}); });
} }
const now = new Date();
const currMonth = now.getMonth() + 1;
const currYear = now.getFullYear();
const lastMonth = currMonth - 1;
const lastYear = currYear - 1;
const nextMonth = currMonth + 1;
const nextYear = currYear + 1;
const mm = (month: number) => String(month).padStart(2, '0'); const mm = (month: number) => String(month).padStart(2, '0');
const yy = (year: number) => String(year - 2000).padStart(2, '0'); const yy = (year: number) => String(year - 2000).padStart(2, '0');
const yyyy = (year: number) => String(year).padStart(4, '0'); const yyyy = (year: number) => String(year).padStart(4, '0');

View File

@@ -230,7 +230,10 @@ function isValidMonth(value: number): boolean {
const CARD_EXPIRATION_MAX_YEARS_IN_FUTURE = 50; const CARD_EXPIRATION_MAX_YEARS_IN_FUTURE = 50;
export function parseCardExpiration(input: string): CardExpirationResult { export function parseCardExpiration(
input: string,
currentDate = new Date()
): CardExpirationResult {
// Trim whitespace and check if empty // Trim whitespace and check if empty
const trimmed = input.trim(); const trimmed = input.trim();
if (trimmed === '') { if (trimmed === '') {
@@ -299,7 +302,6 @@ export function parseCardExpiration(input: string): CardExpirationResult {
return { error: CardExpirationError.MONTH_OUT_OF_RANGE }; return { error: CardExpirationError.MONTH_OUT_OF_RANGE };
} }
const currentDate = new Date();
const currentYear = currentDate.getFullYear(); const currentYear = currentDate.getFullYear();
const currentMonth = currentDate.getMonth() + 1; const currentMonth = currentDate.getMonth() + 1;