File size: 13,129 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import * as deprecate from '@wordpress/deprecated';
import { TIMELESS_PLAN_ECOMMERCE, TIMELESS_PLAN_FREE, FREE_PLAN_PRODUCT_ID } from '../constants';
import * as MockData from '../mock';
import * as Selectors from '../selectors';
import { buildPlanFeaturesDict } from '../test-utils';
import type { State } from '../reducer';

// Test data
const MOCK_LOCALE_1 = 'test-locale-1';
const MOCK_LOCALE_2 = 'test-locale-2';

const mockFeatures = {
	[ MOCK_LOCALE_1 ]: buildPlanFeaturesDict( [
		MockData.STORE_PLAN_FEATURE_CUSTOM_DOMAIN,
		MockData.STORE_PLAN_FEATURE_LIVE_SUPPORT,
	] ),
	[ MOCK_LOCALE_2 ]: buildPlanFeaturesDict( [
		MockData.STORE_PLAN_FEATURE_PRIORITY_SUPPORT,
		MockData.STORE_PLAN_FEATURE_RECURRING_PAYMENTS,
	] ),
};
const mockFeaturesByType = {
	[ MOCK_LOCALE_1 ]: [
		MockData.API_FEATURES_BY_TYPE_GENERAL,
		MockData.API_FEATURES_BY_TYPE_COMMERCE,
	],
	[ MOCK_LOCALE_2 ]: [
		MockData.API_FEATURES_BY_TYPE_GENERAL,
		MockData.API_FEATURES_BY_TYPE_MARKETING,
	],
};
const mockPlans = {
	[ MOCK_LOCALE_1 ]: [ MockData.STORE_PLAN_FREE, MockData.STORE_PLAN_PREMIUM ],
	[ MOCK_LOCALE_2 ]: [ MockData.STORE_PLAN_FREE ],
};
const mockPlanProducts = [ MockData.STORE_PRODUCT_FREE, MockData.STORE_PRODUCT_PREMIUM_ANNUALLY ];

const mockState: State = {
	features: mockFeatures,
	featuresByType: mockFeaturesByType,
	plans: mockPlans,
	planProducts: mockPlanProducts,
};

// Mocks
jest.mock( '@wordpress/deprecated', () => {
	return jest.fn();
} );
jest.mock( '@wordpress/data', () => ( {
	// Mocking `select` allows unit tests to run in isolation for selectors,
	// without the corresponding resolvers being called
	select: () => ( {
		getPlansProducts: () => mockPlanProducts,
		getSupportedPlans: ( locale ) => mockPlans[ locale ] ?? [],
	} ),
} ) );

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

