-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RNMobile] Add API fetch test helper (#50391)
* Add setup api fetch test helper * Use setup api fetch helper in Image block test cases
- Loading branch information
Showing
4 changed files
with
93 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
|
||
/** | ||
* Sets up the `apiFetch` library for testing by mocking request responses. | ||
* | ||
* Example: | ||
* | ||
* const responses = [ | ||
* { | ||
* request: { | ||
* path: `/wp/v2/media/1?context=edit`, | ||
* }, | ||
* response: { | ||
* source_url: 'https://image-1.jpg', | ||
* id: 1, | ||
* }, | ||
* }, | ||
* { | ||
* request: { | ||
* path: `/wp/v2/media/2?context=edit`, | ||
* }, | ||
* response: { | ||
* source_url: 'https://image-2.jpg', | ||
* id: 2, | ||
* }, | ||
* }, | ||
* ]; | ||
* setupApiFetch( responses ); | ||
* ... | ||
* expect( apiFetch ).toHaveBeenCalledWith( responses[1].request ); | ||
* | ||
* @param {object[]} responses Array with the potential responses to return upon requests. | ||
* | ||
*/ | ||
export function setupApiFetch( responses ) { | ||
apiFetch.mockImplementation( async ( options ) => { | ||
const matchedResponse = responses.find( | ||
( { request: { path, method } } ) => { | ||
return ( | ||
path === options.path && | ||
( ! options.method || method === options.method ) | ||
); | ||
} | ||
); | ||
return matchedResponse?.response; | ||
} ); | ||
} |
f4730d7
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.
Flaky tests detected in f4730d7.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4915159907
📝 Reported issues:
specs/editor/various/links.test.js
/test/e2e/specs/site-editor/user-global-styles-revisions.spec.js