File size: 7,100 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
import { JETPACK_SETTINGS_SAVE, JETPACK_SETTINGS_UPDATE } from 'calypso/state/action-types';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import {
	saveJetpackSettingsSuccess,
	updateJetpackSettings,
} from 'calypso/state/jetpack/settings/actions';
import { normalizeSettings } from 'calypso/state/jetpack/settings/utils';
import {
	MAX_WOOCOMMERCE_INSTALL_RETRIES,
	requestJetpackSettings,
	saveJetpackSettings,
	handleSaveSuccess,
	announceRequestFailure,
	handleSaveFailure,
	retryOrAnnounceSaveFailure,
	fromApi,
} from '../';

describe( 'requestJetpackSettings()', () => {
	const siteId = 12345678;

	const action = {
		type: JETPACK_SETTINGS_UPDATE,
		siteId,
	};

	test( 'should dispatch an action for a GET HTTP request to fetch Jetpack settings', () => {
		const result = requestJetpackSettings( action );

		expect( result ).toEqual(
			http(
				{
					apiVersion: '1.1',
					method: 'GET',
					path: '/jetpack-blogs/' + siteId + '/rest-api/',
					query: {
						path: '/jetpack/v4/settings/',
						json: true,
					},
				},
				action
			)
		);
	} );
} );

describe( 'announceRequestFailure()', () => {
	const dispatch = jest.fn();
	const siteId = 12345678;
	const siteUrl = 'http://yourgroovydomain.com';

	test( 'should trigger an error notice with an action button when request fails for a connected site', () => {
		const getState = () => ( {
			jetpack: {
				onboarding: {
					credentials: {},
				},
			},
			sites: {
				items: {
					[ siteId ]: {
						ID: siteId,
						URL: siteUrl,
					},
				},
			},
		} );

		announceRequestFailure( { siteId } )( dispatch, getState );

		expect( dispatch ).toHaveBeenCalledWith(
			expect.objectContaining( {
				notice: expect.objectContaining( {
					button: 'Visit site admin',
					href: siteUrl + '/wp-admin/admin.php?page=jetpack',
					noticeId: `jps-communication-error-${ siteId }`,
					status: 'is-error',
					text: 'Something went wrong.',
				} ),
			} )
		);
	} );

	test( 'should trigger an error notice without action button if url is missing', () => {
		const getState = () => ( {
			jetpack: {
				onboarding: {
					credentials: {
						[ siteId ]: {
							token: 'abcd1234',
							userEmail: 'example@yourgroovydomain.com',
						},
					},
				},
			},
			sites: {
				items: {},
			},
		} );

		announceRequestFailure( { siteId } )( dispatch, getState );

		expect( dispatch ).toHaveBeenCalledWith(
			expect.objectContaining( {
				notice: expect.objectContaining( {
					noticeId: `jps-communication-error-${ siteId }`,
					status: 'is-error',
					text: 'Something went wrong.',
				} ),
			} )
		);
	} );
} );

describe( 'saveJetpackSettings()', () => {
	const dispatch = jest.fn();
	const token = 'abcd1234';
	const userEmail = 'example@yourgroovydomain.com';
	const siteId = 12345678;
	const onboardingSettings = {
		siteTitle: 'My Awesome Site',
		siteDescription: 'Not just another WordPress Site',
	};
	const settings = {
		onboarding: {
			...onboardingSettings,
			token,
			jpUser: userEmail,
		},
	};

	const previousSettings = {
		onboarding: {
			siteTitle: '',
			siteDescription: 'Just another WordPress site',
		},
	};

	const getState = () => ( {
		jetpack: { settings: { [ 12345678 ]: previousSettings } },
	} );

	const action = {
		type: JETPACK_SETTINGS_SAVE,
		siteId,
		settings,
	};

	test( 'should dispatch an action for POST HTTP request to save Jetpack settings, omitting legacy Jetpack Onboarding credentials', () => {
		saveJetpackSettings( action )( dispatch, getState );

		expect( dispatch ).toHaveBeenCalledWith(
			http(
				{
					apiVersion: '1.1',
					method: 'POST',
					path: '/jetpack-blogs/' + siteId + '/rest-api/',
					body: {
						path: '/jetpack/v4/settings/',
						body: JSON.stringify( settings ),
						json: true,
					},
				},
				{ ...action, meta: { ...action.meta, settings: previousSettings } }
			)
		);
		expect( dispatch ).toHaveBeenCalledWith(
			updateJetpackSettings( siteId, { onboarding: onboardingSettings } )
		);
	} );
} );

