Created
May 30, 2025 08:58
-
-
Save citypaul/405ff94b5deacccffd85adfd4f46e4cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
So this is the general pattern I use: | |
const getMockPaymentPostPaymentRequest = (overrides?: Partial<PostPaymentsRequestV3>): PostPaymentsRequestV3 => { | |
return { | |
CardAccountId: '1234567890123456', | |
Amount: 100, | |
Source: 'Web', | |
AccountStatus: 'Normal', | |
LastName: 'Doe', | |
DateOfBirth: '1980-01-01', | |
PayingCardDetails: { | |
Cvv: '123', | |
Token: 'token', | |
}, | |
AddressDetails: { | |
HouseNumber: '123', | |
HouseName: 'Test House', | |
AddressLine1: 'Test Address Line 1', | |
AddressLine2: 'Test Address Line 2', | |
City: 'Test City', | |
}, | |
Brand: 'Visa', | |
...overrides, | |
}; | |
}; | |
Notice how we enforce the idea that we return the full object, but the function itself accepts an optional partial override. | |
This also scales well because say if we needed to do the same for AddressDetails later, I make changes incrementally as I need them. | |
So then you could do: | |
const getMockAddressDetails = (overrides?: Partial<AddressDetails>): AddressDetails => { | |
return { | |
HouseNumber: '123', | |
HouseName: 'Test House', | |
AddressLine1: 'Test Address Line 1', | |
AddressLine2: 'Test Address Line 2', | |
City: 'Test City', | |
...overrides, | |
}; | |
}; | |
const getMockPaymentPostPaymentRequest = (overrides?: Partial<PostPaymentsRequestV3>): PostPaymentsRequestV3 => { | |
return { | |
CardAccountId: '1234567890123456', | |
Amount: 100, | |
Source: 'Web', | |
AccountStatus: 'Normal', | |
LastName: 'Doe', | |
DateOfBirth: '1980-01-01', | |
PayingCardDetails: { | |
Cvv: '123', | |
Token: 'token', | |
}, | |
AddressDetails: getMockAddressDetails(), | |
Brand: 'Visa', | |
...overrides, | |
}; | |
}; | |
// Use cases: | |
// Address in isolation | |
const mockAddress = getMockAddressDetails({ City: 'London' }); | |
// Payment with custom address | |
const mockPayment = getMockPaymentPostPaymentRequest({ | |
AddressDetails: getMockAddressDetails({ City: 'Paris', HouseNumber: '456' }) | |
}); | |
// Payment with partial address override (merges with existing address) | |
const mockPaymentPartial = getMockPaymentPostPaymentRequest({ | |
Amount: 500, | |
AddressDetails: getMockAddressDetails({ City: 'Berlin' }) | |
}); | |
// Calling getMockPaymentPostPaymentRequest in isolation still returns the default address | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like this a lot - thank you for posting it!