Skip to content

Commit

Permalink
Posts: Add getSitePosts selector to Redux posts state
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Feb 29, 2016
1 parent ed1dc6e commit b91d791
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions client/state/posts/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import range from 'lodash/range';
import createSelector from 'lib/create-selector';
import filter from 'lodash/filter';

/**
* Internal dependencies
Expand All @@ -25,6 +26,18 @@ export function getPost( state, globalId ) {
return state.posts.items[ globalId ];
}

/**
* Returns an array of post objects by site ID.
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @return {Array} Site posts
*/
export const getSitePosts = createSelector(
( state, siteId ) => filter( state.posts.items, { site_ID: siteId } ),
( state ) => [ state.posts.items ]
);

/**
* Returns true if the specified posts query is being tracked for the site, or
* false otherwise.
Expand Down
20 changes: 20 additions & 0 deletions client/state/posts/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { expect } from 'chai';
*/
import {
getPost,
getSitePosts,
isTrackingSitePostsQuery,
getSitePostsForQuery,
isRequestingSitePostsForQuery,
Expand All @@ -33,6 +34,25 @@ describe( 'selectors', () => {
} );
} );

describe( '#getSitePosts()', () => {
it( 'should return an array of post objects for the site', () => {
const state = {
posts: {
items: {
'3d097cb7c5473c169bba0eb8e3c6cb64': { ID: 841, site_ID: 2916284, global_ID: '3d097cb7c5473c169bba0eb8e3c6cb64', title: 'Hello World' },
'6c831c187ffef321eb43a67761a525a3': { ID: 413, site_ID: 2916284, global_ID: '6c831c187ffef321eb43a67761a525a3', title: 'Ribs & Chicken' },
'0fcb4eb16f493c19b627438fdc18d57c': { ID: 120, site_ID: 77203074, global_ID: 'f0cb4eb16f493c19b627438fdc18d57c', title: 'Steak & Eggs' }
}
}
};

expect( getSitePosts( state, 2916284 ) ).to.have.members( [
state.posts.items[ '3d097cb7c5473c169bba0eb8e3c6cb64' ],
state.posts.items[ '6c831c187ffef321eb43a67761a525a3' ]
] );
} );
} );

describe( '#isTrackingSitePostsQuery()', () => {
it( 'should return false if the site has not been queried', () => {
const isTracking = isTrackingSitePostsQuery( {
Expand Down

0 comments on commit b91d791

Please sign in to comment.