-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathuse-eligibility.test.ts
More file actions
108 lines (79 loc) · 3.34 KB
/
Copy pathuse-eligibility.test.ts
File metadata and controls
108 lines (79 loc) · 3.34 KB
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
/**
* @jest-environment jsdom
*/
import { renderHook } from '@testing-library/react';
import { getPurchasesError } from 'calypso/state/purchases/selectors';
import { getSiteOption } from 'calypso/state/sites/selectors';
import useStatsPurchases from '../../../hooks/use-stats-purchases';
import useIsPricingGridEligible from '../use-eligibility';
jest.mock( 'calypso/state', () => ( {
useSelector: jest.fn( ( selector ) => selector( {} ) ),
} ) );
jest.mock( 'calypso/state/purchases/selectors' );
jest.mock( 'calypso/state/sites/selectors' );
jest.mock( '../../../hooks/use-stats-purchases' );
const SITE_ID = 123;
const POST_LAUNCH_DATE = '2026-09-01T00:00:00+00:00';
const PRE_LAUNCH_DATE = '2026-01-01T00:00:00+00:00';
function mockSite( {
connectedAt = POST_LAUNCH_DATE as string | number | null,
hasAnyPlan = false,
isLoadingPurchases = false,
purchasesError = null as object | null,
} = {} ) {
( getSiteOption as jest.Mock ).mockReturnValue( connectedAt );
( getPurchasesError as unknown as jest.Mock ).mockReturnValue( purchasesError );
( useStatsPurchases as jest.Mock ).mockReturnValue( {
hasAnyPlan,
isLoading: isLoadingPurchases,
} );
}
describe( 'useIsPricingGridEligible', () => {
beforeEach( () => {
jest.clearAllMocks();
} );
it( 'is eligible for a newly connected site without a plan', () => {
mockSite();
const { result } = renderHook( () => useIsPricingGridEligible( SITE_ID ) );
expect( result.current ).toEqual( {
isEligible: true,
isNewConnection: true,
isLoading: false,
} );
} );
it( 'is not eligible for a site connected before launch', () => {
mockSite( { connectedAt: PRE_LAUNCH_DATE } );
const { result } = renderHook( () => useIsPricingGridEligible( SITE_ID ) );
expect( result.current.isEligible ).toBe( false );
expect( result.current.isNewConnection ).toBe( false );
} );
it( 'is not eligible when the site already holds a Stats plan', () => {
mockSite( { hasAnyPlan: true } );
const { result } = renderHook( () => useIsPricingGridEligible( SITE_ID ) );
expect( result.current.isEligible ).toBe( false );
} );
it( 'fails closed when the purchases fetch errored', () => {
mockSite( { purchasesError: { error: 'fetch_failed' } } );
const { result } = renderHook( () => useIsPricingGridEligible( SITE_ID ) );
expect( result.current.isEligible ).toBe( false );
} );
it( 'accepts a connection date given as unix seconds', () => {
mockSite( { connectedAt: Date.parse( POST_LAUNCH_DATE ) / 1000 } );
const { result } = renderHook( () => useIsPricingGridEligible( SITE_ID ) );
expect( result.current.isNewConnection ).toBe( true );
} );
it( 'is not eligible when the connection date is missing', () => {
mockSite( { connectedAt: null } );
const { result } = renderHook( () => useIsPricingGridEligible( SITE_ID ) );
expect( result.current.isEligible ).toBe( false );
expect( result.current.isLoading ).toBe( false );
} );
it( 'only reports loading for newly connected sites', () => {
mockSite( { isLoadingPurchases: true } );
const { result } = renderHook( () => useIsPricingGridEligible( SITE_ID ) );
expect( result.current.isLoading ).toBe( true );
mockSite( { connectedAt: PRE_LAUNCH_DATE, isLoadingPurchases: true } );
const { result: preLaunch } = renderHook( () => useIsPricingGridEligible( SITE_ID ) );
expect( preLaunch.current.isLoading ).toBe( false );
} );
} );