Setting the file. One moment.
Donation Contract Test · Wix Headless Templates · wix/skills · Skills Docs
ContentsBack to the top of the page (opens in a new tab)
donations/tests/ donation-contract.test.mjs
JavaScript · 71 lines · 4 KB
[
name
,
api
]
of
Object.
entries
({ source, project })) {
9 test ( `${ name }: campaign decimal strings become numeric one-time cart options` , () => {
10 const body = api. cartBody ( 'campaign-id' , { amount: '25.00' , frequency: 'ONE_TIME' , coverFee: false , note: '' });
11 assert. deepEqual (body, {
12 cart: { source: { channelType: 'WEB' } },
13 catalogItems: [{ quantity: 1 , catalogReference: {
14 appId: '333b456e-dd48-4d6b-b32b-9fd48d74e163' ,
15 catalogItemId: 'campaign-id' , options: { amount: 25 },
16 } }],
17 });
18 assert. equal ( typeof JSON . parse ( JSON . stringify (body)).catalogItems[ 0 ].catalogReference.options.amount, 'number' );
19 });
20
21 for ( const frequency of [ 'WEEK' , 'MONTH' , 'YEAR' ]) {
22 test ( `${ name }: ${ frequency } and optional fee flag survive without inflating base amount` , () => {
23 const body = api. cartBody ( 'campaign-id' , { amount: 50 , frequency, coverFee: true , note: 'In memory' });
24 assert. deepEqual (body.catalogItems[ 0 ].catalogReference.options, { amount: 50 , frequency, donorCoveringFees: true });
25 assert. equal (body.cart.note, 'In memory' );
26 });
27 }
28
29 test ( `${ name }: invalid inputs cannot reach cart creation` , () => {
30 for ( const amount of [ '' , ' ' , 'not-money' , 0 , - 1 , NaN , Infinity , null , true , {}]) {
31 assert. throws (() => api. donationLineItem ( 'campaign-id' , { amount, frequency: 'ONE_TIME' }), /positive donation amount/ );
32 }
33 assert. throws (() => api. donationLineItem ( 'campaign-id' , { amount: 25 , frequency: 'QUARTER' }), /supported donation frequency/ );
34 assert. throws (() => api. donationLineItem ( '' , { amount: 25 , frequency: 'ONE_TIME' }), /campaign id/ );
35 });
36
37 test ( `${ name }: Cart V2 id is passed directly to hosted checkout, with separate callbacks` , () => {
38 const id = api. cartIdOf ({ cart: { id: 'cart-id' } });
39 assert. equal (api. cartIdOf ({ _id: 'cart-id' }), id);
40 assert. deepEqual (api. redirectBody (id, 'campaign-id' , 'https://donor.example.test' ), {
41 ecomCheckout: { checkoutId: 'cart-id' },
42 callbacks: { postFlowUrl: 'https://donor.example.test/donate/campaign-id' , thankYouPageUrl: 'https://donor.example.test/donate/thank-you' },
43 });
44 assert. throws (() => api. cartIdOf ({}), /no cart id/ );
45 });
46
47 test ( `${ name }: payment-related API errors keep their actual cause, status and body` , () => {
48 const error = Object. assign ( new Error ( 'Unsupported PAYMENT_METHOD' ), { status: 400 , body: { code: 'PAYMENT_METHOD_UNSUPPORTED' } });
49 assert. equal (api. checkoutError (error), error);
50 assert. equal (api. checkoutError (error, 'redirect' , 'cart-id' ), error);
51 assert. equal (error.stage, 'redirect' );
52 assert. equal (error.cartId, 'cart-id' );
53 assert. deepEqual (error.body, { code: 'PAYMENT_METHOD_UNSUPPORTED' });
54 assert. equal (api. checkoutError ( 'network failure' ).cause, 'network failure' );
55 });
56
57 test ( `${ name }: unavailable goal metrics differ from a verified zero and from an exceeded goal` , () => {
58 const goal = { targetAmount: { amount: '100' } };
59 assert. equal (api. toGoal (goal, null , 'USD' ).raised, '' );
60 assert. notEqual (api. toGoal (goal, [], 'USD' ).raised, '' );
61 const known = api. toGoal (goal, [{ currencyCode: 'USD' , donationCount: 3 , totalAmount: { amount: '125' } }], 'USD' );
62 assert. equal (known.percent, 125 );
63 assert. equal (known.donationCount, 3 );
64 assert. equal (api. toGoal ( null , [], 'USD' ), null );
65 });
66 }
67
68 test ( 'scaffold and deployable donation payloads remain identical' , () => {
69 const input = { amount: 37.5 , frequency: 'MONTH' , coverFee: true , note: 'A message' };
70 assert. deepEqual (source. cartBody ( 'campaign-id' , input), project. cartBody ( 'campaign-id' , input));
71 });