Skip to content

Commit

Permalink
Tests: port lib/user tests to single runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Johnston committed Apr 15, 2016
1 parent a929638 commit 95fae6b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 82 deletions.
12 changes: 0 additions & 12 deletions client/lib/user/Makefile

This file was deleted.

112 changes: 42 additions & 70 deletions client/lib/user/test/utils.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,69 @@
/* eslint-disable vars-on-top */
require( 'lib/react-test-env-setup' )();

/**
* External dependencies
*/

var expect = require( 'chai' ).expect,
mockery = require( 'mockery' ),
sinon = require( 'sinon' );
import { expect } from 'chai';
import sinon from 'sinon';

/**
* Internal dependencies
*/
import useMockery from 'test/helpers/use-mockery';
import useFakeDom from 'test/helpers/use-fake-dom';

const LOGOUT_WITH_DOMAIN = '/url-with-|subdomain|';
const LOGOUT_WITHOUT_DOMAIN = '/url-without-domain';

describe( 'UserUtils', function() {
var user, UserUtils;
var logout_URL;
var always_use_logout_url = false;
var mockedConfig = function() {
return logout_URL;
};
mockedConfig.isEnabled = function( feature ) {
if ( feature === 'desktop' ) {
return false;
}

if ( feature === 'always_use_logout_url' ) {
return always_use_logout_url;
}

return true;
};
describe( 'UserUtils', () => {
let UserUtils, user, configMock;

this.timeout( 10 * 1000 );
useFakeDom();

beforeEach( function() {
mockery.enable( {
warnOnReplace: false,
warnOnUnregistered: false
} );

mockery.registerMock( 'config', mockedConfig );
useMockery( mockery => {
configMock = sinon.stub();
configMock.isEnabled = sinon.stub();
mockery.registerMock( 'config', configMock );
} );

before( () => {
user = require( 'lib/user' )();
UserUtils = require( '../utils' );
} );

afterEach( function() {
mockery.disable();
mockery.deregisterMock( 'config' );
beforeEach( () => {
configMock.returns( '/url-with-|subdomain|' );
} );

it( 'uses userData.logout_URL when available', function() {
sinon.stub( user, 'get', function() {
return {
logout_URL: '/userdata'
};
context( 'without logout url', () => {
before( () => {
configMock.isEnabled
.withArgs( 'always_use_logout_url' )
.returns( false );
} );

always_use_logout_url = false;
logout_URL = LOGOUT_WITH_DOMAIN;
expect( UserUtils.getLogoutUrl() ).to.be.equals( '/userdata' );

user.get.restore();
} );

it( 'works when |subdomain| is not present', function() {
always_use_logout_url = true;
logout_URL = LOGOUT_WITHOUT_DOMAIN;
expect( UserUtils.getLogoutUrl() ).to.be.equals( '/url-without-domain' );
it( 'uses userData.logout_URL when available', () => {
sinon.stub( user, 'get' ).returns( { logout_URL: '/userdata' } );
expect( UserUtils.getLogoutUrl() ).to.equal( '/userdata' );
user.get.restore();
} );
} );

it( 'replaces |subdomain| when present and have domain', function() {
sinon.stub( user, 'get', function() {
return {
localeSlug: 'es'
};
context( 'with logout url', () => {
before( () => {
configMock.isEnabled
.withArgs( 'always_use_logout_url' )
.returns( true );
} );

always_use_logout_url = true;
logout_URL = LOGOUT_WITH_DOMAIN;
expect( UserUtils.getLogoutUrl() ).to.be.equals( '/url-with-es.' );

user.get.restore();
} );
it( 'works when |subdomain| is not present', () => {
configMock.returns( '/url-without-domain' );
expect( UserUtils.getLogoutUrl() ).to.equal( '/url-without-domain' );
} );

it( 'replaces |subdomain| when present but no domain', function() {
always_use_logout_url = true;
logout_URL = LOGOUT_WITH_DOMAIN;
it( 'replaces |subdomain| when present and have domain', () => {
sinon.stub( user, 'get' ).returns( { localeSlug: 'es' } );
expect( UserUtils.getLogoutUrl() ).to.equal( '/url-with-es.' );
user.get.restore();
} );

expect( UserUtils.getLogoutUrl() ).to.be.equals( '/url-with-' );
it( 'replaces |subdomain| when present but no domain', () => {
expect( UserUtils.getLogoutUrl() ).to.equal( '/url-with-' );
} );
} );
} );
3 changes: 3 additions & 0 deletions client/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@
"tree-convert": {
"test": [ "index" ]
},
"user": {
"test": [ "utils" ]
},
"wp": {
"localization": {
"test": [ "index" ]
Expand Down

0 comments on commit 95fae6b

Please sign in to comment.