Skip to content

Commit

Permalink
Use setup api fetch helper in Image block test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
fluiddot committed May 5, 2023
1 parent 0c5e0bf commit 8379426
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions packages/block-library/src/image/test/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getEditorHtml,
render,
waitFor,
setupApiFetch,
} from 'test/helpers';
import { Image } from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard';
Expand All @@ -22,9 +23,10 @@ import {
sendMediaUpload,
subscribeMediaUpload,
} from '@wordpress/react-native-bridge';
import { select } from '@wordpress/data';
import { select, dispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
import apiFetch from '@wordpress/api-fetch';
import '@wordpress/jest-console';

/**
Expand All @@ -45,7 +47,15 @@ function mockGetMedia( media ) {
jest.spyOn( select( coreStore ), 'getMedia' ).mockReturnValue( media );
}

const apiFetchPromise = Promise.resolve( {} );
const FETCH_MEDIA = {
request: {
path: `/wp/v2/media/1?context=edit`,
},
response: {
source_url: 'https://cldup.com/cXyG__fTLN.jpg',
id: 1,
},
};

const clipboardPromise = Promise.resolve( '' );
Clipboard.getString.mockImplementation( () => clipboardPromise );
Expand All @@ -58,6 +68,18 @@ beforeAll( () => {
getSizeSpy.mockImplementation( ( _url, callback ) => callback( 300, 200 ) );
} );

beforeEach( () => {
// Mock media fetch requests
setupApiFetch( [ FETCH_MEDIA ] );

// Invalidate `getMedia` resolutions to allow requesting to the API the same media id
dispatch( coreStore ).invalidateResolutionForStoreSelector( 'getMedia' );
} );

afterEach( () => {
apiFetch.mockReset();
} );

afterAll( () => {
getBlockTypes().forEach( ( { name } ) => {
unregisterBlockType( name );
Expand All @@ -78,8 +100,8 @@ describe( 'Image Block', () => {
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// We must await the image fetch via `getMedia`
await act( () => apiFetchPromise );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );

const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
Expand All @@ -105,8 +127,8 @@ describe( 'Image Block', () => {
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// We must await the image fetch via `getMedia`
await act( () => apiFetchPromise );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );

const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
Expand All @@ -132,8 +154,8 @@ describe( 'Image Block', () => {
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// We must await the image fetch via `getMedia`
await act( () => apiFetchPromise );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );

const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
Expand Down Expand Up @@ -169,8 +191,8 @@ describe( 'Image Block', () => {
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// We must await the image fetch via `getMedia`
await act( () => apiFetchPromise );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );

const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
Expand Down Expand Up @@ -211,8 +233,8 @@ describe( 'Image Block', () => {
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// We must await the image fetch via `getMedia`
await act( () => apiFetchPromise );
// Check that image is not fetched via `getMedia` due to the presence of query parameters in the URL.
expect( apiFetch ).not.toHaveBeenCalledWith( FETCH_MEDIA.request );

const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
Expand All @@ -236,8 +258,8 @@ describe( 'Image Block', () => {
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// We must await the image fetch via `getMedia`
await act( () => apiFetchPromise );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );

const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
Expand Down Expand Up @@ -267,8 +289,8 @@ describe( 'Image Block', () => {
</figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// We must await the image fetch via `getMedia`
await act( () => apiFetchPromise );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );

const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
Expand Down

0 comments on commit 8379426

Please sign in to comment.