File size: 4,284 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import freeze from 'deep-freeze';
import { NOTICE_CREATE } from 'calypso/state/action-types';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import {
	READER_FEED_REQUEST_SUCCESS,
	READER_FEED_REQUEST_FAILURE,
} from 'calypso/state/reader/action-types';
import { requestFeedSearch, receiveFeedSearch } from 'calypso/state/reader/feed-searches/actions';
import queryKey from 'calypso/state/reader/feed-searches/query-key';
import { requestFeed } from 'calypso/state/reader/feeds/actions';
import {
	requestReadFeedSearch,
	receiveReadFeedSearchSuccess,
	receiveReadFeedSearchError,
	fromApi,
	requestReadFeed,
	receiveReadFeedSuccess,
	receiveReadFeedError,
} from '../';

const feeds = freeze( [ { blog_ID: 123, subscribe_URL: 'http://example.com' } ] );

const query = 'okapis and giraffes';

describe( 'wpcom-api', () => {
	describe( 'search feeds', () => {
		describe( '#requestReadFeedSearch', () => {
			test( 'should dispatch http request for feed search with followed feeds excluded by default', () => {
				const action = requestFeedSearch( { query } );

				expect( requestReadFeedSearch( action ) ).toMatchObject(
					http(
						{
							method: 'GET',
							path: '/read/feed',
							apiVersion: '1.1',
							query: { q: query, offset: 0, exclude_followed: true, sort: 'relevance' },
						},
						action
					)
				);
			} );

			test( 'should dispatch http request for feed search with followed feeds included if specified', () => {
				const action = requestFeedSearch( { query, excludeFollowed: false } );

				expect( requestReadFeedSearch( action ) ).toMatchObject(
					http(
						{
							method: 'GET',
							path: '/read/feed',
							apiVersion: '1.1',
							query: { q: query, offset: 0, exclude_followed: false, sort: 'relevance' },
						},
						action
					)
				);
			} );

			test( 'should dispatch http request for feed search with the offset specified', () => {
				const action = requestFeedSearch( { query, offset: 10 } );

				expect( requestReadFeedSearch( action ) ).toMatchObject(
					http(
						{
							method: 'GET',
							path: '/read/feed',
							apiVersion: '1.1',
							query: { q: query, offset: 10, exclude_followed: true, sort: 'relevance' },
						},
						action
					)
				);
			} );
		} );

		describe( '#receiveReadFeedSearchSuccess', () => {
			test( 'should dispatch an action with the feed results', () => {
				const action = requestFeedSearch( { query } );
				const apiResponse = fromApi( { feeds, total: 500 } );

				expect( receiveReadFeedSearchSuccess( action, apiResponse ) ).toMatchObject(
					receiveFeedSearch(
						queryKey( action.payload ),
						[
							{
								blog_ID: 123,
								feed_URL: 'http://example.com',
								subscribe_URL: 'http://example.com',
							},
						],
						200
					)
				);
			} );
		} );

		describe( '#receiveReadFeedSearchError', () => {
			test( 'should dispatch error notice', () => {
				const action = requestFeedSearch( { query } );

				expect( receiveReadFeedSearchError( action ) ).toMatchObject( {
					type: NOTICE_CREATE,
				} );
			} );
		} );
	} );

	describe( 'request a single feed', () => {
		const feedId = 123;

		describe( '#requestReadFeed', () => {
			test( 'should dispatch a http request', () => {
				const action = requestFeed( feedId );

				expect( requestReadFeed( action ) ).toMatchObject(
					http(
						{
							apiVersion: '1.1',
							method: 'GET',
							path: `/read/feed/${ feedId }`,
						},
						action
					)
				);
			} );
		} );

		describe( '#receiveReadFeedSuccess', () => {
			test( 'should dispatch an action with the feed results', () => {
				const action = requestFeed( feedId );
				const apiResponse = { feed_ID: 123 };

				expect( receiveReadFeedSuccess( action, apiResponse ) ).toMatchObject( {
					type: READER_FEED_REQUEST_SUCCESS,
					payload: { feed_ID: 123 },
				} );
			} );
		} );

		describe( '#receiveReadFeedError', () => {
			test( 'should dispatch an error action', () => {
				const action = requestFeed( feedId );
				const apiResponse = { error: '417' };

				expect( receiveReadFeedError( action, apiResponse ) ).toMatchObject( {
					type: READER_FEED_REQUEST_FAILURE,
					payload: { feed_ID: 123 },
					error: apiResponse,
				} );
			} );
		} );
	} );
} );