-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
api-fetch-setup.test.js
109 lines (97 loc) · 2.86 KB
/
api-fetch-setup.test.js
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
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { isPathSupported, shouldEnableCaching } from '../api-fetch-setup';
const supportedPaths = {
GET: [
'wp/v2/media/54?context=edit&_locale=user',
'wp/v2/media/5?context=edit',
'wp/v2/media/54/',
'wp/v2/media/',
'wp/v2/media?context=edit&_locale=user',
'wp/v2/categories/',
'wp/v2/blocks/28?_locale=user',
'/wp/v2/blocks?per_page=100&context=edit&_locale=user',
],
};
// Made up examples.
const unsupportedPaths = {
GET: [ 'wp/v1/media/' ],
POST: [ 'wp/v2/categories' ],
};
const enabledCachingPaths = [
'wp/v2/media/54?context=edit&_locale=user',
'wp/v2/media/5?context=edit',
'wp/v2/media/54/',
'wp/v2/media/',
'wp/v2/media?context=edit&_locale=user',
'wp/v2/categories/',
];
const disabledCachingPaths = [
'wp/v2/blocks/28?_locale=user',
'/wp/v2/blocks?per_page=100&context=edit&_locale=user',
];
describe( 'isPathSupported', () => {
describe( 'GET requests', () => {
supportedPaths.GET.forEach( ( path ) => {
it( `supports ${ path }`, () => {
expect( isPathSupported( path, 'GET' ) ).toBe( true );
} );
} );
unsupportedPaths.GET.forEach( ( path ) => {
it( `does not support ${ path }`, () => {
expect( isPathSupported( path, 'GET' ) ).toBe( false );
} );
} );
} );
describe( 'POST requests', () => {
unsupportedPaths.POST.forEach( ( path ) => {
it( `does not support ${ path }`, () => {
expect( isPathSupported( path, 'POST' ) ).toBe( false );
} );
} );
} );
it( 'checks supported endpoints provided by WP hook', () => {
addFilter(
'native.supported_endpoints',
'gutenberg-mobile',
( endpoints ) => {
return {
GET: [ ...endpoints.GET, /test\/get-endpoint/i ],
POST: [ ...endpoints.POST, /test\/post-endpoint/i ],
};
}
);
expect( isPathSupported( 'test/get-endpoint', 'GET' ) ).toBe( true );
expect( isPathSupported( 'test/post-endpoint', 'POST' ) ).toBe( true );
expect( isPathSupported( 'test/bad-endpoint', 'GET' ) ).toBe( false );
expect( isPathSupported( 'test/bad-endpoint', 'POST' ) ).toBe( false );
} );
} );
describe( 'shouldEnableCaching', () => {
enabledCachingPaths.forEach( ( path ) => {
it( `enables caching for ${ path }`, () => {
expect( shouldEnableCaching( path ) ).toBe( true );
} );
} );
disabledCachingPaths.forEach( ( path ) => {
it( `does not enable caching for ${ path }`, () => {
expect( shouldEnableCaching( path ) ).toBe( false );
} );
} );
it( 'does not enable caching for endpoints provided to filter', () => {
addFilter(
'native.disabled_caching_endpoints',
'gutenberg-mobile',
( endpoints ) => {
return [ ...endpoints, /wp\/v2\/categories/i ];
}
);
// Filter was used to stop caching an endpoint from `enabledCachingPaths` array.
expect( shouldEnableCaching( 'wp/v2/categories' ) ).toBe( false );
} );
} );