diff --git a/client/lib/user/test/utils.js b/client/lib/user/test/utils.js index a4bdd5f5d641e..fc58e12a73bcc 100644 --- a/client/lib/user/test/utils.js +++ b/client/lib/user/test/utils.js @@ -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(); @@ -31,7 +31,7 @@ describe( 'UserUtils', () => { } ); context( 'without logout url', () => { - before( () => { + before( () => { configMock.isEnabled .withArgs( 'always_use_logout_url' ) .returns( false ); diff --git a/client/lib/users/Makefile b/client/lib/users/Makefile deleted file mode 100644 index 42f0832034e1d..0000000000000 --- a/client/lib/users/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -NODE_BIN := $(shell npm bin) -MOCHA ?= $(NODE_BIN)/mocha -BASE_DIR := $(NODE_BIN)/../.. -NODE_PATH := test:$(BASE_DIR)/client -COMPILERS ?= js:babel/register -REPORTER ?= spec -UI ?= bdd - -test: - @NODE_ENV=test NODE_PATH=$(NODE_PATH) $(MOCHA) --compilers $(COMPILERS) --reporter $(REPORTER) --ui $(UI) - -.PHONY: test - diff --git a/client/lib/users/test/lib/mock-actions.js b/client/lib/users/test/fixtures/actions.js similarity index 80% rename from client/lib/users/test/lib/mock-actions.js rename to client/lib/users/test/fixtures/actions.js index f052884e6eb14..f8cb46060cc5f 100644 --- a/client/lib/users/test/lib/mock-actions.js +++ b/client/lib/users/test/fixtures/actions.js @@ -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: { diff --git a/client/lib/users/test/lib/mock-deleted-user-data.js b/client/lib/users/test/fixtures/deleted-user.js similarity index 100% rename from client/lib/users/test/lib/mock-deleted-user-data.js rename to client/lib/users/test/fixtures/deleted-user.js diff --git a/client/lib/users/test/lib/mock-more-users-data.js b/client/lib/users/test/fixtures/more-users.js similarity index 100% rename from client/lib/users/test/lib/mock-more-users-data.js rename to client/lib/users/test/fixtures/more-users.js diff --git a/client/lib/users/test/lib/mock-polling-users-data.js b/client/lib/users/test/fixtures/polling-users.js similarity index 80% rename from client/lib/users/test/lib/mock-polling-users-data.js rename to client/lib/users/test/fixtures/polling-users.js index 2440935399fe7..56fa95d3b5879 100644 --- a/client/lib/users/test/lib/mock-polling-users-data.js +++ b/client/lib/users/test/fixtures/polling-users.js @@ -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 ) => { diff --git a/client/lib/users/test/lib/mock-single-user.js b/client/lib/users/test/fixtures/single-user.js similarity index 100% rename from client/lib/users/test/lib/mock-single-user.js rename to client/lib/users/test/fixtures/single-user.js diff --git a/client/lib/users/test/lib/mock-site.js b/client/lib/users/test/fixtures/site.js similarity index 100% rename from client/lib/users/test/lib/mock-site.js rename to client/lib/users/test/fixtures/site.js diff --git a/client/lib/users/test/lib/mock-updated-single-user.js b/client/lib/users/test/fixtures/updated-single-user.js similarity index 100% rename from client/lib/users/test/lib/mock-updated-single-user.js rename to client/lib/users/test/fixtures/updated-single-user.js diff --git a/client/lib/users/test/lib/mock-users-data.js b/client/lib/users/test/fixtures/users.js similarity index 100% rename from client/lib/users/test/lib/mock-users-data.js rename to client/lib/users/test/fixtures/users.js diff --git a/client/lib/users/test/store.js b/client/lib/users/test/store.js index 069c5f4a6d2e2..eee794790cf24 100644 --- a/client/lib/users/test/store.js +++ b/client/lib/users/test/store.js @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); } ); } ); } );