File size: 2,091 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
import {
	GRAVATAR_UPLOAD_RECEIVE,
	GRAVATAR_UPLOAD_REQUEST_SUCCESS,
	GRAVATAR_UPLOAD_REQUEST_FAILURE,
} from 'calypso/state/action-types';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { uploadGravatar, announceSuccess, announceFailure } from '../';

describe( '#uploadGravatar()', () => {
	test( 'dispatches an HTTP request to the gravatar upload endpoint', () => {
		const action = {
			type: 'DUMMY_ACTION',
			file: 'file',
			email: 'email',
		};

		const result = uploadGravatar( action );

		expect( result ).toEqual(
			http(
				{
					apiNamespace: 'wpcom/v2',
					method: 'POST',
					body: {},
					path: '/gravatar-upload',
					formData: [
						[ 'account', 'email' ],
						[ 'filedata', 'file' ],
					],
				},
				action
			)
		);
	} );
} );

describe( '#announceSuccess()', () => {
	const noop = () => {};
	const tempImageSrc = 'tempImageSrc';

	beforeAll( () => {
		global.FileReader = jest.fn( () => ( {
			readAsDataURL: noop,
			addEventListener: function ( event, callback ) {
				this.result = tempImageSrc;
				callback();
			},
		} ) );
	} );

	test( 'dispatches a success action when the file is read', () => {
		const action = {
			type: 'DUMMY_ACTION',
			file: 'file',
			email: 'email',
		};
		const dispatch = jest.fn();

		announceSuccess( action, noop, { success: true } )( dispatch );
		expect( dispatch ).toHaveBeenCalledWith(
			expect.objectContaining( { type: GRAVATAR_UPLOAD_REQUEST_SUCCESS } )
		);
	} );

	test( 'dispatches a upload received action with the image data when the file is read', () => {
		const action = {
			type: 'DUMMY_ACTION',
			file: 'file',
			email: 'email',
		};
		const dispatch = jest.fn();

		announceSuccess( action, noop, { success: true } )( dispatch );
		expect( dispatch ).toHaveBeenCalledWith( {
			type: GRAVATAR_UPLOAD_RECEIVE,
			src: 'tempImageSrc',
		} );
	} );
} );

describe( '#announceFailure()', () => {
	test( 'should dispatch an error notice', () => {
		const result = announceFailure();

		expect( result[ 0 ].type ).toEqual( GRAVATAR_UPLOAD_REQUEST_FAILURE );
	} );
} );