-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move embed API call out of block and into data #6678
Changes from 6 commits
1c5090e
a78196b
c9d9bc5
e2f4f0a
14b7e75
49ef894
3db8a04
796bf22
320a2ec
741b2af
5b456c4
35db192
1a451d8
ad831ec
142d263
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,3 +168,36 @@ export const getEntityRecords = createSelector( | |
export function getThemeSupports( state ) { | ||
return state.themeSupports; | ||
} | ||
|
||
/** | ||
* Returns the embed preview for the given URL. | ||
* | ||
* @param {Object} state Data state. | ||
* @param {string} url Embedded URL. | ||
* | ||
* @return {*} Undefined if the preview has not been fetched, otherwise, the preview fetched from the embed preview API. | ||
*/ | ||
export function getEmbedPreview( state, url ) { | ||
return state.embedPreviews[ url ]; | ||
} | ||
|
||
/** | ||
* Determines if the returned preview is an oEmbed link fallback. | ||
* | ||
* WordPress can be configured to return a simple link to a URL if it is not embeddable. | ||
* We need to be able to determine if a URL is embeddable or not, based on what we | ||
* get back from the oEmbed preview API. | ||
* | ||
* @param {Object} state Data state. | ||
* @param {string} url Embedded URL. | ||
* | ||
* @return {booleans} Is the preview for the URL an oEmbed link fallback. | ||
*/ | ||
export function isPreviewEmbedFallback( state, url ) { | ||
const preview = state.embedPreviews[ url ]; | ||
const oEmbedLinkCheck = '<a href="' + url + '">' + url + '</a>'; | ||
if ( ! preview ) { | ||
return false; | ||
} | ||
return preview.html === oEmbedLinkCheck; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems fragile to test the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The embed fallbacks are currently done by a filter in |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,16 @@ | |
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { stringify } from 'querystring'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above :) |
||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getCategories, getEntityRecord, getEntityRecords } from '../resolvers'; | ||
import { receiveTerms, receiveEntityRecords, addEntities } from '../actions'; | ||
import { getCategories, getEntityRecord, getEntityRecords, getEmbedPreview } from '../resolvers'; | ||
import { receiveTerms, receiveEntityRecords, addEntities, receiveEmbedPreview } from '../actions'; | ||
|
||
jest.mock( '@wordpress/api-fetch', () => jest.fn() ); | ||
|
||
|
@@ -105,3 +110,31 @@ describe( 'getEntityRecords', () => { | |
expect( received ).toEqual( receiveEntityRecords( 'root', 'postType', Object.values( POST_TYPES ) ) ); | ||
} ); | ||
} ); | ||
|
||
describe( 'getEmbedPreview', () => { | ||
const SUCCESSFUL_EMBED_RESPONSE = { data: '<p>some html</p>' }; | ||
const UNEMBEDDABLE_RESPONSE = false; | ||
const EMBEDDABLE_URL = 'http://twitter.com/notnownikki'; | ||
const UNEMBEDDABLE_URL = 'http://example.com/'; | ||
|
||
beforeAll( () => { | ||
apiFetch.mockImplementation( ( options ) => { | ||
if ( options.path === `/oembed/1.0/proxy?${ stringify( { url: EMBEDDABLE_URL } ) }` ) { | ||
return Promise.resolve( SUCCESSFUL_EMBED_RESPONSE ); | ||
} | ||
throw 404; | ||
} ); | ||
} ); | ||
|
||
it( 'yields with fetched embed preview', async () => { | ||
const fulfillment = getEmbedPreview( {}, EMBEDDABLE_URL ); | ||
const received = ( await fulfillment.next() ).value; | ||
expect( received ).toEqual( receiveEmbedPreview( EMBEDDABLE_URL, SUCCESSFUL_EMBED_RESPONSE ) ); | ||
} ); | ||
|
||
it( 'yields false if the URL cannot be embedded', async () => { | ||
const fulfillment = getEmbedPreview( {}, UNEMBEDDABLE_URL ); | ||
const received = ( await fulfillment.next() ).value; | ||
expect( received ).toEqual( receiveEmbedPreview( UNEMBEDDABLE_URL, UNEMBEDDABLE_RESPONSE ) ); | ||
} ); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use
addQueryArgs
instead of this. see #8300