Skip to content

Commit

Permalink
Tests: port lib/users to single test runner
Browse files Browse the repository at this point in the history
update test doubles

register test with runner

refactor store test
  • Loading branch information
Jason Johnston committed Apr 15, 2016
1 parent ceb0425 commit 98b2df8
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 122 deletions.
4 changes: 2 additions & 2 deletions client/lib/user/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import useFakeDom from 'test/helpers/use-fake-dom';
describe( 'UserUtils', () => {
let UserUtils, user, configMock;

useFakeDom();
useFakeDom();

useMockery( mockery => {
configMock = sinon.stub();
Expand All @@ -31,7 +31,7 @@ describe( 'UserUtils', () => {
} );

context( 'without logout url', () => {
before( () => {
before( () => {
configMock.isEnabled
.withArgs( 'always_use_logout_url' )
.returns( false );
Expand Down
13 changes: 0 additions & 13 deletions client/lib/users/Makefile

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var site = require( './mock-site' ),
usersData = require( './mock-users-data' ),
moreUsersData = require( './mock-more-users-data' ),
deletedUserData = require( './mock-deleted-user-data' ),
updatedUserData = require( './mock-updated-single-user' ),
singleUserData = require( './mock-single-user' ),
pollingUsersData = require( './mock-polling-users-data' );
const site = require( './site' ),
usersData = require( './users' ),
moreUsersData = require( './more-users' ),
deletedUserData = require( './deleted-user' ),
updatedUserData = require( './updated-single-user' ),
singleUserData = require( './single-user' ),
pollingUsersData = require( './polling-users' );

module.exports = {
fetched: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import cloneDeep from 'lodash/cloneDeep';
/**
* Internal dependencies
*/
import usersData from './mock-users-data';
import moreUsersData from './mock-more-users-data';
import usersData from './users';
import moreUsersData from './more-users';

const clonedMoreUsers = cloneDeep( moreUsersData.users );
const updatedUsers = clonedMoreUsers.map( ( user ) => {
Expand Down
File renamed without changes.
File renamed without changes.
204 changes: 106 additions & 98 deletions client/lib/users/test/store.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
require( 'lib/react-test-env-setup' )();

/**
* External dependencies
*/
const assert = require( 'chai' ).assert;

import { assert } from 'chai';
import findIndex from 'lodash/findIndex';
import some from 'lodash/some';
import isUndefined from 'lodash/isUndefined';
/**
* Internal dependencies
*/
const actions = require( './lib/mock-actions' ),
site = require( './lib/mock-site' ),
usersData = require( './lib/mock-users-data' );
import useFakeDom from 'test/helpers/use-fake-dom';
import actions from './fixtures/actions';
import site from './fixtures/site';
import usersData from './fixtures/users';

/**
* Module variables
*/
const siteId = site.ID,
options = {
siteId: siteId
};
describe( 'Users Store', () => {
var Dispatcher, UsersStore, siteId, options;

describe( 'Users Store', function() {
var Dispatcher, UsersStore;
useFakeDom();

beforeEach( function() {
beforeEach( () => {
Dispatcher = require( 'dispatcher' );
UsersStore = require( 'lib/users/store' );
siteId = site.ID;
options = { siteId };
} );

it( 'Store should be an object', function() {
it( 'Store should be an object', () => {
assert.isObject( UsersStore );
} );

it( 'Store should have method emitChange', function() {
it( 'Store should have method emitChange', () => {
assert.isFunction( UsersStore.emitChange );
} );

describe( 'Getting user by login', function() {
it( 'Store should have method getUserByLogin', function() {
describe( 'Getting user by login', () => {
it( 'Store should have method getUserByLogin', () => {
assert.isFunction( UsersStore.getUserByLogin );
} );

it( 'Should return undefined when user not in store', function() {
it( 'Should return undefined when user not in store', () => {
assert.isUndefined( UsersStore.getUserByLogin( siteId, usersData.users[0].login ) );
} );

it( 'Should return a user object when the user exists', function() {
var user;
it( 'Should return a user object when the user exists', () => {
let user;
Dispatcher.handleServerAction( actions.fetched );
user = UsersStore.getUserByLogin( siteId, usersData.users[0].login );

Expand All @@ -55,103 +52,121 @@ describe( 'Users Store', function() {
} );
} );

describe( 'Fetch Users', function() {
beforeEach( function() {
describe( 'Fetch Users', () => {
let users;

beforeEach( () => {
Dispatcher.handleServerAction( actions.fetched );
users = UsersStore.getUsers( options );
} );

it( 'Should update the store on RECEIVE_USERS', function() {
assert.isNotNull( UsersStore.getUsers( options ) );
it( 'Should update the store on RECEIVE_USERS', () => {
assert.isNotNull( users );
} );

it( 'The users store should return an array of objects when fetching users', function() {
var users = UsersStore.getUsers( options );
it( 'The users store should return an array of objects when fetching users', () => {
assert.isArray( users );
assert.isObject( users[0] );
} );

it( 'A user object from the store should contain an array of roles', function() {
var users = UsersStore.getUsers( options ),
user = users[0];
assert.isArray( user.roles );
it( 'A user object from the store should contain an array of roles', () => {
assert.isArray( users[0].roles );
} );

it( 'Re-fetching a list of users when a users was deleted from the site should result in a smaller array', function() {
var users = UsersStore.getUsers( options ),
usersAgain;
assert.equal( users.length, 5 );
it( 'Re-fetching a list of users when a users was deleted from the site should result in a smaller array', () => {
let lessUsers;
Dispatcher.handleServerAction( actions.fetchAgainUserDeleted );
usersAgain = UsersStore.getUsers( options );
assert.equal( usersAgain.length, 4 );
lessUsers = UsersStore.getUsers( options );
assert.isBelow( lessUsers.length, users.length );
} );

it( 'Fetching more users should add to the array of objects', function() {
var users = UsersStore.getUsers( options ),
moreUsers;
assert.equal( users.length, 5 );
it( 'Fetching more users should add to the array of objects', () => {
let moreUsers;
Dispatcher.handleServerAction( actions.fetchMoreUsers );
moreUsers = UsersStore.getUsers( options );
assert.equal( moreUsers.length, 7 );
assert.isAbove( moreUsers.length, users.length );
} );
} );

describe( 'Pagination', () => {
let pagination;
beforeEach( () => {
Dispatcher.handleServerAction( actions.fetched );
pagination = UsersStore.getPaginationData( options );
} );

it( 'Pagination data should update when we fetch more users', function() {
var pagination = UsersStore.getPaginationData( options );
it( 'has the correct total users', () => {
assert.equal( pagination.totalUsers, 7 );
} );

it( 'has the correct offset', () => {
assert.equal( pagination.usersCurrentOffset, 0 );
} );

it( 'fetches the correct number of users', () => {
assert.equal( pagination.numUsersFetched, 5 );
Dispatcher.handleServerAction( actions.fetchMoreUsers );
pagination = UsersStore.getPaginationData( options );
assert.equal( pagination.usersCurrentOffset, 5 );
assert.equal( pagination.numUsersFetched, 7 );
} );

context( 'after fetching more users', () => {
beforeEach( () => {
Dispatcher.handleServerAction( actions.fetchMoreUsers );
pagination = UsersStore.getPaginationData( options );
} );

it( 'has the correct offset', () => {
assert.equal( pagination.usersCurrentOffset, 5 );
} );

it( 'fetches the correct number of users', () => {
assert.equal( pagination.numUsersFetched, 7 );
} );
} );
} );

describe( 'Polling updated users', function() {
beforeEach( function() {
describe( 'Polling updated users', () => {
beforeEach( () => {
Dispatcher.handleServerAction( actions.fetched );
Dispatcher.handleServerAction( actions.fetchMoreUsers );
Dispatcher.handleServerAction( actions.receiveUpdatedUsers );
} );

it( 'getUpdatedParams returns correct params', function() {
var updatedParams = UsersStore.getUpdatedParams( options );
it( 'getUpdatedParams returns correct params', () => {
const updatedParams = UsersStore.getUpdatedParams( options );
assert.equal( updatedParams.offset, 0 );
assert.equal( updatedParams.number, usersData.found )
} );

it( 'Polling updates expected users', function() {
var updatedUsers = UsersStore.getUsers( options );
it( 'Polling updates expected users', () => {
const updatedUsers = UsersStore.getUsers( options );
assert.equal( updatedUsers.length, usersData.found );

// The last two users should have a 'contributor' role
updatedUsers.slice( -2, 2 ).forEach( function( user ) {
updatedUsers.slice( -2, 2 ).forEach( user => {
assert.equal( user.roles[0], 'contributor' );
} );
} );
} );

describe( 'Update a user', function() {
beforeEach( function() {
describe( 'Update a user', () => {
beforeEach( () => {
Dispatcher.handleServerAction( actions.fetched );
} );
it( 'Should update a specific user with new attributes', function() {
var users = UsersStore.getUsers( options ),
usersAgain, testUserIndex;

users.forEach( function( user, i ) {
if ( user.name !== 'Test One' ) {
return;
}
testUserIndex = i;
} );
it( 'Should update a specific user with new attributes', () => {
const users = UsersStore.getUsers( options ),
testUserIndex = findIndex( users, user => user.name === 'Test One' );
let usersAgain;

Dispatcher.handleServerAction( actions.updateSingleUser );
usersAgain = UsersStore.getUsers( options );
assert.equal( usersAgain[ testUserIndex ].name, 'Test Won' );
} );
it( 'Error should restore the updated user', function() {
var userId = usersData.users[0].ID,
user, userAgain, userRestored;
user = UsersStore.getUser( siteId, userId );

it( 'Error should restore the updated user', () => {
const userId = usersData.users[0].ID,
user = UsersStore.getUser( siteId, userId );
let userAgain, userRestored;

assert.equal( user.name, 'Test One' );

Dispatcher.handleServerAction( actions.updateSingleUser );
Expand All @@ -165,24 +180,21 @@ describe( 'Users Store', function() {
} );

describe( 'Delete a user', function() {
let userId, userAgain;

beforeEach( function() {
Dispatcher.handleServerAction( actions.fetched );
userId = usersData.users[0].ID;
} );
it( 'Should delete a specific user', function() {
var userId = usersData.users[0].ID,
user, userAgain;
user = UsersStore.getUser( siteId, userId );
assert.equal( user.name, 'Test One' );

it( 'Should delete a specific user', () => {
Dispatcher.handleServerAction( actions.deleteUser );
userAgain = UsersStore.getUser( siteId, userId );
assert.equal( userAgain, null );
} );
it( 'Error should restore the deleted user', function() {
var userId = usersData.users[0].ID,
user, userAgain, userRestored;
user = UsersStore.getUser( siteId, userId );
assert.equal( user.name, 'Test One' );

it( 'Error should restore the deleted user', () => {
let userRestored;

Dispatcher.handleServerAction( actions.deleteUser );
userAgain = UsersStore.getUser( siteId, userId );
Expand All @@ -192,31 +204,27 @@ describe( 'Users Store', function() {
userRestored = UsersStore.getUser( siteId, userId );
assert.equal( userRestored.name, 'Test One' );
} );
it( 'There should be no undefined objects in user array after deleting a user', function() {
var userId = usersData.users[0].ID,
user, users;
user = UsersStore.getUser( siteId, userId );
assert.equal( user.name, 'Test One' );

it( 'There should be no undefined objects in user array after deleting a user', () => {
let users, someUndefined;
Dispatcher.handleServerAction( actions.deleteUser );
users = UsersStore.getUsers( options );
users.forEach( _user => {
assert.notEqual( undefined, _user );
} );
someUndefined = some( users, isUndefined );
assert.isFalse( someUndefined );
} );
} );

describe( 'Get single user', function() {
beforeEach( function() {
describe( 'Get single user', () => {
beforeEach( () => {
Dispatcher.handleServerAction( actions.fetched );
} );
it( 'Fetching a single user should add to the store', function() {
var users = UsersStore.getUsers( options ),
usersAgain;
assert.equal( 5, users.length );
it( 'Fetching a single user should add to the store', () => {
const users = UsersStore.getUsers( options );
let usersAgain;
assert.lengthOf( users, 5 );
Dispatcher.handleServerAction( actions.receiveSingleUser );
usersAgain = UsersStore.getUsers( options );
assert.equal( 6, usersAgain.length );
assert.lengthOf( usersAgain, 6 );
} );
} );
} );

0 comments on commit 98b2df8

Please sign in to comment.