File size: 2,769 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
import { translate } from 'i18n-calypso';
import {
	EMAIL_VERIFY_REQUEST_SUCCESS,
	EMAIL_VERIFY_REQUEST_FAILURE,
} from 'calypso/state/action-types';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import {
	infoNotice,
	errorNotice,
	successNotice,
	removeNotice,
} from 'calypso/state/notices/actions';
import { requestEmailVerification, handleSuccess, handleError } from '../';

describe( 'send-email-verification', () => {
	describe( '#requestEmailVerification', () => {
		test( 'should dispatch HTTP request to me/send-verification-email endpoint', () => {
			expect( requestEmailVerification( { type: 'DUMMY' } ) ).toContainEqual(
				expect.objectContaining(
					http(
						{
							apiVersion: '1.1',
							method: 'POST',
							path: '/me/send-verification-email',
						},
						{ type: 'DUMMY' }
					)
				)
			);
		} );

		test( 'should dispatch info notice when showing global notices', () => {
			expect(
				requestEmailVerification( { type: 'DUMMY', showGlobalNotices: true } )
			).toContainEqual(
				infoNotice( translate( 'Sending email…' ), {
					id: 'email-verification-info-notice',
					duration: 4000,
				} )
			);
		} );
	} );

	describe( '#handleError', () => {
		const message = 'This is an error message.';
		const rawError = Error( message );

		test( 'should dispatch failure action with error message', () => {
			expect( handleError( null, rawError ) ).toContainEqual( {
				type: EMAIL_VERIFY_REQUEST_FAILURE,
				message,
			} );
		} );

		test( 'should dispatch error notice when showing global notices', () => {
			expect( handleError( { showGlobalNotices: true }, rawError ) ).toContainEqual(
				errorNotice( translate( 'Sorry, we couldn’t send the email.' ), {
					id: 'email-verification-error-notice',
					duration: 4000,
				} )
			);
		} );

		test( 'should remove info notice when showing global notices', () => {
			expect( handleError( { showGlobalNotices: true }, rawError ) ).toContainEqual(
				removeNotice( 'email-verification-info-notice' )
			);
		} );
	} );

	describe( '#handleSuccess', () => {
		test( 'should dispatch failure action with error message', () => {
			expect( handleSuccess() ).toContainEqual( {
				type: EMAIL_VERIFY_REQUEST_SUCCESS,
			} );
		} );

		test( 'should dispatch success notice when showing global notices', () => {
			expect( handleSuccess( { showGlobalNotices: true } ) ).toContainEqual(
				successNotice( translate( 'Email sent' ), {
					id: 'email-verification-success-notice',
					duration: 4000,
				} )
			);
		} );

		test( 'should remove info notice when showing global notices', () => {
			expect( handleSuccess( { showGlobalNotices: true } ) ).toContainEqual(
				removeNotice( 'email-verification-info-notice' )
			);
		} );
	} );
} );