Skip to content

Commit

Permalink
Stub network handlers for e2e tests
Browse files Browse the repository at this point in the history
Supported natively in Cypress, but not yet for window.fetch

See: cypress-io/cypress#95
  • Loading branch information
aduth committed Nov 19, 2017
1 parent 3c4dfa5 commit ca99db8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 18 deletions.
2 changes: 1 addition & 1 deletion e2e/tests/create-done.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe( 'Create done', () => {
it( 'Edits done', () => {
cy.get( '.done-text' ).focus();
cy.get( ':focus' ).type( ' with #tag!{enter}' );
cy.get( '.dones-list__item:not( .is-placeholder )' )
cy.get( '.dones-list__item' )
.should( 'have.length', 1 )
.and( 'have.text', '\u0000Auto\u0000 focus with #tag!' );
cy.get( '.done-text code' ).should( 'have.length', 1 );
Expand Down
85 changes: 76 additions & 9 deletions e2e/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,88 @@
<link rel="stylesheet" href="dist/style.css">
<div id="app"></div>
<script>
const HANDLERS = [
{
method: 'GET',
url: '/wp-json/dones/v1/users',
response: [ {
avatar: 'http://www.gravatar.com/avatar/?d=identicon',
id: 1,
name: 'Anonymous User',
} ],
},
{
method: 'GET',
url: '/wp-json/dones/v1/dones',
response: [],
},
{
method: 'POST',
url: '/wp-json/dones/v1/dones',
response: function( params ) {
return params.body;
},
},
{
method: '*',
url: '*',
response: Promise.reject( new Error() ),
},
];

window.fetch = function( path, params ) {
const method = params.method || 'GET';
path = path.split( '?' )[ 0 ];

let handler;
for ( let i = 0; i < HANDLERS.length; i++ ) {
handler = HANDLERS[ i ];

// Test method match, allow wildcard method
if ( handler.method !== '*' && handler.method !== method ) {
continue;
}

// Escape URL as regex, allowing wildcard path
let regex = handler.url
.replace( /[-[\]{}()+?.,\\^$|#\s]/g, '\\$&' )
.replace( '*', '.*' );

regex = new RegExp( regex );

if ( regex.test( path ) ) {
break;
}
}

let response = handler.response;

if ( 'function' === typeof response ) {
response = response( params );
}

if ( ! ( response instanceof Promise ) ) {
response = Promise.resolve( response );
}

return response.then( function( result ) {
// Form response as fetch Response object
return {
json: function() {
return Promise.resolve( result );
},
headers: [],
};
} );
};

window.dones = {
apiRoot: '/wp-json',
brandColor: '#986dda',
dateFormat: 'F j, Y',
gmtOffset: 0,
i18n: {},
preload: {
'/dones/v1/users': {
body: [ {
avatar: 'http://www.gravatar.com/avatar/?d=identicon',
id: 1,
name: 'Anonymous User',
} ],
},
},
preload: {},
siteName: 'Dones',
siteUrl: window.location.href.replace( /\/$/, '' ),
userId: 1,
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
"prebuild": "npm run clean",
"build:es6": "cross-env NODE_ENV=production webpack",
"build:legacy": "cross-env BUILD_TARGET=legacy BABEL_ENV=legacy NODE_ENV=production webpack",
"postbuild:e2e": "cp -r dist e2e/www",
"build": "npm-run-all --parallel build:*",
"build-theme": "./bin/build-theme.sh",
"predev": "npm run clean",
"dev": "webpack --watch",
"test-file": "cross-env NODE_PATH=src NODE_ENV=test mocha -r mocha.config.js -r babel-register",
"test:lint": "eslint .",
"test:unit": "npm run test-file -- 'src/**/test/*.js'",
"pretest:e2e": "NODE_ENV=production DISABLE_NETWORK=1 webpack",
"pretest:e2e": "npm run build && cp -r dist e2e/www",
"test:e2e": "cypress run --browser chrome",
"pretest": "npm run clean",
"test": "npm-run-all --parallel test:*"
Expand Down
4 changes: 0 additions & 4 deletions src/state/requests/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ export default ( { dispatch, getState } ) => {
}
}

if ( process.env.DISABLE_NETWORK ) {
return;
}

// Append current nonce if exists
const nonce = getRequestNonce( state );
if ( nonce ) {
Expand Down
3 changes: 1 addition & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const webpack = require( 'webpack' );
const ExtractTextPlugin = require( 'extract-text-webpack-plugin' );
const UglifyJsPlugin = require( 'uglifyjs-webpack-plugin' );

const { BUILD_TARGET, NODE_ENV, DISABLE_NETWORK } = process.env;
const { BUILD_TARGET, NODE_ENV } = process.env;

const config = module.exports = {
entry: {
Expand Down Expand Up @@ -49,7 +49,6 @@ const config = module.exports = {
plugins: [
new webpack.DefinePlugin( {
'process.env.NODE_ENV': JSON.stringify( NODE_ENV ),
'process.env.DISABLE_NETWORK': JSON.stringify( DISABLE_NETWORK ),
} ),
new webpack.optimize.CommonsChunkPlugin( {
name: 'vendor',
Expand Down

0 comments on commit ca99db8

Please sign in to comment.