File size: 2,565 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
import {
	EMAIL_ACCOUNTS_REQUEST,
	EMAIL_ACCOUNTS_REQUEST_SUCCESS,
	EMAIL_ACCOUNTS_REQUEST_FAILURE,
} from 'calypso/state/action-types';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { getEmailAccounts, getEmailAccountsFailure, getEmailAccountsSuccess } from '../';

const isErrorNotice = ( action ) => {
	return action && action.notice && 'is-error' === action.notice.status;
};

describe( 'wpcom-api', () => {
	describe( 'email accounts request', () => {
		const siteId = 1;
		const action = {
			type: EMAIL_ACCOUNTS_REQUEST,
			siteId,
		};

		describe( '#getEmailAccounts', () => {
			test( 'should dispatch an HTTP request to the get email-accounts endpoint', () => {
				expect( getEmailAccounts( action ) ).toEqual(
					http(
						{
							method: 'GET',
							path: '/sites/1/email-accounts',
						},
						action
					)
				);
			} );
		} );

		describe( '#getEmailAccountsFailure', () => {
			const message = 'An error has occurred';

			test( 'should dispatch a get email accounts failure action on error', () => {
				const resultActions = getEmailAccountsFailure( action, { message } );
				expect( resultActions ).toHaveLength( 2 );
				expect( isErrorNotice( resultActions[ 0 ] ) ).toBe( true );
				expect( resultActions[ 1 ] ).toEqual( {
					type: EMAIL_ACCOUNTS_REQUEST_FAILURE,
					siteId,
					error: { message },
				} );
			} );
		} );

		describe( '#getEmailAccountsSuccess', () => {
			test( 'should dispatch a get email accounts success action and response', () => {
				const response = {
					accounts: [
						{
							domain_name: 'test.blog',
							product_name: 'G Suite Basic',
							product_slug: 'gapps',
							product_type: 'gapps',
							site_id: 1,
							mailboxes: [
								{
									name: 'user',
									first_name: 'User',
									last_name: 'One',
									state: 'suspended',
								},
							],
						},
					],
				};
				expect( getEmailAccountsSuccess( action, response ) ).toEqual( {
					type: EMAIL_ACCOUNTS_REQUEST_SUCCESS,
					siteId,
					response,
				} );
			} );

			test( 'should dispatch a get email accounts failure action on no response', () => {
				const resultActions = getEmailAccountsSuccess( action, undefined );
				expect( resultActions ).toHaveLength( 2 );
				expect( isErrorNotice( resultActions[ 0 ] ) ).toBe( true );
				expect( resultActions[ 1 ] ).toEqual( {
					type: EMAIL_ACCOUNTS_REQUEST_FAILURE,
					siteId,
					error: { message: 'Failed to retrieve your email accounts. No response was received' },
				} );
			} );
		} );
	} );
} );