File size: 3,275 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as MockData from '../mock';
import { getSupportedPlans } from '../resolvers';
import { buildPlanFeaturesDict } from '../test-utils';
import type { PricedAPIPlan } from '../types';

// Don't need to mock specific functions for any tests, but mocking
// module because it accesses the `document` global.
jest.mock( 'wpcom-proxy-request', () => ( {
	__esModule: true,
} ) );

const MOCK_LOCALE = 'test-locale';

beforeEach( () => {
	jest.clearAllMocks();
} );

describe( 'Plans resolvers', () => {
	describe( 'getSupportedPlans', () => {
		it( 'should fetch APIs data and set features and plans data', () => {
			const iter = getSupportedPlans( MOCK_LOCALE );

			// Prepare stricter iterator types
			type IteratorReturnType = ReturnType< typeof iter.next >;
			type PlanPriceApiDataIterator = ( planPriceData: PricedAPIPlan[] ) => IteratorReturnType;
			type PlanDetailsApiDataIterator = ( { body: DetailsAPIResponse } ) => IteratorReturnType;

			// request to prices endpoint
			expect( iter.next().value ).toEqual( {
				request: {
					apiVersion: '1.5',
					path: '/plans',
					query: `locale=${ MOCK_LOCALE }`,
				},
				type: 'WPCOM_REQUEST',
			} );

			// request to plan details/features endpoint
			const planPriceApiData = [
				MockData.API_PLAN_PRICE_FREE,
				MockData.API_PLAN_PRICE_PREMIUM_ANNUALLY,
				MockData.API_PLAN_PRICE_PREMIUM_MONTHLY,
			];
			expect( ( iter.next as PlanPriceApiDataIterator )( planPriceApiData ).value ).toEqual( {
				type: 'FETCH_AND_PARSE',
				resource: `https://public-api.wordpress.com/wpcom/v2/plans/details?locale=${ MOCK_LOCALE }`,
				options: {
					credentials: 'omit',
					mode: 'cors',
				},
			} );

			// setPlans call
			const planDetailsApiData = {
				body: MockData.API_PLAN_DETAILS,
			};
			expect( ( iter.next as PlanDetailsApiDataIterator )( planDetailsApiData ).value ).toEqual( {
				locale: MOCK_LOCALE,
				type: 'SET_PLANS',
				plans: [ MockData.STORE_PLAN_FREE, MockData.STORE_PLAN_PREMIUM ],
			} );

			// setPlanProducts call
			expect( iter.next().value ).toEqual( {
				type: 'SET_PLAN_PRODUCTS',
				products: [
					MockData.STORE_PRODUCT_FREE,
					MockData.STORE_PRODUCT_PREMIUM_ANNUALLY,
					MockData.STORE_PRODUCT_PREMIUM_MONTHLY,
				],
			} );

			expect( iter.next().value ).toEqual( {
				locale: MOCK_LOCALE,
				type: 'SET_FEATURES',
				features: buildPlanFeaturesDict( [
					MockData.STORE_PLAN_FEATURE_CUSTOM_DOMAIN,
					MockData.STORE_PLAN_FEATURE_LIVE_SUPPORT,
					MockData.STORE_PLAN_FEATURE_PRIORITY_SUPPORT,
					MockData.STORE_PLAN_FEATURE_RECURRING_PAYMENTS,
					MockData.STORE_PLAN_FEATURE_WORDADS,
				] ),
			} );

			expect( iter.next().value ).toEqual( {
				locale: MOCK_LOCALE,
				type: 'SET_FEATURES_BY_TYPE',
				featuresByType: [
					MockData.API_FEATURES_BY_TYPE_GENERAL,
					MockData.API_FEATURES_BY_TYPE_COMMERCE,
					MockData.API_FEATURES_BY_TYPE_MARKETING,
				],
			} );
		} );

		it( 'should default to english locale', () => {
			const englishLocale = 'en';
			const iter = getSupportedPlans();

			// request to prices endpoint
			expect( iter.next().value ).toEqual( {
				request: {
					apiVersion: '1.5',
					path: '/plans',
					query: `locale=${ englishLocale }`,
				},
				type: 'WPCOM_REQUEST',
			} );
		} );
	} );
} );