forked from WP-API/node-wpapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomments.js
More file actions
314 lines (284 loc) · 8.68 KB
/
comments.js
File metadata and controls
314 lines (284 loc) · 8.68 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
'use strict';
const WPAPI = require( '../../' );
const WPRequest = require( '../../lib/constructors/wp-request.js' );
// Variable to use as our 'success token' in promise assertions
const SUCCESS = 'success';
// Define some arrays to use ensuring the returned data is what we expect
// it to be (e.g. an array of the titles from posts on the first page)
const expectedResults = {
postsAndAuthors: {
page1: [
'1170John Doe',
'1148Jane Doe',
'1148John Doe',
'1148John Doe',
'1148Jane Doe',
'1148John Doe',
'1148Joe Bloggs',
'1148Jane Bloggs',
'1148Joe Bloggs',
],
page2: [
'1148Jane Bloggs',
'1148Joe Bloggs',
'1148Fred Bloggs',
'1148Fred Bloggs',
'1148Jane Bloggs',
'1148John Doe',
'1148John Doe',
'1148John Doe',
'1148Jane Doe',
'1148Anonymous User',
],
page3: [
'1148John Doe',
'1149John Doe',
'155John Doe',
'155Anon',
'155tellyworthtest2',
],
},
postsAndAuthorsAsc: {
page1: [
'155tellyworthtest2',
'155Anon',
'155John Doe',
'1149John Doe',
'1148John Doe',
'1148Anonymous User',
'1148Jane Doe',
'1148John Doe',
'1148John Doe',
'1148John Doe',
],
},
};
// Inspecting the posts and authors of the returned comments arrays is an easy
// way to validate that the right page of results was returned
const getPostsAndAuthors = comments => comments
.map( comment => comment.post + comment.author_name );
describe( 'integration: comments()', () => {
let wp;
beforeEach( () => {
wp = new WPAPI( {
endpoint: 'http://wpapi.local/wp-json',
} );
} );
it( 'can be used to retrieve a list of comments, omitting a password-protected comment', () => {
const prom = wp.comments()
.get()
.then( ( comments ) => {
expect( Array.isArray( comments ) ).toBe( true );
expect( comments.length ).toBe( 9 );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'fetches the first page, omitting a password-protected comment', () => {
const prom = wp.comments()
.get()
.then( ( comments ) => {
expect( getPostsAndAuthors( comments ) ).toEqual( expectedResults.postsAndAuthors.page1 );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'fetches the 10 oldest comments when sorted "asc"', () => {
const prom = wp.comments()
.order( 'asc' )
.get()
.then( ( comments ) => {
expect( getPostsAndAuthors( comments ) ).toEqual( expectedResults.postsAndAuthorsAsc.page1 );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
describe( 'paging properties', () => {
it( 'are exposed as _paging on the response array', () => {
const prom = wp.comments()
.get()
.then( ( posts ) => {
expect( posts ).toHaveProperty( '_paging' );
expect( typeof posts._paging ).toBe( 'object' );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'include the total number of posts', () => {
const prom = wp.comments()
.get()
.then( ( posts ) => {
expect( posts._paging ).toHaveProperty( 'total' );
expect( posts._paging.total ).toBe( 25 );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'include the total number of pages available', () => {
const prom = wp.comments()
.get()
.then( ( posts ) => {
expect( posts._paging ).toHaveProperty( 'totalPages' );
expect( posts._paging.totalPages ).toBe( 3 );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'provides a bound WPRequest for the next page as .next', () => {
const prom = wp.comments()
.get()
.then( ( posts ) => {
expect( posts._paging ).toHaveProperty( 'next' );
expect( typeof posts._paging.next ).toBe( 'object' );
expect( posts._paging.next ).toBeInstanceOf( WPRequest );
expect( posts._paging.next._options.endpoint )
.toEqual( 'http://wpapi.local/wp-json/wp/v2/comments?page=2' );
// Get last page & ensure 'next' no longer appears
return wp.comments()
.page( posts._paging.totalPages )
.get()
.then( ( posts ) => {
expect( posts._paging ).not.toHaveProperty( 'next' );
expect( getPostsAndAuthors( posts ) ).toEqual( expectedResults.postsAndAuthors.page3 );
return SUCCESS;
} );
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'allows access to the next page of results via .next', () => {
const prom = wp.comments()
.get()
.then( ( posts ) => {
return posts._paging.next
.get()
.then( ( posts ) => {
expect( Array.isArray( posts ) ).toBe( true );
expect( posts.length ).toBe( 10 );
expect( getPostsAndAuthors( posts ) ).toEqual( expectedResults.postsAndAuthors.page2 );
return SUCCESS;
} );
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'provides a bound WPRequest for the previous page as .prev', () => {
const prom = wp.comments()
.get()
.then( ( posts ) => {
expect( posts._paging ).not.toHaveProperty( 'prev' );
return posts._paging.next
.get()
.then( ( posts ) => {
expect( posts._paging ).toHaveProperty( 'prev' );
expect( typeof posts._paging.prev ).toBe( 'object' );
expect( posts._paging.prev ).toBeInstanceOf( WPRequest );
expect( posts._paging.prev._options.endpoint )
.toEqual( 'http://wpapi.local/wp-json/wp/v2/comments?page=1' );
return SUCCESS;
} );
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'allows access to the previous page of results via .prev', () => {
const prom = wp.comments()
.page( 2 )
.get()
.then( ( posts ) => {
expect( getPostsAndAuthors( posts ) ).toEqual( expectedResults.postsAndAuthors.page2 );
return posts._paging.prev
.get()
.then( ( posts ) => {
expect( Array.isArray( posts ) ).toBe( true );
// 9 because one comment is for a password-protected post
expect( posts.length ).toBe( 9 );
expect( getPostsAndAuthors( posts ) ).toEqual( expectedResults.postsAndAuthors.page1 );
return SUCCESS;
} );
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
} );
describe( 'querying by ID', () => {
let commentCollection;
let commentId;
let commentProm;
beforeEach( () => {
commentCollection = [];
commentProm = wp.comments()
.get()
.then( ( comments ) => {
commentCollection = comments;
commentId = commentCollection[4].id;
return wp.comments()
.id( commentId )
.get();
} );
} );
it( 'returns an object, not an array', () => {
const prom = commentProm
.then( ( comment ) => {
expect( Array.isArray( comment ) ).toBe( false );
expect( typeof comment ).toBe( 'object' );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'returns the correct comment', () => {
const prom = commentProm.then( ( comment ) => {
expect( comment.id ).toBe( commentId );
[ 'author_name', 'post', 'parent', 'date', 'status' ].forEach( ( prop ) => {
expect( comment[ prop ] ).toBe( commentCollection[4][ prop ] );
} );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
} );
describe( '.post() query', () => {
let pageComments;
let commentProm;
beforeEach( () => {
const pageId = 155;
commentProm = wp.pages()
.id( pageId )
.embed()
.get()
.then( ( page ) => {
// Do a flatten reduction because .replies will be an array of arrays
pageComments = page._embedded.replies.reduce( ( flatArr, arr ) => flatArr.concat( arr ), [] );
return wp.comments()
.post( pageId )
.get();
} );
} );
it( 'returns an array of posts', () => {
const prom = commentProm
.then( ( comments ) => {
expect( Array.isArray( comments ) ).toBe( true );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'returns the correct number of comments', () => {
const prom = commentProm
.then( ( comments ) => {
expect( comments.length ).toBe( 3 );
expect( comments.length ).toBe( pageComments.length );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
it( 'returns the correct comments', () => {
const prom = commentProm
.then( ( comments ) => {
pageComments.forEach( ( comment, i ) => {
[ 'id', 'parent', 'author', 'author_name' ].forEach( ( prop ) => {
expect( comment[ prop ] ).toBe( comments[ i ][ prop ] );
} );
expect( comment.content.rendered ).toBe( comments[ i ].content.rendered );
} );
return SUCCESS;
} );
return expect( prom ).resolves.toBe( SUCCESS );
} );
} );
} );