mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2025-12-20 02:08:57 +00:00
Fix use of Date.now() in test
This commit is contained in:
@@ -284,9 +284,18 @@ describe('DonationsCardForm', () => {
|
||||
});
|
||||
|
||||
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) {
|
||||
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
|
||||
) {
|
||||
it(`${JSON.stringify(input)} -> ${error} (error with values)`, () => {
|
||||
assert.deepEqual(parseCardExpiration(input), {
|
||||
assert.deepEqual(parseCardExpiration(input, now), {
|
||||
error,
|
||||
month,
|
||||
year,
|
||||
@@ -307,19 +316,10 @@ describe('DonationsCardForm', () => {
|
||||
|
||||
function checkError(input: string, error: CardExpirationError) {
|
||||
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 yy = (year: number) => String(year - 2000).padStart(2, '0');
|
||||
const yyyy = (year: number) => String(year).padStart(4, '0');
|
||||
|
||||
@@ -230,7 +230,10 @@ function isValidMonth(value: number): boolean {
|
||||
|
||||
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
|
||||
const trimmed = input.trim();
|
||||
if (trimmed === '') {
|
||||
@@ -299,7 +302,6 @@ export function parseCardExpiration(input: string): CardExpirationResult {
|
||||
return { error: CardExpirationError.MONTH_OUT_OF_RANGE };
|
||||
}
|
||||
|
||||
const currentDate = new Date();
|
||||
const currentYear = currentDate.getFullYear();
|
||||
const currentMonth = currentDate.getMonth() + 1;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user