Skip to content

Commit

Permalink
Adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jun 1, 2018
1 parent caa9ae9 commit da95f14
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 8 deletions.
10 changes: 5 additions & 5 deletions packages/api-request/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import jQuery from 'jquery';
/**
* Internal dependencies
*/
import createNonceMiddleware from './nonce-middleware';
import createRootURLMiddleware from './root-url-middleware';
import createPreloadingMiddleware from './preloading-middleware';
import namespaceEndpointMiddleware from './namespace-endpoint-middleware';
import httpV1Middleware from './http-v1-middleware';
import createNonceMiddleware from './middlewares/nonce';
import createRootURLMiddleware from './middlewares/root-url';
import createPreloadingMiddleware from './middlewares/preloading';
import namespaceEndpointMiddleware from './middlewares/namespace-endpoint';
import httpV1Middleware from './middlewares/http-v1';

const middlewares = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function httpV1Middleware( options, next ) {
if ( newOptions.method ) {
if ( [ 'PATCH', 'PUT', 'DELETE' ].indexOf( newOptions.method.toUpperCase() ) >= 0 ) {
if ( ! newOptions.headers ) {
options.headers = {};
newOptions.headers = {};
}
newOptions.headers[ 'X-HTTP-Method-Override' ] = newOptions.method;
newOptions.method = 'POST';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import jQuery from 'jquery';

const createNonceMiddleware = ( nonce ) => ( options, next ) => {
let usedNonce = nonce;
/**
Expand All @@ -6,7 +11,7 @@ const createNonceMiddleware = ( nonce ) => ( options, next ) => {
* Configure heartbeat to refresh the wp-api nonce, keeping the editor
* authorization intact.
*/
window.jQuery( document ).on( 'heartbeat-tick', ( event, response ) => {
jQuery( document ).on( 'heartbeat-tick', ( event, response ) => {
if ( response[ 'rest-nonce' ] ) {
usedNonce = response[ 'rest-nonce' ];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import namespaceAndEndpointMiddleware from './namespace-endpoint-middleware';
import namespaceAndEndpointMiddleware from './namespace-endpoint';

const createRootURLMiddleware = ( rootURL ) => ( options, next ) => {
return namespaceAndEndpointMiddleware( options, ( optionsWithPath ) => {
Expand Down
21 changes: 21 additions & 0 deletions packages/api-request/src/middlewares/test/http-v1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import httpV1Middleware from '../http-v1';

describe( 'HTTP v1 Middleware', () => {
it( 'should use a POST for a PUT requests', () => {
const callback = ( options ) => {
expect( options.method ).toBe( 'POST' );
expect( options.headers[ 'X-HTTP-Method-Override' ] ).toBe( 'PUT' );
};

httpV1Middleware( { method: 'PUT', data: {} }, callback );
} );

it( 'shouldn\'t touch the options for GET requests', () => {
const requestOptions = { method: 'GET', path: '/wp/v2/posts' };
const callback = ( options ) => {
expect( options ).toEqual( requestOptions );
};

httpV1Middleware( requestOptions, callback );
} );
} );
18 changes: 18 additions & 0 deletions packages/api-request/src/middlewares/test/namespace-endpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import namespaceEndpointMiddleware from '../namespace-endpoint';

describe( 'Namespace & Endpoint middleware', () => {
it( 'should concat the endpoint and namespace into a path property', () => {
const requestOptions = {
method: 'GET',
namespace: '/wp/v2',
endpoint: '/posts',
};
const callback = ( options ) => {
expect( options.path ).toBe( 'wp/v2/posts' );
expect( options.namespace ).toBeUndefined();
expect( options.endpoint ).toBeUndefined();
};

namespaceEndpointMiddleware( requestOptions, callback );
} );
} );
32 changes: 32 additions & 0 deletions packages/api-request/src/middlewares/test/nonce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import createNonceMiddleware from '../nonce';

describe( 'Nonce middleware', () => {
it( 'should add a nonce header to the request', () => {
const nonce = 'nonce';
const nonceMiddleware = createNonceMiddleware( nonce );
const requestOptions = {
method: 'GET',
path: '/wp/v2/posts',
};
const callback = ( options ) => {
expect( options.headers[ 'X-WP-Nonce' ] ).toBe( nonce );
};

nonceMiddleware( requestOptions, callback );
} );

it( 'should not add a nonce header to requests with nonces', () => {
const nonce = 'nonce';
const nonceMiddleware = createNonceMiddleware( nonce );
const requestOptions = {
method: 'GET',
path: '/wp/v2/posts',
headers: { 'X-WP-Nonce': 'existing nonce' },
};
const callback = ( options ) => {
expect( options.headers[ 'X-WP-Nonce' ] ).toBe( 'existing nonce' );
};

nonceMiddleware( requestOptions, callback );
} );
} );
41 changes: 41 additions & 0 deletions packages/api-request/src/middlewares/test/preloading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import createPreloadingMiddleware from '../preloading';

describe( 'Preloading Middleware', () => {
it( 'should return the preloaded data if provided', () => {
const body = {
status: 'this is the preloaded response',
};
const preloadedData = {
'wp/v2/posts': {
body,
},
};
const prelooadingMiddleware = createPreloadingMiddleware( preloadedData );
const requestOptions = {
method: 'GET',
path: 'wp/v2/posts',
};

const response = prelooadingMiddleware( requestOptions );
response.then( ( value ) => {
expect( value ).toEqual( body );
} );
} );

it( 'should move to the next middleware if no preloaded data', () => {
const preloadedData = {};
const prelooadingMiddleware = createPreloadingMiddleware( preloadedData );
const requestOptions = {
method: 'GET',
path: 'wp/v2/posts',
};

const callback = ( options ) => {
expect( options ).toBe( requestOptions );
return true;
};

const ret = prelooadingMiddleware( requestOptions, callback );
expect( ret ).toBe( true );
} );
} );
17 changes: 17 additions & 0 deletions packages/api-request/src/middlewares/test/root-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import createRootUrlMiddleware from '../root-url';

describe( 'Root URL middleware', () => {
it( 'should append the root URL', () => {
const rootURL = 'http://wp.org/wp-admin/rest/';
const rootURLMiddleware = createRootUrlMiddleware( rootURL );
const requestOptions = {
method: 'GET',
path: '/wp/v2/posts',
};
const callback = ( options ) => {
expect( options.url ).toBe( 'http://wp.org/wp-admin/rest/wp/v2/posts' );
};

rootURLMiddleware( requestOptions, callback );
} );
} );

0 comments on commit da95f14

Please sign in to comment.