Skip to content

Commit

Permalink
chore(history): add api tests (#20157)
Browse files Browse the repository at this point in the history
  • Loading branch information
markkaylor committed Apr 23, 2024
1 parent 7431ba9 commit bdaafbb
Show file tree
Hide file tree
Showing 8 changed files with 495 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,36 @@ export const VersionHeader = ({ headerId }: VersionHeaderProps) => {

const mainFieldValue = version.data[mainField];

const getBackLink = (): To => {
const getNextNavigation = (): To => {
const pluginsQueryParams = stringify({ plugins: query.plugins }, { encode: false });

if (collectionType === COLLECTION_TYPES) {
return {
pathname: `../${collectionType}/${version.contentType}/${version.relatedDocumentId}`,
pathname: `/content-manager/${collectionType}/${version.contentType}/${version.relatedDocumentId}`,
search: pluginsQueryParams,
};
}

return {
pathname: `../${collectionType}/${version.contentType}`,
pathname: `/content-manager/${collectionType}/${version.contentType}`,
search: pluginsQueryParams,
};
};

const handleRestore = async () => {
try {
const response = await restoreVersion({
documentId: version.relatedDocumentId,
collectionType,
params: {
versionId: version.id,
documentId: version.relatedDocumentId,
contentType: version.contentType,
},
body: { contentType: version.contentType },
});

if ('data' in response) {
navigate(`/content-manager/${collectionType}/${slug}/${response.data.data?.documentId}`);
navigate(getNextNavigation());

toggleNotification({
type: 'success',
Expand Down Expand Up @@ -137,7 +138,7 @@ export const VersionHeader = ({ headerId }: VersionHeaderProps) => {
startIcon={<ArrowLeft />}
as={NavLink}
// @ts-expect-error - types are not inferred correctly through the as prop.
to={getBackLink()}
to={getNextNavigation()}
>
{formatMessage({
id: 'global.back',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('VersionHeader', () => {
const backLink = screen.getByRole('link', { name: 'Back' });
expect(backLink).toHaveAttribute(
'href',
'/collection-types/api::kitchensink.kitchensink/pcwmq3rlmp5w0be3cuplhnpr'
'/content-manager/collection-types/api::kitchensink.kitchensink/pcwmq3rlmp5w0be3cuplhnpr'
);
});

Expand Down Expand Up @@ -111,7 +111,7 @@ describe('VersionHeader', () => {
const backLink = screen.getByRole('link', { name: 'Back' });
expect(backLink).toHaveAttribute(
'href',
'/collection-types/api::kitchensink.kitchensink/pcwmq3rlmp5w0be3cuplhnpr?plugins[i18n][locale]=en'
'/content-manager/collection-types/api::kitchensink.kitchensink/pcwmq3rlmp5w0be3cuplhnpr?plugins[i18n][locale]=en'
);
});

Expand Down Expand Up @@ -158,7 +158,10 @@ describe('VersionHeader', () => {
expect(await screen.findByText('Test Title (homepage)')).toBeInTheDocument();

const backLink = screen.getByRole('link', { name: 'Back' });
expect(backLink).toHaveAttribute('href', '/single-types/api::homepage.homepage');
expect(backLink).toHaveAttribute(
'href',
'/content-manager/single-types/api::homepage.homepage'
);
});

it('should display the correct title and subtitle for a localized entry', async () => {
Expand All @@ -185,7 +188,7 @@ describe('VersionHeader', () => {
const backLink = screen.getByRole('link', { name: 'Back' });
expect(backLink).toHaveAttribute(
'href',
'/single-types/api::homepage.homepage?plugins[i18n][locale]=en'
'/content-manager/single-types/api::homepage.homepage?plugins[i18n][locale]=en'
);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { Data } from '@strapi/types';

import {
GetHistoryVersions,
RestoreHistoryVersion,
} from '../../../../shared/contracts/history-versions';
import { COLLECTION_TYPES } from '../../constants/collections';
import { contentManagerApi } from '../../services/api';

interface RestoreVersion extends RestoreHistoryVersion.Request {
documentId: Data.ID;
collectionType?: string;
}

const historyVersionsApi = contentManagerApi.injectEndpoints({
endpoints: (builder) => ({
getHistoryVersions: builder.query<
Expand All @@ -21,23 +29,27 @@ const historyVersionsApi = contentManagerApi.injectEndpoints({
},
providesTags: ['HistoryVersion'],
}),
restoreVersion: builder.mutation<RestoreHistoryVersion.Response, RestoreHistoryVersion.Request>(
{
query({ params, body }) {
return {
url: `/content-manager/history-versions/${params.versionId}/restore`,
method: 'PUT',
data: body,
};
},
invalidatesTags: (_res, _error, { params }) => {
return [
'HistoryVersion',
{ type: 'Document', id: `${params.contentType}_${params.documentId}` },
];
},
}
),
restoreVersion: builder.mutation<RestoreHistoryVersion.Response, RestoreVersion>({
query({ params, body }) {
return {
url: `/content-manager/history-versions/${params.versionId}/restore`,
method: 'PUT',
data: body,
};
},
invalidatesTags: (_res, _error, { documentId, collectionType, params }) => {
return [
'HistoryVersion',
{
type: 'Document',
id:
collectionType === COLLECTION_TYPES
? `${params.contentType}_${documentId}`
: params.contentType,
},
];
},
}),
}),
});

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ const createHistoryVersionController = ({ strapi }: { strapi: Core.Strapi }) =>
return {
async findMany(ctx) {
const contentTypeUid = ctx.query.contentType as UID.ContentType;
const isSingleType = strapi.getModel(contentTypeUid).kind === 'singleType';
const isSingleType = strapi.getModel(contentTypeUid)?.kind === 'singleType';

if (isSingleType && !contentTypeUid) {
throw new errors.ForbiddenError('contentType is required');
}

if (!contentTypeUid && !ctx.query.documentId) {
if (!isSingleType && (!contentTypeUid || !ctx.query.documentId)) {
throw new errors.ForbiddenError('contentType and documentId are required');
}

Expand Down

0 comments on commit bdaafbb

Please sign in to comment.