File size: 7,121 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
// Test resources
import {
	FEATURE_SOCIAL_SHARES_1000,
	WPCOM_FEATURES_AKISMET,
	WPCOM_FEATURES_BACKUPS,
	WPCOM_FEATURES_INSTANT_SEARCH,
	WPCOM_FEATURES_SCAN,
	WPCOM_FEATURES_VIDEOPRESS,
} from '@automattic/calypso-products';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import getSiteSlug from 'calypso/state/sites/selectors/get-site-slug';
import isBackupPluginActive from 'calypso/state/sites/selectors/is-backup-plugin-active';
import isJetpackSite from 'calypso/state/sites/selectors/is-jetpack-site';
import isJetpackSiteMultiSite from 'calypso/state/sites/selectors/is-jetpack-site-multi-site';
import isSearchPluginActive from 'calypso/state/sites/selectors/is-search-plugin-active';
// Mocks
jest.mock( 'calypso/state/selectors/is-site-wpcom-atomic' );
jest.mock( 'calypso/state/selectors/site-has-feature' );
jest.mock( 'calypso/state/sites/selectors/get-site-slug' );
jest.mock( 'calypso/state/sites/selectors/is-backup-plugin-active' );
jest.mock( 'calypso/state/sites/selectors/is-jetpack-site' );
jest.mock( 'calypso/state/sites/selectors/is-jetpack-site-multi-site' );
jest.mock( 'calypso/state/sites/selectors/is-search-plugin-active' );
// Functions under test
import { getLandingPath, isSiteEligibleForJetpackCloud } from '../selectors';

const FAKE_SITE_SLUG = 'mysite.example';

const mockSiteFeatures =
	( ...featuresToMock ) =>
	( state, siteId, featureToTest ) =>
		featuresToMock.includes( featureToTest );

