Skip to content

Commit

Permalink
fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskmnds committed Oct 16, 2024
1 parent b4b54d8 commit c1b1972
Showing 1 changed file with 123 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
* Default mock implementations
*/
jest.mock( '@wordpress/data' );
jest.mock( '@automattic/data-stores', () => ( {
Plans: {
usePlans: jest.fn(),
useSitePlans: jest.fn(),
useIntroOffers: jest.fn(),
useCurrentPlan: jest.fn(),
},
Purchases: {
useSitePurchaseById: jest.fn(),
},
} ) );
// jest.mock( '@automattic/data-stores', () => ( {
// Plans: {
// usePlans: jest.fn(),
// useSitePlans: jest.fn(),
// useIntroOffers: jest.fn(),
// useCurrentPlan: jest.fn(),
// },
// Purchases: {
// useSitePurchaseById: jest.fn(),
// },
// } ) );

jest.mock( '../../', () => ( {
usePlans: jest.fn(),
Expand All @@ -32,8 +32,44 @@ jest.mock( '../../../purchases', () => ( {
import { PLAN_PERSONAL, PLAN_PREMIUM } from '@automattic/calypso-products';
import * as Plans from '../../';
import * as Purchases from '../../../purchases';
import { COST_OVERRIDE_REASONS } from '../../constants';
import usePricingMetaForGridPlans from '../use-pricing-meta-for-grid-plans';

const SITE_PLANS = {
[ PLAN_PREMIUM ]: {
expiry: null,
pricing: {
billPeriod: 365,
currencyCode: 'USD',
originalPrice: {
full: 500,
monthly: 500,
},
discountedPrice: {
full: 250,
monthly: 250,
},
},
},
};

const PLANS = {
[ PLAN_PREMIUM ]: {
pricing: {
billPeriod: 365,
currencyCode: 'USD',
originalPrice: {
full: 500,
monthly: 500,
},
discountedPrice: {
full: 400,
monthly: 400,
},
},
},
};

describe( 'usePricingMetaForGridPlans', () => {
beforeEach( () => {
jest.clearAllMocks();
Expand All @@ -43,49 +79,75 @@ describe( 'usePricingMetaForGridPlans', () => {
} ) );
Plans.usePlans.mockImplementation( () => ( {
isLoading: false,
data: {
[ PLAN_PREMIUM ]: {
pricing: {
billPeriod: 365,
currencyCode: 'USD',
originalPrice: {
full: 500,
monthly: 500,
},
discountedPrice: {
full: 400,
monthly: 400,
},
},
},
data: PLANS,
} ) );
Plans.useSitePlans.mockImplementation( () => ( {
isLoading: false,
data: SITE_PLANS,
} ) );
Plans.useCurrentPlan.mockImplementation( () => undefined );
} );

it( 'should return intro offer when available', () => {
const introOffer = {
formattedPrice: '$150',
rawPrice: {
monthly: 12.5,
full: 150,
},
isOfferComplete: false,
intervalUnit: 'year',
intervalCount: 1,
};

const useCheckPlanAvailabilityForPurchase = () => {
return {
[ PLAN_PREMIUM ]: true,
};
};

Plans.useIntroOffers.mockImplementation( () => ( {
[ PLAN_PREMIUM ]: introOffer,
} ) );
Plans.useSitePlans.mockImplementation( () => ( {
isLoading: false,
data: {
[ PLAN_PREMIUM ]: {
expiry: null,
...SITE_PLANS[ PLAN_PREMIUM ],
pricing: {
billPeriod: 365,
currencyCode: 'USD',
originalPrice: {
full: 500,
monthly: 500,
},
discountedPrice: {
full: 250,
monthly: 250,
},
costOverrides: [
{
overrideCode: 'recent-plan-proration',
},
],
...SITE_PLANS[ PLAN_PREMIUM ].pricing,
introOffer,
},
},
},
} ) );
Plans.useCurrentPlan.mockImplementation( () => undefined );

const pricingMeta = usePricingMetaForGridPlans( {
planSlugs: [ PLAN_PREMIUM ],
storageAddOns: null,
siteId: 100,
coupon: undefined,
useCheckPlanAvailabilityForPurchase,
} );

const expectedPricingMeta = {
[ PLAN_PREMIUM ]: {
originalPrice: {
full: 500,
monthly: 500,
},
discountedPrice: {
full: 250,
monthly: 250,
},
billingPeriod: 365,
currencyCode: 'USD',
expiry: null,
introOffer,
},
};

expect( pricingMeta ).toEqual( expectedPricingMeta );
} );

it( 'should return the original price as the site plan price and discounted price as Null for the current plan', () => {
Expand Down Expand Up @@ -121,7 +183,7 @@ describe( 'usePricingMetaForGridPlans', () => {
billingPeriod: 365,
currencyCode: 'USD',
expiry: null,
introOffer: null,
introOffer: undefined,
},
};

Expand Down Expand Up @@ -161,7 +223,7 @@ describe( 'usePricingMetaForGridPlans', () => {
billingPeriod: 365,
currencyCode: 'USD',
expiry: null,
introOffer: null,
introOffer: undefined,
},
};

Expand Down Expand Up @@ -202,7 +264,7 @@ describe( 'usePricingMetaForGridPlans', () => {
billingPeriod: 365,
currencyCode: 'USD',
expiry: null,
introOffer: null,
introOffer: undefined,
},
};

Expand Down Expand Up @@ -244,7 +306,7 @@ describe( 'usePricingMetaForGridPlans', () => {
billingPeriod: 365,
currencyCode: 'USD',
expiry: null,
introOffer: null,
introOffer: undefined,
},
};

Expand All @@ -257,6 +319,19 @@ describe( 'usePricingMetaForGridPlans', () => {
planSlug: PLAN_PERSONAL,
} ) );

Plans.useSitePlans.mockImplementation( () => ( {
isLoading: false,
data: {
[ PLAN_PREMIUM ]: {
...SITE_PLANS[ PLAN_PREMIUM ],
pricing: {
...SITE_PLANS[ PLAN_PREMIUM ].pricing,
costOverrides: [ { overrideCode: COST_OVERRIDE_REASONS.RECENT_PLAN_PRORATION } ],
},
},
},
} ) );

const useCheckPlanAvailabilityForPurchase = () => {
return {
[ PLAN_PREMIUM ]: true,
Expand Down Expand Up @@ -286,7 +361,7 @@ describe( 'usePricingMetaForGridPlans', () => {
billingPeriod: 365,
currencyCode: 'USD',
expiry: null,
introOffer: null,
introOffer: undefined,
},
};

Expand Down

0 comments on commit c1b1972

Please sign in to comment.