Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 37 additions & 24 deletions editor/sidebar/post-author/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* External dependencies
*/
import { connect } from 'react-redux';
import { filter, flowRight } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { PanelRow, withInstanceId } from '@wordpress/components';
import { PanelRow, withAPIData, withInstanceId } from '@wordpress/components';
import { Component } from '@wordpress/element';

/**
Expand All @@ -17,40 +18,40 @@ import './style.scss';
import { getEditedPostAttribute } from '../../selectors';
import { editPost } from '../../actions';

class PostAuthor extends Component {
export class PostAuthor extends Component {
constructor() {
super( ...arguments );
this.state = {
authors: [],
};
}

fetchAuthors() {
this.fetchAuthorsRequest = new wp.api.collections.Users().fetch( { data: {
per_page: 100,
} } );
this.fetchAuthorsRequest.then( ( authors ) => {
this.setState( { authors } );
} );
this.setAuthorId = this.setAuthorId.bind( this );
}

componentDidMount() {
this.fetchAuthors();
setAuthorId( event ) {
const { onUpdateAuthor } = this.props;
const { value } = event.target;
onUpdateAuthor( Number( value ) );
}

componentWillUnmount() {
this.fetchAuthorsRequest.abort();
getAuthors() {
// While User Levels are officially deprecated, the behavior of the
// existing users dropdown on `who=authors` tests `user_level != 0`
//
// See: https://github.com/WordPress/WordPress/blob/a193916/wp-includes/class-wp-user-query.php#L322-L327
// See: https://codex.wordpress.org/Roles_and_Capabilities#User_Levels
const { users } = this.props;
return filter( users.data, ( user ) => {
return user.capabilities.level_1;
} );
}

render() {
const { onUpdateAuthor, postAuthor, instanceId } = this.props;
const { authors } = this.state;
const selectId = 'post-author-selector-' + instanceId;

const authors = this.getAuthors();
if ( authors.length < 2 ) {
return null;
}

const { postAuthor, instanceId } = this.props;
const selectId = 'post-author-selector-' + instanceId;

// Disable reason: A select with an onchange throws a warning

/* eslint-disable jsx-a11y/no-onchange */
Expand All @@ -60,7 +61,7 @@ class PostAuthor extends Component {
<select
id={ selectId }
value={ postAuthor }
onChange={ ( event ) => onUpdateAuthor( event.target.value ) }
onChange={ this.setAuthorId }
className="editor-post-author__select"
>
{ authors.map( ( author ) => (
Expand All @@ -73,7 +74,7 @@ class PostAuthor extends Component {
}
}

export default connect(
const applyConnect = connect(
( state ) => {
return {
postAuthor: getEditedPostAttribute( state, 'author' ),
Expand All @@ -84,4 +85,16 @@ export default connect(
return editPost( { author } );
},
},
)( withInstanceId( PostAuthor ) );
);

const applyWithAPIData = withAPIData( () => {
return {
users: '/wp/v2/users?context=edit&per_page=100',
};
} );

export default flowRight( [
applyConnect,
applyWithAPIData,
withInstanceId,
] )( PostAuthor );
102 changes: 102 additions & 0 deletions editor/sidebar/post-author/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';

/**
* Internal dependencies
*/
import { PostAuthor } from '../';

describe( 'PostAuthor', () => {
const users = {
data: [
{
id: 1,
name: 'admin',
capabilities: {
level_1: true,
},
},
{
id: 2,
name: 'subscriber',
capabilities: {
level_0: true,
},
},
{
id: 3,
name: 'andrew',
capabilities: {
level_1: true,
},
},
],
};

describe( '#getAuthors()', () => {
it( 'returns empty array on unknown users', () => {
const wrapper = shallow( <PostAuthor users={ {} } /> );

const authors = wrapper.instance().getAuthors();

expect( authors ).toEqual( [] );
} );

it( 'filters users to authors', () => {
const wrapper = shallow( <PostAuthor users={ users } /> );

const authors = wrapper.instance().getAuthors();

expect( authors.map( ( author ) => author.id ).sort() ).toEqual( [ 1, 3 ] );
} );
} );

describe( '#render()', () => {
it( 'should not render anything if users unknown', () => {
const wrapper = shallow( <PostAuthor users={ {} } /> );

expect( wrapper.type() ).toBe( null );
} );

it( 'should not render anything if single user', () => {
const wrapper = shallow(
<PostAuthor users={ { data: users.data.slice( 0, 1 ) } } />
);

expect( wrapper.type() ).toBe( null );
} );

it( 'should not render anything if single filtered user', () => {
const wrapper = shallow(
<PostAuthor users={ { data: users.data.slice( 0, 2 ) } } />
);

expect( wrapper.type() ).toBe( null );
} );

it( 'should render select control', () => {
const wrapper = shallow( <PostAuthor users={ users } /> );

expect( wrapper.find( 'select' ).length ).not.toBe( 0 );
} );

it( 'should update author', () => {
const onUpdateAuthor = jest.fn();
const wrapper = shallow(
<PostAuthor
users={ users }
onUpdateAuthor={ onUpdateAuthor } />
);

wrapper.find( 'select' ).simulate( 'change', {
target: {
value: '3',
},
} );

expect( onUpdateAuthor ).toHaveBeenCalledWith( 3 );
} );
} );
} );