File size: 8,681 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
/**
 * @jest-environment jsdom
 */
jest.mock( 'calypso/components/data/document-head', () => 'document-head' );
jest.mock( 'calypso/lib/analytics/page-view-tracker', () => 'page-view-tracker' );
jest.mock( 'calypso/components/feature-example', () => ( { children } ) => {
	return <div data-testid="feature-example-wrapper">{ children }</div>;
} );
jest.mock( 'calypso/lib/wp', () => ( {
	req: {
		get: jest.fn(),
	},
} ) );

import {
	FEATURE_SFTP,
	FEATURE_SITE_STAGING_SITES,
	PLAN_BUSINESS_MONTHLY,
	PLAN_ECOMMERCE_TRIAL_MONTHLY,
	PLAN_FREE,
	PLAN_PERSONAL,
	WPCOM_FEATURES_ATOMIC,
} from '@automattic/calypso-products';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { render, screen, waitFor } from '@testing-library/react';
import { Provider } from 'react-redux';
import configureStore from 'redux-mock-store';
import { thunk } from 'redux-thunk';
import wp from 'calypso/lib/wp';
import { transferStates } from 'calypso/state/automated-transfer/constants';
import Hosting from '../main';

const wpcomGetStub = wp.req.get;

const getTestConfig = ( {
	isAtomicSite = false,
	isWpcomStagingSite = false,
	planSlug = PLAN_FREE,
	siteFeatures = [],
	transferStatus = transferStates.NONE,
} ) => {
	wpcomGetStub.mockResolvedValue( {
		status: transferStatus,
	} );

	return {
		isAtomicSite,
		isWpcomStagingSite,
		planSlug,
		siteFeatures,
		transferStatus,
	};
};

const createTestStore = ( {
	isAtomicSite,
	isWpcomStagingSite,
	planSlug,
	siteFeatures,
	transferStatus,
} ) => {
	const TEST_SITE_ID = 1;
	const middlewares = [ thunk ];

	const mockStore = configureStore( middlewares );
	const store = mockStore( {
		atomicHosting: {
			[ TEST_SITE_ID ]: {
				isLoadingSftpUsers: false,
				isLoadingSshAccess: false,
			},
		},
		automatedTransfer: {
			[ TEST_SITE_ID ]: {
				status: transferStatus,
			},
		},
		documentHead: { unreadCount: 0 },
		ui: { selectedSiteId: TEST_SITE_ID },
		currentUser: {
			capabilities: {
				[ TEST_SITE_ID ]: {
					manage_options: true,
				},
			},
			id: 1,
		},
		sites: {
			features: {
				[ TEST_SITE_ID ]: {
					data: {
						active: siteFeatures,
					},
				},
			},
			items: {
				[ TEST_SITE_ID ]: {
					is_wpcom_staging_site: isWpcomStagingSite,
					jetpack: false,
					options: {
						is_automated_transfer: isAtomicSite,
						is_wpcom_atomic: isAtomicSite,
					},
					URL: 'test-site-example.wordpress.com',
				},
			},
			plans: {
				[ TEST_SITE_ID ]: {
					data: [
						{
							currentPlan: true,
							productSlug: planSlug,
						},
					],
				},
			},
			requesting: {
				[ TEST_SITE_ID ]: true,
			},
		},
		preferences: {},
	} );
	return store;
};

const renderComponentWithStoreAndQueryClient = ( store ) => {
	render(
		<Provider store={ store }>
			<QueryClientProvider client={ new QueryClient() }>
				<Hosting />
			</QueryClientProvider>
		</Provider>
	);
};

const stringsForBasicFeatureCards = [ 'Restore plugins and themes', 'Clear all caches' ];

