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 [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 = {

View File

@ -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(