File size: 3,135 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { processInboundChain, processOutboundChain } from '../';

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

const getSites = {
	method: 'GET',
	path: '/',
	apiVersion: 'v1',
	onSuccess: succeeder,
	onFailure: failer,
};

describe( '#processInboundChain', () => {
	const aborter = ( inboundData ) => ( {
		...inboundData,
		failures: [],
		shouldAbort: true,
		successes: [],
	} );

	const responderDoubler = ( inboundData ) => ( {
		...inboundData,
		failures: [ ...inboundData.failures, ...inboundData.failures ],
		successes: [ ...inboundData.successes, ...inboundData.successes ],
	} );

	test( 'should pass through data given an empty chain', () => {
		expect(
			processInboundChain( [] )(
				getSites,
				{},
				{ value: 1 },
				{ error: 'bad' },
				{ header: 'foobar' }
			)
		).toEqual( {
			failures: [ getSites.onFailure ],
			nextData: { value: 1 },
			nextError: { error: 'bad' },
			nextHeaders: { header: 'foobar' },
			successes: [ getSites.onSuccess ],
		} );
	} );

	test( 'should sequence a single processor', () => {
		expect( processInboundChain( [ responderDoubler ] )( getSites, {}, {}, {}, {} ) ).toEqual( {
			failures: [ getSites.onFailure, getSites.onFailure ],
			nextData: {},
			nextError: {},
			nextHeaders: {},
			successes: [ getSites.onSuccess, getSites.onSuccess ],
		} );
	} );

	test( 'should sequence multiple processors', () => {
		expect(
			processInboundChain( [ responderDoubler, responderDoubler ] )( getSites, {}, {}, {}, {} )
		).toEqual( {
			failures: new Array( 4 ).fill( getSites.onFailure ),
			nextData: {},
			nextError: {},
			nextHeaders: {},
			successes: new Array( 4 ).fill( getSites.onSuccess ),
		} );
	} );

	test( 'should abort the chain as soon as `shouldAbort` is set', () => {
		expect(
			processInboundChain( [ aborter, responderDoubler ] )( getSites, {}, {}, {}, {} )
		).toEqual( {
			failures: [],
			nextData: {},
			nextError: {},
			nextHeaders: {},
			successes: [],
			shouldAbort: true,
		} );
	} );
} );

describe( '#processOutboundChain', () => {
	const aborter = ( outboundData ) => ( {
		...outboundData,
		nextRequest: null,
	} );

	const pathDoubler = ( outboundData ) => {
		const { nextRequest } = outboundData;
		const { path } = nextRequest;

		return {
			...outboundData,
			nextRequest: {
				...nextRequest,
				path: path + path,
			},
		};
	};

	test( 'should pass requests given an empty chain', () => {
		expect( processOutboundChain( [] )( getSites, {} ) ).toEqual( getSites );
	} );

	test( 'should sequence a single processor', () => {
		expect( processOutboundChain( [ pathDoubler ] )( getSites, {} ) ).toEqual( {
			...getSites,
			path: getSites.path + getSites.path,
		} );
	} );

	test( 'should sequence multiple processors', () => {
		expect( processOutboundChain( [ pathDoubler, pathDoubler ] )( getSites, {} ) ).toEqual( {
			...getSites,
			path: new Array( 4 ).fill( getSites.path ).join( '' ),
		} );
	} );

	test( 'should abort the chain as soon as the `nextRequest` is `null`', () => {
		expect( processOutboundChain( [ aborter, pathDoubler ] )( getSites, {} ) ).toBeNull();
	} );
} );