Skip to content

Commit

Permalink
Test: Improve code that uses use sandbox helper
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Apr 7, 2016
1 parent a6c4602 commit f37aa64
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import assert from 'assert';
import sinon from 'sinon';

describe( 'domain-management/edit/mapped-domain', () => {
let React,
MappedDomain,
i18n,
props,
sinonWrapper,
TestUtils;

before( () => {
Expand All @@ -22,7 +22,6 @@ describe( 'domain-management/edit/mapped-domain', () => {
} );

require( 'test/helpers/use-fake-dom' ).withContainer();
sinonWrapper = require( 'test/helpers/use-sinon' ).useSandbox();

require( 'test/helpers/use-mockery' )( mockery => {
React = require( 'react' );
Expand All @@ -41,16 +40,16 @@ describe( 'domain-management/edit/mapped-domain', () => {
assert( out );
} );

it( 'should use selectedSite.slug for URLs', () => {
it( 'should use selectedSite.slug for URLs', sinon.test( function() {
const paths = require( 'my-sites/upgrades/paths' );
const dnsStub = sinonWrapper.sandbox.stub( paths, 'domainManagementDns' );
const emailStub = sinonWrapper.sandbox.stub( paths, 'domainManagementEmail' );
const dnsStub = this.stub( paths, 'domainManagementDns' );
const emailStub = this.stub( paths, 'domainManagementEmail' );

const renderer = TestUtils.createRenderer();
renderer.render( <MappedDomain { ...props } /> );
renderer.getRenderOutput();

assert( dnsStub.calledWith( 'neverexpires.wordpress.com', 'neverexpires.com' ) );
assert( emailStub.calledWith( 'neverexpires.wordpress.com', 'neverexpires.com' ) );
} );
} ) );
} );
17 changes: 13 additions & 4 deletions client/my-sites/upgrades/domain-management/list/test/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
import assert from 'assert';

describe( 'upgrades/domain-management/list', function() {
const sinonWrapper = require( 'test/helpers/use-sinon' ).useSandbox();
/**
* Internal dependencies
*/
import { useSandbox } from 'test/helpers/use-sinon';

describe( 'upgrades/domain-management/list', function() {
let React,
ReactDom,
ReactInjection,
DomainList,
TestUtils,
i18n,
noticeTypes,
component;
component,
sandbox;

useSandbox( newSandbox => sandbox = newSandbox );

require( 'test/helpers/use-fake-dom' )( '<div id="main" />' );
require( 'test/helpers/use-mockery' )(
Expand Down Expand Up @@ -141,7 +150,7 @@ describe( 'upgrades/domain-management/list', function() {
setPrimaryDomainResolve,
setPrimaryDomainReject;
beforeEach( () => {
setPrimaryDomainStub = sinonWrapper.sandbox.stub( component, 'setPrimaryDomain' ).returns( new Promise( ( resolve, reject ) => {
setPrimaryDomainStub = sandbox.stub( component, 'setPrimaryDomain' ).returns( new Promise( ( resolve, reject ) => {
setPrimaryDomainResolve = resolve;
setPrimaryDomainReject = reject;
} ) );
Expand Down
14 changes: 5 additions & 9 deletions client/post-editor/media-modal/test/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ describe( 'EditorMediaModal', function() {
useMockery();
useFakeDom();
useI18n();
useSandbox( ( _sandbox ) => sandbox = _sandbox );
useSandbox( ( newSandbox ) => {
sandbox = newSandbox;
deleteMedia = sandbox.stub();
accept = sandbox.stub().callsArgWithAsync( 1, true );
} );

before( function() {
ModalViews = require( '../constants' ).Views;
i18n = require( 'lib/mixins/i18n' );

// Sinon
deleteMedia = sandbox.stub();
accept = sandbox.stub().callsArgWithAsync( 1, true );

// Mockery
mockery.registerMock( 'my-sites/media-library', EMPTY_COMPONENT );
mockery.registerMock( './detail', EMPTY_COMPONENT );
Expand All @@ -59,10 +59,6 @@ describe( 'EditorMediaModal', function() {
EditorMediaModal.prototype.__reactAutoBindMap.translate = i18n.translate;
} );

beforeEach( function() {
sandbox.reset();
} );

it( 'should prompt to delete a single item from the list view', function( done ) {
var media = DUMMY_MEDIA.slice( 0, 1 ),
tree;
Expand Down
2 changes: 1 addition & 1 deletion client/post-editor/media-modal/test/markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe( 'markup', function() {
let sandbox, markup, sites;

useFakeDom();
useSandbox( ( _sandbox ) => sandbox = _sandbox );
useSandbox( ( newSandbox ) => sandbox = newSandbox );

before( () => {
markup = require( '../markup' );
Expand Down
6 changes: 2 additions & 4 deletions client/post-editor/media-modal/test/preload-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ describe( '#preloadImage()', function() {
let sandbox, Image;

useFakeDom();
useSandbox( ( _sandbox ) => sandbox = _sandbox );

before( function() {
useSandbox( ( newSandbox ) => {
sandbox = newSandbox;
Image = sandbox.stub( global.window, 'Image' );
} );

beforeEach( function() {
sandbox.reset();
preloadImage.cache.clear();
} );

Expand Down
2 changes: 1 addition & 1 deletion client/post-editor/test/post-editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe( 'PostEditor', function() {
let sandbox, TestUtils, PostEditor, SitesList, PostEditStore;

useFakeDom();
useSandbox( ( _sandbox ) => sandbox = _sandbox );
useSandbox( ( newSandbox ) => sandbox = newSandbox );
useMockery();

before( () => {
Expand Down
8 changes: 2 additions & 6 deletions client/state/sites/media-storage/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ import {
import { useSandbox } from 'test/helpers/use-sinon';

describe( 'actions', () => {
let spy;
let sandbox;

beforeEach( () => {
spy = sandbox.spy();
} );
let sandbox, spy;

useSandbox( newSandbox => {
sandbox = newSandbox;
spy = sandbox.spy();
} );

describe( '#receiveMediaStorage()', () => {
Expand Down
3 changes: 0 additions & 3 deletions client/state/sites/media-storage/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ describe( 'reducer', () => {

useSandbox( newSandbox => {
sandbox = newSandbox;
} );

before( () => {
sandbox.stub( console, 'warn' );
} );

Expand Down
10 changes: 5 additions & 5 deletions test/test/helpers/use-sinon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,26 @@ export function useFakeTimers( now = 0, clockCallback = noop ) {
*
* @param {Object|Function} config The configuration to use, or a callback that is invoked with the sandbox instance
* @param {Function} sandboxCallback A callback function that is invoked with the sandbox instance
* @returns { {sandbox: Object} } Object with .sandbox property that points to the current sandbox instance
*/
export function useSandbox( config, sandboxCallback = noop ) {
const wrapper = {};
if ( isFunction( config ) && sandboxCallback === noop ) {
sandboxCallback = config;
config = undefined;
}

before( function() {
wrapper.sandbox = this.sandbox = sinon.sandbox.create( config );
this.sandbox = sinon.sandbox.create( config );
sandboxCallback( this.sandbox );
} );

beforeEach( function() {
this.sandbox.reset();
} );

after( function() {
if ( this.sandbox ) {
this.sandbox.restore();
this.sandbox = null;
wrapper.sandbox = null;
}
} );
return wrapper;
}

0 comments on commit f37aa64

Please sign in to comment.