Skip to content

Commit

Permalink
User: Add getCurrentUserLocale state selector
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Feb 18, 2016
1 parent 39470bf commit 6176d54
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
15 changes: 15 additions & 0 deletions client/state/current-user/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@ export function getCurrentUser( state ) {

return getUser( state, state.currentUser.id );
}

/**
* Returns the locale slug for the current user.
*
* @param {Object} state Global state tree
* @return {?String} Current user locale
*/
export function getCurrentUserLocale( state ) {
const user = getCurrentUser( state );
if ( ! user ) {
return null;
}

return user.localeSlug || null;
}
47 changes: 46 additions & 1 deletion client/state/current-user/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { expect } from 'chai';
/**
* Internal dependencies
*/
import { getCurrentUser } from '../selectors';
import {
getCurrentUser,
getCurrentUserLocale
} from '../selectors';

describe( 'selectors', () => {
describe( '#getCurrentUser()', () => {
Expand Down Expand Up @@ -35,4 +38,46 @@ describe( 'selectors', () => {
expect( selected ).to.eql( { ID: 73705554, login: 'testonesite2014' } );
} );
} );

describe( '#getCurrentUserLocale', () => {
it( 'should return null if the current user is not set', () => {
const locale = getCurrentUserLocale( {
currentUser: {
id: null
}
} );

expect( locale ).to.be.null;
} );

it( 'should return null if the current user locale slug is not set', () => {
const locale = getCurrentUserLocale( {
users: {
items: {
73705554: { ID: 73705554, login: 'testonesite2014' }
}
},
currentUser: {
id: 73705554
}
} );

expect( locale ).to.be.null;
} );

it( 'should return the current user locale slug', () => {
const locale = getCurrentUserLocale( {
users: {
items: {
73705554: { ID: 73705554, login: 'testonesite2014', localeSlug: 'fr' }
}
},
currentUser: {
id: 73705554
}
} );

expect( locale ).to.equal( 'fr' );
} );
} );
} );

0 comments on commit 6176d54

Please sign in to comment.