File size: 1,546 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
import { extendAction } from '@automattic/state-utils';
import useNock, { nock } from 'calypso/test-helpers/use-nock';
import { failureMeta, queueRequest, successMeta } from '../';

const processInbound = ( action ) => action;
const processOutbound = ( action, store, data, error ) => ( {
	failures: [ action.onFailure ],
	nextData: data,
	nextError: error,
	successes: [ action.onSuccess ],
} );

const http = queueRequest( processInbound, processOutbound );

const succeeder = { type: 'SUCCESS' };
const failer = { type: 'FAIL' };

const getMe = {
	method: 'GET',
	path: '/me',
	apiVersion: 'v1.1',
	onFailure: failer,
	onSuccess: succeeder,
};

describe( '#queueRequest', () => {
	useNock();

	test( 'should call `onSuccess` when a response returns with data', () => {
		return new Promise( ( done ) => {
			const data = { value: 1 };
			nock( 'https://public-api.wordpress.com:443' ).get( '/rest/v1.1/me' ).reply( 200, data );

			const dispatch = ( action ) => {
				expect( action ).toEqual( extendAction( succeeder, successMeta( data ) ) );
				done();
			};

			http( { dispatch }, getMe );
		} );
	} );

	test( 'should call `onFailure` when a response returns with an error', () => {
		return new Promise( ( done ) => {
			const error = { error: 'bad' };
			nock( 'https://public-api.wordpress.com:443' ).get( '/rest/v1.1/me' ).replyWithError( error );

			const dispatch = ( action ) => {
				expect( action ).toEqual( extendAction( failer, failureMeta( error ) ) );
				done();
			};

			http( { dispatch }, getMe );
		} );
	} );
} );