Skip to content

Commit

Permalink
Core Data: Add getPostMedia
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed May 1, 2018
1 parent 7ec3f8a commit 57be330
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
11 changes: 11 additions & 0 deletions core-data/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ export async function* getMedia( state, id ) {
yield receiveMedia( media );
}

/**
* Requests all media elements attached to a post from the REST API.
*
* @param {Object} state State tree.
* @param {number} postId Post id.
*/
export async function* getPostMedia( state, postId ) {
const media = await apiRequest( { path: `/wp/v2/media?parent=${ postId }` } );
yield receiveMedia( media );
}

/**
* Requests a post type element from the REST API.
*
Expand Down
17 changes: 17 additions & 0 deletions core-data/selectors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { filter } from 'lodash';

/**
* Returns all the available terms for the given taxonomy.
*
Expand Down Expand Up @@ -58,6 +63,18 @@ export function getMedia( state, id ) {
return state.media[ id ];
}

/**
* Returns media objects attached to a post, given a post ID.
*
* @param {Object} state Data state.
* @param {number} postId Post id.
*
* @return {Array} Media list.
*/
export function getPostMedia( state, postId ) {
return filter( state.media, { post: postId } );
}

/**
* Returns the Post Type object by slug.
*
Expand Down
20 changes: 19 additions & 1 deletion core-data/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import apiRequest from '@wordpress/api-request';
/**
* Internal dependencies
*/
import { getCategories, getMedia, getPostType } from '../resolvers';
import { getCategories, getMedia, getPostMedia, getPostType } from '../resolvers';
import { setRequested, receiveTerms, receiveMedia, receivePostTypes } from '../actions';

jest.mock( '@wordpress/api-request' );
Expand Down Expand Up @@ -49,6 +49,24 @@ describe( 'getMedia', () => {
} );
} );

describe( 'getPostMedia', () => {
const MEDIA = [ { id: 10 }, { id: 20 } ];

beforeAll( () => {
apiRequest.mockImplementation( ( options ) => {
if ( options.path === '/wp/v2/media?parent=1' ) {
return Promise.resolve( MEDIA );
}
} );
} );

it( 'yields with requested media', async () => {
const fulfillment = getPostMedia( {}, 1 );
const received = ( await fulfillment.next() ).value;
expect( received ).toEqual( receiveMedia( MEDIA ) );
} );
} );

describe( 'getPostType', () => {
const POST_TYPE = { slug: 'post' };

Expand Down
26 changes: 25 additions & 1 deletion core-data/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import deepFreeze from 'deep-freeze';
/**
* Internal dependencies
*/
import { getTerms, isRequestingTerms, getMedia, getPostType } from '../selectors';
import { getTerms, isRequestingTerms, getMedia, getPostMedia, getPostType } from '../selectors';

describe( 'getTerms()', () => {
it( 'returns value of terms by taxonomy', () => {
Expand Down Expand Up @@ -75,6 +75,30 @@ describe( 'getMedia', () => {
} );
} );

describe( 'getPostMedia', () => {
it( 'should return undefined for unknown media', () => {
const state = deepFreeze( {
media: {},
} );
expect( getPostMedia( state, 1 ) ).toEqual( [] );
} );

it( 'should return a media element by id', () => {
const state = deepFreeze( {
media: {
1: { id: 1, post: 42 },
2: { id: 2, post: 42 },
3: { id: 3, post: 84 },
4: { id: 4 },
},
} );
expect( getPostMedia( state, 42 ) ).toEqual( [
{ id: 1, post: 42 },
{ id: 2, post: 42 },
] );
} );
} );

describe( 'getPostType', () => {
it( 'should return undefined for unknown post type', () => {
const state = deepFreeze( {
Expand Down

0 comments on commit 57be330

Please sign in to comment.