From bebff9dba924aa612bcb2741b7c32c7d0310760f Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Mon, 3 Apr 2023 12:36:26 -0400 Subject: [PATCH 1/2] fix: Avoid empty Gallery block error Setting a default function parameter avoids an error when `innerBlockImages` is `undefined`. This default function parameter may be unnecessary and masking a underlying issue. --- .../src/gallery/test/use-get-media.native.js | 24 +++++++++++++++++++ .../src/gallery/use-get-media.native.js | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 packages/block-library/src/gallery/test/use-get-media.native.js diff --git a/packages/block-library/src/gallery/test/use-get-media.native.js b/packages/block-library/src/gallery/test/use-get-media.native.js new file mode 100644 index 0000000000000..67c7a8d0b5df2 --- /dev/null +++ b/packages/block-library/src/gallery/test/use-get-media.native.js @@ -0,0 +1,24 @@ +/** + * External dependencies + */ +import { render } from 'test/helpers'; + +/** + * Internal dependencies + */ +import useGetMedia from '../use-get-media'; + +describe( 'useGetMedia', () => { + // TODO: Explore why a Gallery block would ever be without inner blocks. + // This test and the associated default function parameter might be unnecessary. + it( 'should not error when receiving zero images', () => { + // Arrange + const TestSubject = () => { + useGetMedia( undefined ); + return null; + }; + + // Assert + expect( () => render( ) ).not.toThrow(); + } ); +} ); diff --git a/packages/block-library/src/gallery/use-get-media.native.js b/packages/block-library/src/gallery/use-get-media.native.js index b03defbdb49d5..6adb9d0c63ffd 100644 --- a/packages/block-library/src/gallery/use-get-media.native.js +++ b/packages/block-library/src/gallery/use-get-media.native.js @@ -14,7 +14,7 @@ const EMPTY_IMAGE_MEDIA = []; * * @return {Array} An array of media info options for each gallery image. */ -export default function useGetMedia( innerBlockImages ) { +export default function useGetMedia( innerBlockImages = [] ) { return useSelect( ( select ) => { const imagesUploading = innerBlockImages.some( From e00b1948250c7d61bdf9652be59ab98c5b01eb21 Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Fri, 7 Apr 2023 10:47:38 -0400 Subject: [PATCH 2/2] docs: Add change log entry --- packages/react-native-editor/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react-native-editor/CHANGELOG.md b/packages/react-native-editor/CHANGELOG.md index 0f05498769e26..c8c58752dd413 100644 --- a/packages/react-native-editor/CHANGELOG.md +++ b/packages/react-native-editor/CHANGELOG.md @@ -10,6 +10,7 @@ For each user feature we should also add a importance categorization label to i --> ## Unreleased +- [*] Avoid empty Gallery block error [#49557] ## 1.92.0 * No User facing changes *