describe( 'handleSaveSuccess()', () => {
	const siteId = 12345678;
	const settings = {
		siteTitle: 'My Awesome Site',
		siteDescription: 'Not just another WordPress Site',
	};

	test( 'should dispatch a save success action upon successful save request', () => {
		const result = handleSaveSuccess( { siteId }, { data: settings } );

		expect( result ).toEqual(
			expect.objectContaining( saveJetpackSettingsSuccess( siteId, settings ) )
		);
	} );
} );

describe( 'handleSaveFailure()', () => {
	const siteId = 12345678;
	const action = {
		type: JETPACK_SETTINGS_SAVE,
		siteId,
		settings: {
			siteTitle: 'My Awesome Site',
			siteDescription: 'Not just another WordPress Site',
		},
		meta: {
			settings: {
				siteTitle: '',
				siteDescription: 'Just another WordPress site',
			},
		},
	};

	test( 'should trigger an error notice upon unsuccessful save request', () => {
		const result = handleSaveFailure( { siteId }, action );

		expect( result[ 1 ] ).toMatchObject( {
			notice: {
				status: 'is-error',
				text: 'An unexpected error occurred. Please try again later.',
				noticeId: `jps-notice-error-${ siteId }`,
				duration: 5000,
			},
		} );
	} );
} );

describe( 'retryOrAnnounceSaveFailure()', () => {
	const siteId = 12345678;
	const settings = {
		onboarding: {
			installWooCommerce: true,
		},
	};
	const action = {
		type: JETPACK_SETTINGS_SAVE,
		siteId,
		settings,
		meta: {
			dataLayer: {
				trackRequest: true,
			},
		},
	};
	const error = {
		error: 'http_request_failed',
		message: 'cURL error 28: Operation timed out after 5001 milliseconds with 0 bytes received',
	};

	test( 'should trigger saveJetpackSettings upon first WooCommerce install timeout', () => {
		const result = retryOrAnnounceSaveFailure( action, error );

		expect( result ).toEqual(
			expect.objectContaining( {
				settings,
				siteId,
				type: JETPACK_SETTINGS_SAVE,
				meta: {
					dataLayer: {
						retryCount: 1,
						trackRequest: true,
					},
				},
			} )
		);
	} );

	test( 'should trigger handleSaveFailure upon max number of WooCommerce install timeout', () => {
		const thirdAttemptAction = {
			...action,
			meta: {
				...action.meta,
				dataLayer: {
					...action.meta.dataLayer,
					retryCount: MAX_WOOCOMMERCE_INSTALL_RETRIES + 1,
				},
			},
		};

		const result = retryOrAnnounceSaveFailure( thirdAttemptAction, error );

		expect( result[ 1 ] ).toMatchObject( {
			notice: {
				status: 'is-error',
				text: 'An unexpected error occurred. Please try again later.',
				noticeId: `jps-notice-error-${ siteId }`,
				duration: 5000,
			},
		} );
	} );
} );

describe( 'fromApi', () => {
	test( 'should throw an error if no data field is set', () => {
		const response = { noData: { onboarding: {} } };
		expect( () => fromApi( response ) ).toThrow( 'missing settings' );
	} );

	test( 'should return normalized data if present', () => {
		const settings = {
			jetpack_portfolio: true,
			onboarding: { siteTitle: 'Yet Another Site Title' },
		};
		expect( fromApi( { data: settings } ) ).toEqual( normalizeSettings( settings ) );
	} );
} );