File size: 2,555 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
import { JETPACK_REMOTE_INSTALL, JETPACK_REMOTE_INSTALL_FAILURE } from 'calypso/state/action-types';
import {
	jetpackRemoteInstallComplete,
	jetpackRemoteInstallUpdateError,
} from 'calypso/state/jetpack-remote-install/actions';
import {
	handleError,
	handleSuccess,
	installJetpackPlugin,
	JETPACK_REMOTE_INSTALL_RETRIES,
} from '../';

const url = 'https://yourgroovydomain.com';
const user = 'blah123';
const password = 'hGhrskf145kst';

const INSTALL_ACTION = {
	type: JETPACK_REMOTE_INSTALL,
	url,
	user,
	password,
	meta: {
		dataLayer: {
			trackRequest: true,
		},
	},
};

const SUCCESS_RESPONSE = { status: true };

const FAILURE_RESPONSE = {
	error: 'COULD_NOT_LOGIN',
	message: 'extra info',
};

const TIMEOUT_RESPONSE = {
	error: 'http_request_failed',
	message: 'cURL error 28: Operation timed out after 10000 milliseconds with 0 bytes received',
};

describe( 'installJetpackPlugin', () => {
	test( 'should return an http request', () => {
		const result = installJetpackPlugin( { url, user, password } );
		expect( result ).toMatchSnapshot();
	} );
} );

describe( 'handleSuccess', () => {
	test( 'should return jetpackRemoteInstallComplete', () => {
		const result = handleSuccess( { url }, SUCCESS_RESPONSE );
		expect( result ).toEqual( expect.objectContaining( jetpackRemoteInstallComplete( url ) ) );
	} );
} );

describe( 'handleError', () => {
	test( 'should return JetpackRemoteInstallUpdateError if not timeout error', () => {
		const result = handleError( INSTALL_ACTION, FAILURE_RESPONSE );
		expect( result ).toEqual(
			expect.objectContaining(
				jetpackRemoteInstallUpdateError( url, 'COULD_NOT_LOGIN', 'extra info' )
			)
		);
	} );

	test( 'should retry on timeout error', () => {
		const result = handleError( INSTALL_ACTION, TIMEOUT_RESPONSE );
		expect( result ).toEqual(
			expect.objectContaining( {
				type: JETPACK_REMOTE_INSTALL,
				url,
				user,
				password,
				meta: {
					dataLayer: {
						retryCount: 1,
						trackRequest: true,
					},
				},
			} )
		);
	} );

	test( 'should trigger an error if max num retries reached', () => {
		const installAction = {
			...INSTALL_ACTION,
			meta: { dataLayer: { retryCount: JETPACK_REMOTE_INSTALL_RETRIES } },
		};
		const result = handleError( installAction, TIMEOUT_RESPONSE );
		expect( result ).toEqual(
			expect.objectContaining( {
				type: JETPACK_REMOTE_INSTALL_FAILURE,
				url,
				errorCode: 'http_request_failed',
				errorMessage:
					'cURL error 28: Operation timed out after 10000 milliseconds with 0 bytes received',
			} )
		);
	} );
} );