describe( 'getLandingPath', () => {
	beforeEach( () => {
		jest.resetAllMocks();
	} );

	it.each( [ [ undefined ], [ null ], [ Infinity ], [ 4.2 ], [ true ], [ 'abc' ] ] )(
		'should return /landing if an integer site ID is not specified',
		( siteId ) => {
			isJetpackSite.mockReturnValue( true );
			getSiteSlug.mockReturnValue( FAKE_SITE_SLUG );

			const landingPath = getLandingPath( {}, siteId );
			expect( landingPath ).toEqual( '/landing' );
		}
	);

	it( 'should return /landing for ineligible sites', () => {
		isJetpackSite.mockReturnValue( false );

		const landingPath = getLandingPath( {}, 0 );
		expect( landingPath ).toEqual( '/landing' );
	} );

	it( 'should return /backup/<site> for eligible sites with Backup', () => {
		isJetpackSite.mockReturnValue( true );
		getSiteSlug.mockReturnValue( FAKE_SITE_SLUG );
		siteHasFeature.mockImplementation( mockSiteFeatures( WPCOM_FEATURES_BACKUPS ) );

		const landingPath = getLandingPath( {}, 0 );
		expect( landingPath ).toEqual( `/backup/${ FAKE_SITE_SLUG }` );
	} );

	it.each( [
		[ WPCOM_FEATURES_SCAN ],
		[ WPCOM_FEATURES_INSTANT_SEARCH ],
		[ FEATURE_SOCIAL_SHARES_1000 ],
		[ WPCOM_FEATURES_SCAN, WPCOM_FEATURES_INSTANT_SEARCH, FEATURE_SOCIAL_SHARES_1000 ],
	] )(
		'should return /backup/<site> for eligible sites with Backup and other features',
		( otherFeatures ) => {
			isJetpackSite.mockReturnValue( true );
			getSiteSlug.mockReturnValue( FAKE_SITE_SLUG );
			siteHasFeature.mockImplementation(
				mockSiteFeatures( WPCOM_FEATURES_BACKUPS, ...otherFeatures )
			);

			const landingPath = getLandingPath( {}, 0 );
			expect( landingPath ).toEqual( `/backup/${ FAKE_SITE_SLUG }` );
		}
	);

	it( 'should return /scan/<site> for sites with Scan but not Backup', () => {
		isJetpackSite.mockReturnValue( true );
		getSiteSlug.mockReturnValue( FAKE_SITE_SLUG );
		siteHasFeature.mockImplementation( mockSiteFeatures( WPCOM_FEATURES_SCAN ) );

		const landingPath = getLandingPath( {}, 0 );
		expect( landingPath ).toEqual( `/scan/${ FAKE_SITE_SLUG }` );
	} );

	it.each( [
		[ WPCOM_FEATURES_INSTANT_SEARCH ],
		[ FEATURE_SOCIAL_SHARES_1000 ],
		[ WPCOM_FEATURES_INSTANT_SEARCH, FEATURE_SOCIAL_SHARES_1000 ],
	] )(
		'should return /scan/<site> for eligible sites with Scan and other non-Backup features',
		( otherFeatures ) => {
			isJetpackSite.mockReturnValue( true );
			getSiteSlug.mockReturnValue( FAKE_SITE_SLUG );
			siteHasFeature.mockImplementation(
				mockSiteFeatures( WPCOM_FEATURES_SCAN, ...otherFeatures )
			);

			const landingPath = getLandingPath( {}, 0 );
			expect( landingPath ).toEqual( `/scan/${ FAKE_SITE_SLUG }` );
		}
	);

	it( 'should return /jetpack-search/<site> for sites with Search but not Scan or Backup', () => {
		isJetpackSite.mockReturnValue( true );
		getSiteSlug.mockReturnValue( FAKE_SITE_SLUG );
		siteHasFeature.mockImplementation( mockSiteFeatures( WPCOM_FEATURES_INSTANT_SEARCH ) );

		const landingPath = getLandingPath( {}, 0 );
		expect( landingPath ).toEqual( `/jetpack-search/${ FAKE_SITE_SLUG }` );
	} );

	it( 'should return /jetpack-search/<site> for sites with Search and Social', () => {
		isJetpackSite.mockReturnValue( true );
		getSiteSlug.mockReturnValue( FAKE_SITE_SLUG );
		siteHasFeature.mockImplementation(
			mockSiteFeatures( WPCOM_FEATURES_INSTANT_SEARCH, FEATURE_SOCIAL_SHARES_1000 )
		);

		const landingPath = getLandingPath( {}, 0 );
		expect( landingPath ).toEqual( `/jetpack-search/${ FAKE_SITE_SLUG }` );
	} );

	it( 'should return /jetpack-social/<site> for sites with Social but not Backup, Scan, or Search', () => {
		isJetpackSite.mockReturnValue( true );
		getSiteSlug.mockReturnValue( FAKE_SITE_SLUG );
		siteHasFeature.mockImplementation( mockSiteFeatures( FEATURE_SOCIAL_SHARES_1000 ) );

		const landingPath = getLandingPath( {}, 0 );
		expect( landingPath ).toEqual( `/jetpack-social/${ FAKE_SITE_SLUG }` );
	} );

	it.each( [ [ WPCOM_FEATURES_AKISMET ], [ WPCOM_FEATURES_VIDEOPRESS ] ] )(
		'should return /backup/<site> for sites with any other feature set',
		( featureSet ) => {
			isJetpackSite.mockReturnValue( true );
			getSiteSlug.mockReturnValue( FAKE_SITE_SLUG );
			siteHasFeature.mockImplementation( mockSiteFeatures( ...featureSet ) );

			const landingPath = getLandingPath( {}, 0 );
			expect( landingPath ).toEqual( `/backup/${ FAKE_SITE_SLUG }` );
		}
	);

	it( 'should return /backup/<site> when site features are unavailable', () => {
		isJetpackSite.mockReturnValue( true );
		getSiteSlug.mockReturnValue( FAKE_SITE_SLUG );
		siteHasFeature.mockReturnValue( false );

		const landingPath = getLandingPath( {}, 0 );
		expect( landingPath ).toEqual( `/backup/${ FAKE_SITE_SLUG }` );
	} );
} );

describe( 'isSiteEligibleForJetpackCloud', () => {
	beforeEach( () => {
		jest.resetAllMocks();
	} );

	it( 'should return false for multisites', () => {
		isJetpackSite.mockReturnValue( true );
		isJetpackSiteMultiSite.mockReturnValue( true );

		expect( isSiteEligibleForJetpackCloud( {}, 0 ) ).toEqual( false );
	} );

	it( 'should return true for single non-Atomic sites with an active combined Jetpack plugin', () => {
		isJetpackSite.mockReturnValue( true );

		expect( isSiteEligibleForJetpackCloud( {}, 0 ) ).toEqual( true );
	} );

	it( 'should return true for single non-Atomic sites with an active Backup plugin', () => {
		isBackupPluginActive.mockReturnValue( true );

		expect( isSiteEligibleForJetpackCloud( {}, 0 ) ).toEqual( true );
	} );

	it( 'should return true for single non-Atomic sites with an active Jetpack Search plugin', () => {
		isSearchPluginActive.mockReturnValue( true );

		expect( isSiteEligibleForJetpackCloud( {}, 0 ) ).toEqual( true );
	} );
} );