Skip to content

Commit

Permalink
Merge pull request #518 from kiva/CIT-2542_add_metadata_donation_input
Browse files Browse the repository at this point in the history
feat: add metadata input to donation mutation
  • Loading branch information
eddieferrer authored Feb 12, 2025
2 parents 652c867 + dd37e7f commit a918aac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 8 additions & 4 deletions @kiva/kv-shop/src/basketItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { callShopMutation } from './shopQueries';

export interface SetTipDonationOptions {
amount: string | number,
metadata?: string | null,
apollo: ApolloClient<any>,
}

Expand All @@ -14,29 +15,32 @@ export interface SetTipDonationData {
id: string,
price: string,
isTip: boolean,
metadata: string | null,
} | null,
} | null,
}

export async function setTipDonation({ amount, apollo }: SetTipDonationOptions) {
export async function setTipDonation({ amount, apollo, metadata }: SetTipDonationOptions) {
const donationAmount = numeral(amount).format('0.00');
const data = await callShopMutation<SetTipDonationData>(apollo, {
awaitRefetchQueries: true,
mutation: gql`mutation setTipDonation($price: Money!, $basketId: String) {
mutation: gql`mutation setTipDonation($price: Money!, $basketId: String, $metadata: String) {
shop (basketId: $basketId) {
id
updateDonation (donation: {
price: $price,
isTip: true
isTip: true,
metadata: $metadata,
})
{
id
price
isTip
metadata
}
}
}`,
variables: { price: donationAmount },
variables: { price: donationAmount, metadata },
});

return data?.shop?.updateDonation;
Expand Down
12 changes: 12 additions & 0 deletions @kiva/kv-shop/src/tests/basketItems.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ describe('basketItem.ts', () => {
expect(donation).toEqual({ id: 123 });
});

it('should set tip donation with metadata', async () => {
const apollo = new MockApolloClient({ data: { shop: { updateDonation: { id: 123 } } } });
const donation = await setTipDonation({ amount: 25, metadata: 'test metadata', apollo });
expect(apollo.mutate).toHaveBeenCalledWith({
mutation: expect.anything(),
awaitRefetchQueries: true,
refetchQueries: expect.anything(),
variables: { basketId: '', price: '25.00', metadata: 'test metadata' },
});
expect(donation).toEqual({ id: 123 });
});

it('retry then throw an error for missing baskets', async () => {
const apollo = new MockApolloClient({ data: {}, errors: [{ code: 'shop.basketRequired' }] });
expect(setTipDonation({ amount: 25, apollo })).rejects.toThrow('There was a problem with your basket.');
Expand Down

0 comments on commit a918aac

Please sign in to comment.