From cf23e3989222fac664e4716d44f0e662fb22452c Mon Sep 17 00:00:00 2001 From: Josh <37798644+joshuaellis@users.noreply.github.com> Date: Tue, 18 Oct 2022 12:06:57 +0100 Subject: [PATCH] fix: RelationInput tests --- .../components/RelationInput/RelationInput.js | 31 +++---- .../RelationInput/tests/RelationInput.test.js | 89 +++++++++++-------- 2 files changed, 63 insertions(+), 57 deletions(-) diff --git a/packages/core/admin/admin/src/content-manager/components/RelationInput/RelationInput.js b/packages/core/admin/admin/src/content-manager/components/RelationInput/RelationInput.js index c2e81fe366..4dbd8f31d8 100644 --- a/packages/core/admin/admin/src/content-manager/components/RelationInput/RelationInput.js +++ b/packages/core/admin/admin/src/content-manager/components/RelationInput/RelationInput.js @@ -80,9 +80,7 @@ const RelationInput = ({ const outerListRef = useRef(); const [overflow, setOverflow] = useState(''); - const { - data: { pages }, - } = searchResults; + const { data } = searchResults; const relations = paginatedRelations.data; const totalNumberOfRelations = relations.length ?? 0; @@ -102,12 +100,12 @@ const RelationInput = ({ const options = useMemo( () => - pages.flat().map((result) => ({ + data.flat().map((result) => ({ ...result, value: result.id, label: result.mainField, })), - [pages] + [data] ); useEffect(() => { @@ -362,17 +360,14 @@ const ReactQueryRelationResult = PropTypes.shape({ }); const ReactQuerySearchResult = PropTypes.shape({ - data: PropTypes.shape({ - pages: PropTypes.arrayOf( - PropTypes.arrayOf( - PropTypes.shape({ - id: PropTypes.number.isRequired, - mainField: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), - publicationState: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), - }) - ) - ), - }), + data: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number.isRequired, + href: PropTypes.string, + mainField: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), + publicationState: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), + }) + ), hasNextPage: PropTypes.bool, isLoading: PropTypes.bool.isRequired, isSuccess: PropTypes.bool.isRequired, @@ -386,8 +381,8 @@ RelationInput.defaultProps = { labelLoadMore: null, onSearchClose: undefined, required: false, - relations: [], - searchResults: [], + relations: { data: [] }, + searchResults: { data: [] }, }; RelationInput.propTypes = { diff --git a/packages/core/admin/admin/src/content-manager/components/RelationInput/tests/RelationInput.test.js b/packages/core/admin/admin/src/content-manager/components/RelationInput/tests/RelationInput.test.js index 7745c67862..75ff678dbd 100644 --- a/packages/core/admin/admin/src/content-manager/components/RelationInput/tests/RelationInput.test.js +++ b/packages/core/admin/admin/src/content-manager/components/RelationInput/tests/RelationInput.test.js @@ -7,47 +7,40 @@ import { ThemeProvider, lightTheme } from '@strapi/design-system'; import { RelationInput } from '../index'; const FIXTURES_RELATIONS = { - data: { - pages: [ - [ - { - id: 1, - href: '/', - mainField: 'Relation 1', - publicationState: 'draft', - }, - { - id: 2, - href: '', - mainField: 'Relation 2', - publicationState: 'published', - }, - { - id: 3, - href: '', - mainField: 'Relation 3', - publicationState: false, - }, - ], - ], - }, + data: [ + { + id: 1, + href: '/', + mainField: 'Relation 1', + publicationState: 'draft', + }, + { + id: 2, + href: '', + mainField: 'Relation 2', + publicationState: 'published', + }, + { + id: 3, + href: '', + mainField: 'Relation 3', + publicationState: false, + }, + ], isLoading: false, isSuccess: true, hasNextPage: true, + isFetchingNextPage: false, }; const FIXTURES_SEARCH = { - data: { - pages: [ - [ - { - id: 4, - mainField: 'Relation 4', - publicationState: 'draft', - }, - ], - ], - }, + data: [ + { + id: 4, + mainField: 'Relation 4', + publicationState: 'draft', + }, + ], isLoading: false, isSuccess: true, }; @@ -173,7 +166,7 @@ describe('Content-Manager || RelationInput', () => { describe('States', () => { test('should display search loading state', () => { - setup({ searchResults: { data: { pages: [] }, isLoading: true, isSuccess: true } }); + setup({ searchResults: { data: [], isLoading: true, isSuccess: true } }); fireEvent.mouseDown(screen.getByText(/select\.\.\./i)); @@ -182,7 +175,13 @@ describe('Content-Manager || RelationInput', () => { test('should display load more button loading if loading is true', () => { setup({ - relations: { data: { pages: [] }, isLoading: true, isSuccess: true, hasNextPage: true }, + relations: { + data: [], + isLoading: true, + isSuccess: true, + hasNextPage: true, + isFetchingNextPage: false, + }, }); expect(screen.getByRole('button', { name: /load more/i })).toHaveAttribute( @@ -193,7 +192,13 @@ describe('Content-Manager || RelationInput', () => { test('should not display load more button loading if there is no next page', () => { setup({ - relations: { data: { pages: [] }, isLoading: false, isSuccess: true, hasNextPage: false }, + relations: { + data: [], + isLoading: false, + isSuccess: true, + hasNextPage: false, + isFetchingNextPage: false, + }, }); expect(screen.queryByText('Load more')).not.toBeInTheDocument(); @@ -201,7 +206,13 @@ describe('Content-Manager || RelationInput', () => { test('should display load more button loading if there is no next page but loading is true', () => { setup({ - relations: { data: { pages: [] }, isLoading: true, isSuccess: true, hasNextPage: false }, + relations: { + data: [], + isLoading: true, + isSuccess: true, + hasNextPage: false, + isFetchingNextPage: false, + }, }); expect(screen.getByRole('button', { name: /load more/i })).toHaveAttribute(