// Tests
describe( 'Plans selectors', () => {
	describe( 'getFeatures', () => {
		it( 'should select the right list of features for an given locale', () => {
			expect( Selectors.getFeatures( mockState, MOCK_LOCALE_1 ) ).toEqual(
				mockFeatures[ MOCK_LOCALE_1 ]
			);
			expect( Selectors.getFeatures( mockState, MOCK_LOCALE_2 ) ).toEqual(
				mockFeatures[ MOCK_LOCALE_2 ]
			);
		} );

		it( 'should return an empty object if the given locale does not exist in the store', () => {
			expect( Selectors.getFeatures( mockState, 'non-existing' ) ).toEqual( {} );
		} );
	} );

	describe( 'getFeaturesByType', () => {
		it( 'should select the right list of features by type for an given locale', () => {
			expect( Selectors.getFeaturesByType( mockState, MOCK_LOCALE_1 ) ).toEqual(
				mockFeaturesByType[ MOCK_LOCALE_1 ]
			);
			expect( Selectors.getFeaturesByType( mockState, MOCK_LOCALE_2 ) ).toEqual(
				mockFeaturesByType[ MOCK_LOCALE_2 ]
			);
		} );

		it( 'should return an empty array if the given locale does not exist in the store', () => {
			expect( Selectors.getFeaturesByType( mockState, 'non-existing' ) ).toEqual( [] );
		} );
	} );

	describe( 'getPlanByProductId', () => {
		it( 'should return undefined if the product ID is undefined', () => {
			expect( Selectors.getPlanByProductId( mockState, undefined, MOCK_LOCALE_1 ) ).toBeUndefined();
		} );

		it( 'should return undefined if the product ID is not associated to any products', () => {
			expect( Selectors.getPlanByProductId( mockState, -1, MOCK_LOCALE_1 ) ).toBeUndefined();
		} );

		it( 'should select the right product for a given correct product ID', () => {
			// Free
			expect(
				Selectors.getPlanByProductId(
					mockState,
					MockData.STORE_PRODUCT_FREE.productId,
					MOCK_LOCALE_1
				)
			).toEqual( MockData.STORE_PLAN_FREE );

			// Paid (annually billed)
			expect(
				Selectors.getPlanByProductId(
					mockState,
					MockData.STORE_PRODUCT_PREMIUM_ANNUALLY.productId,
					MOCK_LOCALE_1
				)
			).toEqual( MockData.STORE_PLAN_PREMIUM );

			// Paid (monthly billed)
			expect(
				Selectors.getPlanByProductId(
					mockState,
					MockData.STORE_PRODUCT_PREMIUM_MONTHLY.productId,
					MOCK_LOCALE_1
				)
			).toEqual( MockData.STORE_PLAN_PREMIUM );
		} );

		it( 'should return undefined if the product ID does not exist for the given locale', () => {
			expect(
				Selectors.getPlanByProductId(
					mockState,
					MockData.STORE_PRODUCT_PREMIUM_ANNUALLY.productId,
					MOCK_LOCALE_2
				)
			).toBeUndefined();
		} );
	} );

	describe( 'getPlanProductById', () => {
		it( 'should return undefined if the product ID is undefined', () => {
			expect( Selectors.getPlanProductById( mockState, undefined ) ).toBeUndefined();
		} );

		it( 'should return undefined if the product ID is not associated to any products', () => {
			// Non existing product
			expect( Selectors.getPlanProductById( mockState, -1 ) ).toBeUndefined();
		} );

		it( 'should select the right product for a given correct product ID', () => {
			// Free
			expect(
				Selectors.getPlanProductById( mockState, MockData.STORE_PRODUCT_FREE.productId )
			).toEqual( MockData.STORE_PRODUCT_FREE );

			// Paid (annually billed)
			expect(
				Selectors.getPlanProductById( mockState, MockData.STORE_PRODUCT_PREMIUM_ANNUALLY.productId )
			).toEqual( MockData.STORE_PRODUCT_PREMIUM_ANNUALLY );
		} );

		it( 'should return undefined if the product ID does not match any products in the store', () => {
			// Existing Product, but not in store (monthly billed)
			expect(
				Selectors.getPlanProductById( mockState, MockData.STORE_PRODUCT_PREMIUM_MONTHLY.productId )
			).toBeUndefined();
		} );
	} );

	describe( 'getPlanByPeriodAgnosticSlug', () => {
		it( 'should return undefined if the plan slug is undefined', () => {
			expect(
				Selectors.getPlanByPeriodAgnosticSlug( mockState, undefined, MOCK_LOCALE_1 )
			).toBeUndefined();
		} );

		it( 'should return undefined if the plan slug does not match any plans in the store', () => {
			expect(
				Selectors.getPlanByPeriodAgnosticSlug( mockState, 'ecommerce', MOCK_LOCALE_1 )
			).toBeUndefined();
		} );

		it( 'should select the right plan for a given, correct plan slug and locale', () => {
			// Locale 1
			expect(
				Selectors.getPlanByPeriodAgnosticSlug(
					mockState,
					MockData.STORE_PLAN_PREMIUM.periodAgnosticSlug,
					MOCK_LOCALE_1
				)
			).toEqual( MockData.STORE_PLAN_PREMIUM );

			// Locale 2
			expect(
				Selectors.getPlanByPeriodAgnosticSlug(
					mockState,
					MockData.STORE_PLAN_FREE.periodAgnosticSlug,
					MOCK_LOCALE_2
				)
			).toEqual( MockData.STORE_PLAN_FREE );
		} );

		it( 'should return undefined if the plan slug does not match any plans in the store for the given locale', () => {
			expect(
				Selectors.getPlanByPeriodAgnosticSlug(
					mockState,
					MockData.STORE_PLAN_PREMIUM.periodAgnosticSlug,
					MOCK_LOCALE_2
				)
			).toBeUndefined();
		} );
	} );

	describe( 'getDefaultPaidPlan', () => {
		it( 'should select the correct default paid plan for the given locale', () => {
			expect( Selectors.getDefaultPaidPlan( mockState, MOCK_LOCALE_1 ) ).toEqual(
				MockData.STORE_PLAN_PREMIUM
			);
		} );

		it( 'should return undefined if the default paid plan is not available in the store for a given locale', () => {
			expect( Selectors.getDefaultPaidPlan( mockState, MOCK_LOCALE_2 ) ).toBeUndefined();
		} );
	} );

	describe( 'getDefaultFreePlan', () => {
		it( 'should select the correct default free plan for the given locale', () => {
			expect( Selectors.getDefaultFreePlan( mockState, MOCK_LOCALE_1 ) ).toEqual(
				MockData.STORE_PLAN_FREE
			);
			expect( Selectors.getDefaultFreePlan( mockState, MOCK_LOCALE_2 ) ).toEqual(
				MockData.STORE_PLAN_FREE
			);
		} );
	} );

	describe( 'getSupportedPlans', () => {
		it( 'should select the supported plans for a given locale', () => {
			expect( Selectors.getSupportedPlans( mockState, MOCK_LOCALE_1 ) ).toEqual(
				mockPlans[ MOCK_LOCALE_1 ]
			);
			expect( Selectors.getSupportedPlans( mockState, MOCK_LOCALE_2 ) ).toEqual(
				mockPlans[ MOCK_LOCALE_2 ]
			);
		} );

		it( 'should return an empty list if the given locale does not exist in the store', () => {
			expect( Selectors.getSupportedPlans( mockState, 'non-existing' ) ).toEqual( [] );
		} );
	} );

	describe( 'getPlansProducts', () => {
		it( 'should select all of the plans products from the store', () => {
			expect( Selectors.getPlansProducts( mockState ) ).toEqual( mockPlanProducts );
		} );
	} );

	describe( 'getPrices', () => {
		it( 'should select the prices for each plan product for a given locale', () => {
			const expectedPrices = {
				[ MockData.STORE_PRODUCT_FREE.storeSlug ]: MockData.STORE_PRODUCT_FREE.price,
				[ MockData.STORE_PRODUCT_PREMIUM_ANNUALLY.storeSlug ]:
					MockData.STORE_PRODUCT_PREMIUM_ANNUALLY.price,
			};
			expect( Selectors.getPrices( mockState, MOCK_LOCALE_1 ) ).toEqual( expectedPrices );
			expect( Selectors.getPrices( mockState, MOCK_LOCALE_2 ) ).toEqual( expectedPrices );
		} );

		it( 'should be marked as deprecated', () => {
			Selectors.getPrices( mockState, MOCK_LOCALE_1 );

			expect( deprecate ).toHaveBeenCalled();
			expect( deprecate ).toHaveBeenCalledWith( 'getPrices', {
				alternative: 'getPlanProduct().price',
			} );
		} );
	} );

	describe( 'getPlanByPath', () => {
		it( 'should return undefined if the plan slug is undefined', () => {
			expect( Selectors.getPlanByPath( mockState, undefined, MOCK_LOCALE_1 ) ).toBeUndefined();
		} );

		it( 'should return undefined if the plan slug does not match any plans in the store', () => {
			expect( Selectors.getPlanByPath( mockState, 'ecommerce', MOCK_LOCALE_1 ) ).toBeUndefined();
		} );

		it( 'should select the correct plan for the given locale', () => {
			expect(
				Selectors.getPlanByPath(
					mockState,
					MockData.STORE_PRODUCT_PREMIUM_ANNUALLY.pathSlug,
					MOCK_LOCALE_1
				)
			).toEqual( MockData.STORE_PLAN_PREMIUM );
		} );

		it( 'should return undefined if the plan slug does not match any plans in the store for the given locale', () => {
			expect(
				Selectors.getPlanByPath(
					mockState,
					MockData.STORE_PRODUCT_PREMIUM_ANNUALLY.pathSlug,
					MOCK_LOCALE_2
				)
			).toBeUndefined();
		} );
	} );

	describe( 'getPlanProduct', () => {
		it( 'should return undefined if the plan slug is undefined', () => {
			expect( Selectors.getPlanProduct( mockState, undefined, 'ANNUALLY' ) ).toBeUndefined();
		} );

		it( 'should return undefined if the billing period is undefined', () => {
			expect(
				Selectors.getPlanProduct(
					mockState,
					MockData.STORE_PLAN_FREE.periodAgnosticSlug,
					undefined
				)
			).toBeUndefined();
		} );

		it( 'should select the free plan if the plan slug matches the free plan (regardless of the billing period)', () => {
			expect(
				Selectors.getPlanProduct(
					mockState,
					MockData.STORE_PLAN_FREE.periodAgnosticSlug,
					'ANNUALLY'
				)
			).toEqual( MockData.STORE_PRODUCT_FREE );
			expect(
				Selectors.getPlanProduct(
					mockState,
					MockData.STORE_PLAN_FREE.periodAgnosticSlug,
					'MONTHLY'
				)
			).toEqual( MockData.STORE_PRODUCT_FREE );
		} );

		it( 'should select the correct paid plan given the billing period', () => {
			expect(
				Selectors.getPlanProduct(
					mockState,
					MockData.STORE_PLAN_PREMIUM.periodAgnosticSlug,
					'ANNUALLY'
				)
			).toEqual( MockData.STORE_PRODUCT_PREMIUM_ANNUALLY );
		} );

		it( 'should return undefined if there is not math in the store for the given plan slug and billing period', () => {
			expect(
				Selectors.getPlanProduct(
					mockState,
					MockData.STORE_PLAN_PREMIUM.periodAgnosticSlug,
					'MONTHLY'
				)
			).toBeUndefined();
		} );
	} );

	describe( 'isPlanEcommerce', () => {
		it( 'should return true if the eCommerce plan slug is passed', () => {
			expect( Selectors.isPlanEcommerce( mockState, TIMELESS_PLAN_ECOMMERCE ) ).toBe( true );
		} );
		it( 'should return false if the free plan slug is passed', () => {
			expect( Selectors.isPlanEcommerce( mockState, TIMELESS_PLAN_FREE ) ).toBe( false );
		} );
		it( 'should return false if the plan slug is undefined', () => {
			expect( Selectors.isPlanEcommerce( mockState ) ).toBe( false );
		} );
	} );

	describe( 'isPlanFree', () => {
		it( 'should return false if the eCommerce plan slug is passed', () => {
			expect( Selectors.isPlanFree( mockState, TIMELESS_PLAN_ECOMMERCE ) ).toBe( false );
		} );
		it( 'should return true if the free plan slug is passed', () => {
			expect( Selectors.isPlanFree( mockState, TIMELESS_PLAN_FREE ) ).toBe( true );
		} );
		it( 'should return false if the plan slug is undefined', () => {
			expect( Selectors.isPlanFree( mockState ) ).toBe( false );
		} );
	} );

	describe( 'isPlanProductFree', () => {
		it( 'should return true if the free plan product ID is passed', () => {
			expect( Selectors.isPlanProductFree( mockState, FREE_PLAN_PRODUCT_ID ) ).toBe( true );
		} );
		it( 'should return false if a product ID not associated to the free plan is passed', () => {
			expect( Selectors.isPlanProductFree( mockState, 2 ) ).toBe( false );
		} );
		it( 'should return false if the product ID is undefined', () => {
			expect( Selectors.isPlanProductFree( mockState ) ).toBe( false );
		} );
	} );
} );