From 8379426948bd9ddf97504530ccc763829d9fe7e5 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Fri, 5 May 2023 18:17:08 +0200 Subject: [PATCH] Use setup api fetch helper in Image block test cases --- .../src/image/test/edit.native.js | 54 +++++++++++++------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/packages/block-library/src/image/test/edit.native.js b/packages/block-library/src/image/test/edit.native.js index 73f4ec22a554d..1f1bdd6ae4eda 100644 --- a/packages/block-library/src/image/test/edit.native.js +++ b/packages/block-library/src/image/test/edit.native.js @@ -8,6 +8,7 @@ import { getEditorHtml, render, waitFor, + setupApiFetch, } from 'test/helpers'; import { Image } from 'react-native'; import Clipboard from '@react-native-clipboard/clipboard'; @@ -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'; /** @@ -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 ); @@ -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 ); @@ -78,8 +100,8 @@ describe( 'Image Block', () => {
Mountain
`; 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 ); @@ -105,8 +127,8 @@ describe( 'Image Block', () => {
Mountain
`; 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 ); @@ -132,8 +154,8 @@ describe( 'Image Block', () => {
Mountain
`; 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 ); @@ -169,8 +191,8 @@ describe( 'Image Block', () => {
Mountain
`; 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 ); @@ -211,8 +233,8 @@ describe( 'Image Block', () => {
Mountain
`; 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 ); @@ -236,8 +258,8 @@ describe( 'Image Block', () => {
Mountain
`; 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 ); @@ -267,8 +289,8 @@ describe( 'Image Block', () => { `; 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 );