fix: RelationInput tests

This commit is contained in:
Josh 2022-10-18 12:06:57 +01:00 committed by Gustav Hansen
parent 6383fc85ce
commit cf23e39892
2 changed files with 63 additions and 57 deletions

View File

@ -80,9 +80,7 @@ const RelationInput = ({
const outerListRef = useRef(); const outerListRef = useRef();
const [overflow, setOverflow] = useState(''); const [overflow, setOverflow] = useState('');
const { const { data } = searchResults;
data: { pages },
} = searchResults;
const relations = paginatedRelations.data; const relations = paginatedRelations.data;
const totalNumberOfRelations = relations.length ?? 0; const totalNumberOfRelations = relations.length ?? 0;
@ -102,12 +100,12 @@ const RelationInput = ({
const options = useMemo( const options = useMemo(
() => () =>
pages.flat().map((result) => ({ data.flat().map((result) => ({
...result, ...result,
value: result.id, value: result.id,
label: result.mainField, label: result.mainField,
})), })),
[pages] [data]
); );
useEffect(() => { useEffect(() => {
@ -362,17 +360,14 @@ const ReactQueryRelationResult = PropTypes.shape({
}); });
const ReactQuerySearchResult = PropTypes.shape({ const ReactQuerySearchResult = PropTypes.shape({
data: PropTypes.shape({ data: PropTypes.arrayOf(
pages: PropTypes.arrayOf(
PropTypes.arrayOf(
PropTypes.shape({ PropTypes.shape({
id: PropTypes.number.isRequired, id: PropTypes.number.isRequired,
href: PropTypes.string,
mainField: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), mainField: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
publicationState: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), publicationState: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
}) })
)
), ),
}),
hasNextPage: PropTypes.bool, hasNextPage: PropTypes.bool,
isLoading: PropTypes.bool.isRequired, isLoading: PropTypes.bool.isRequired,
isSuccess: PropTypes.bool.isRequired, isSuccess: PropTypes.bool.isRequired,
@ -386,8 +381,8 @@ RelationInput.defaultProps = {
labelLoadMore: null, labelLoadMore: null,
onSearchClose: undefined, onSearchClose: undefined,
required: false, required: false,
relations: [], relations: { data: [] },
searchResults: [], searchResults: { data: [] },
}; };
RelationInput.propTypes = { RelationInput.propTypes = {

View File

@ -7,9 +7,7 @@ import { ThemeProvider, lightTheme } from '@strapi/design-system';
import { RelationInput } from '../index'; import { RelationInput } from '../index';
const FIXTURES_RELATIONS = { const FIXTURES_RELATIONS = {
data: { data: [
pages: [
[
{ {
id: 1, id: 1,
href: '/', href: '/',
@ -29,25 +27,20 @@ const FIXTURES_RELATIONS = {
publicationState: false, publicationState: false,
}, },
], ],
],
},
isLoading: false, isLoading: false,
isSuccess: true, isSuccess: true,
hasNextPage: true, hasNextPage: true,
isFetchingNextPage: false,
}; };
const FIXTURES_SEARCH = { const FIXTURES_SEARCH = {
data: { data: [
pages: [
[
{ {
id: 4, id: 4,
mainField: 'Relation 4', mainField: 'Relation 4',
publicationState: 'draft', publicationState: 'draft',
}, },
], ],
],
},
isLoading: false, isLoading: false,
isSuccess: true, isSuccess: true,
}; };
@ -173,7 +166,7 @@ describe('Content-Manager || RelationInput', () => {
describe('States', () => { describe('States', () => {
test('should display search loading state', () => { 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)); 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', () => { test('should display load more button loading if loading is true', () => {
setup({ 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( 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', () => { test('should not display load more button loading if there is no next page', () => {
setup({ 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(); 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', () => { test('should display load more button loading if there is no next page but loading is true', () => {
setup({ 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( expect(screen.getByRole('button', { name: /load more/i })).toHaveAttribute(