describe( 'Hosting Configuration', () => {
	beforeAll( () => {
		// Mock the missing `window.matchMedia` function that's not even in JSDOM
		Object.defineProperty( window, 'matchMedia', {
			writable: true,
			value: jest.fn().mockImplementation( ( query ) => ( {
				matches: false,
				media: query,
				onchange: null,
				addListener: jest.fn(), // deprecated
				removeListener: jest.fn(), // deprecated
				addEventListener: jest.fn(),
				removeEventListener: jest.fn(),
				dispatchEvent: jest.fn(),
			} ) ),
		} );

		// Mock `ResizeObserver`, which isn't currently supported in JSDOM
		Object.defineProperty( global, 'ResizeObserver', {
			writable: false,
			value: jest.fn().mockImplementation( () => ( {
				observe: jest.fn(),
				unobserve: jest.fn(),
				disconnect: jest.fn(),
			} ) ),
		} );
	} );

	afterAll( () => {
		jest.resetAllMocks();
	} );

	describe( 'Site on free plan', () => {
		it( 'should show upsell banner and all cards should be within FeatureExample', async () => {
			const testConfig = getTestConfig( { planSlug: PLAN_FREE } );

			renderComponentWithStoreAndQueryClient( createTestStore( testConfig ) );
			await waitFor( () => expect( wpcomGetStub ).toHaveBeenCalled() );

			expect(
				await screen.findByText( 'Upgrade to the Business plan to access all hosting features:' )
			).toBeVisible();
			expect( screen.getByText( 'Upgrade to Business Plan' ) ).toBeVisible();

			const [ mainFeatureExampleElement ] = screen.getAllByTestId( 'feature-example-wrapper' );

			expect( mainFeatureExampleElement ).toBeVisible();
		} );
	} );

	describe( 'Site on Personal plan', () => {
		it( 'should show upsell banner and all cards should be within FeatureExample', async () => {
			const testConfig = getTestConfig( { planSlug: PLAN_PERSONAL } );

			renderComponentWithStoreAndQueryClient( createTestStore( testConfig ) );
			await waitFor( () => expect( wpcomGetStub ).toHaveBeenCalled() );

			expect(
				await screen.findByText( 'Upgrade to the Business plan to access all hosting features:' )
			).toBeVisible();

			expect( screen.getByText( 'Upgrade to Business Plan' ) ).toBeVisible();

			const [ mainFeatureExampleElement ] = screen.getAllByTestId( 'feature-example-wrapper' );

			expect( mainFeatureExampleElement ).toBeVisible();
		} );
	} );

	describe( 'Site on Business plan', () => {
		it( 'should show activation notice when the site is not Atomic', async () => {
			const testConfig = getTestConfig( {
				planSlug: PLAN_BUSINESS_MONTHLY,
				siteFeatures: [ WPCOM_FEATURES_ATOMIC, FEATURE_SFTP, FEATURE_SITE_STAGING_SITES ],
				transferStatus: 'none',
			} );

			renderComponentWithStoreAndQueryClient( createTestStore( testConfig ) );
			await waitFor( () => expect( wpcomGetStub ).toHaveBeenCalled() );

			expect(
				screen.getByText( 'Please activate the hosting access to begin using these features.' )
			).toBeVisible();
			expect( screen.getByText( 'Activate' ) ).toBeVisible();

			const [ mainFeatureExampleElement ] = screen.getAllByTestId( 'feature-example-wrapper' );

			expect( mainFeatureExampleElement ).toBeVisible();
		} );

		it( 'should not show the activation notice when the site is Atomic', async () => {
			const testConfig = getTestConfig( {
				isAtomicSite: true,
				planSlug: PLAN_BUSINESS_MONTHLY,
				siteFeatures: [ WPCOM_FEATURES_ATOMIC, FEATURE_SFTP, FEATURE_SITE_STAGING_SITES ],
				transferStatus: transferStates.COMPLETE,
			} );

			renderComponentWithStoreAndQueryClient( createTestStore( testConfig ) );
			await waitFor( () => expect( wpcomGetStub ).toHaveBeenCalled() );

			expect(
				screen.queryByText( 'Please activate the hosting access to begin using these features.' )
			).toBeNull();
			expect( screen.queryByText( 'Activate' ) ).toBeNull();
			expect( screen.queryByTestId( 'feature-example-wrapper' ) ).toBeNull();
		} );

		it( 'should show the transferring notice when the site is transferring to Atomic', async () => {
			const testConfig = getTestConfig( {
				isAtomicSite: false,
				planSlug: PLAN_BUSINESS_MONTHLY,
				siteFeatures: [ WPCOM_FEATURES_ATOMIC, FEATURE_SFTP, FEATURE_SITE_STAGING_SITES ],
				transferStatus: transferStates.START,
			} );

			renderComponentWithStoreAndQueryClient( createTestStore( testConfig ) );
			await waitFor( () => expect( wpcomGetStub ).toHaveBeenCalled() );

			expect(
				screen.getByText( 'Please wait while we activate the hosting features.' )
			).toBeVisible();

			const [ mainFeatureExampleElement ] = screen.getAllByTestId( 'feature-example-wrapper' );

			expect( mainFeatureExampleElement ).toBeVisible();
		} );
	} );

	describe( 'Site on Woo Express/eCommerce Trial plan', () => {
		it( 'should show the upsell banner with alternate text and show the basic features', async () => {
			const testConfig = getTestConfig( {
				isAtomicSite: true,
				planSlug: PLAN_ECOMMERCE_TRIAL_MONTHLY,
				siteFeatures: [ WPCOM_FEATURES_ATOMIC ],
				transferStatus: transferStates.COMPLETE,
			} );

			renderComponentWithStoreAndQueryClient( createTestStore( testConfig ) );
			await waitFor( () => expect( wpcomGetStub ).toHaveBeenCalled() );

			expect(
				screen.getByText( 'Upgrade your plan to access all hosting features' )
			).toBeVisible();
			expect( screen.getByText( 'Upgrade your plan' ) ).toBeVisible();

			const [ mainFeatureExampleElement ] = screen.getAllByTestId( 'feature-example-wrapper' );

			expect( mainFeatureExampleElement ).toBeVisible();

			stringsForBasicFeatureCards.forEach( ( string ) => {
				const elementForString = screen.getByText( string );
				expect( elementForString ).toBeVisible();
				expect( mainFeatureExampleElement ).not.toContainElement( elementForString );
			} );
		} );
	} );
} );