From 17d0bbf7bbb8e9a83163661b2d790678967611bc Mon Sep 17 00:00:00 2001 From: Ash Date: Tue, 18 Jun 2024 10:07:38 +0100 Subject: [PATCH] test(e2e): ensure custom publish action can patch document before publication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bjørge Næss --- .../actions/createCustomPublishAction.ts | 19 +++++++++++++ dev/test-studio/documentActions/index.ts | 5 +++- .../schema/debug/documentActions.js | 1 + .../tests/document-actions/publish.spec.ts | 27 +++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 dev/test-studio/documentActions/actions/createCustomPublishAction.ts diff --git a/dev/test-studio/documentActions/actions/createCustomPublishAction.ts b/dev/test-studio/documentActions/actions/createCustomPublishAction.ts new file mode 100644 index 00000000000..52ab480f3e6 --- /dev/null +++ b/dev/test-studio/documentActions/actions/createCustomPublishAction.ts @@ -0,0 +1,19 @@ +/* eslint-disable react-hooks/rules-of-hooks */ +import {type DocumentActionComponent, type DocumentActionProps, useDocumentOperation} from 'sanity' + +export function createCustomPublishAction(originalAction: DocumentActionComponent) { + return function CustomPublishAction(props: DocumentActionProps) { + const defaultPublishAction = originalAction(props) + const documentOperations = useDocumentOperation(props.id, props.type) + + return { + ...defaultPublishAction, + label: 'Custom publish that sets publishedAt to now', + onHandle: () => { + documentOperations.patch.execute([{set: {publishedAt: new Date().toISOString()}}]) + + defaultPublishAction?.onHandle?.() + }, + } + } +} diff --git a/dev/test-studio/documentActions/index.ts b/dev/test-studio/documentActions/index.ts index 8c2baad981b..9e6ec576dfb 100644 --- a/dev/test-studio/documentActions/index.ts +++ b/dev/test-studio/documentActions/index.ts @@ -1,5 +1,6 @@ import {type DocumentActionsResolver} from 'sanity' +import {createCustomPublishAction} from './actions/createCustomPublishAction' import {TestConfirmDialogAction} from './actions/TestConfirmDialogAction' import {TestCustomComponentAction} from './actions/TestCustomComponentAction' import {TestCustomRestoreAction} from './actions/TestCustomRestoreAction' @@ -18,7 +19,9 @@ export const resolveDocumentActions: DocumentActionsResolver = (prev, {schemaTyp if (action.action === 'restore') { return TestCustomRestoreAction(action) } - + if (action.action === 'publish') { + return createCustomPublishAction(action) + } return action }) } diff --git a/dev/test-studio/schema/debug/documentActions.js b/dev/test-studio/schema/debug/documentActions.js index 26e9311f04c..eb5a3f80bd3 100644 --- a/dev/test-studio/schema/debug/documentActions.js +++ b/dev/test-studio/schema/debug/documentActions.js @@ -8,5 +8,6 @@ export default { name: 'title', title: 'Title', }, + {type: 'datetime', name: 'publishedAt', title: 'Published at'}, ], } diff --git a/test/e2e/tests/document-actions/publish.spec.ts b/test/e2e/tests/document-actions/publish.spec.ts index 535f2381b57..bfb8ed5c36c 100644 --- a/test/e2e/tests/document-actions/publish.spec.ts +++ b/test/e2e/tests/document-actions/publish.spec.ts @@ -20,3 +20,30 @@ test(`document panel displays correct title for published document`, async ({ // Ensure the correct title is displayed after publishing. expect(page.getByTestId('document-panel-document-title')).toHaveText(title) }) + +test(`custom publish action can patch document before publication`, async ({ + page, + createDraftDocument, +}) => { + const title = 'Test Title' + + const publishKeypress = () => page.locator('body').press('Control+Alt+p') + const documentStatus = page.getByTestId('pane-footer-document-status') + const titleInput = page.getByTestId('field-title').getByTestId('string-input') + const publishedAtInput = page.getByTestId('field-publishedAt').getByTestId('date-input') + + await createDraftDocument('/test/content/input-debug;documentActionsTest') + await titleInput.fill(title) + + // Wait for the document to be published. + // + // Note: This is invoked using the publish keyboard shortcut, because the publish document action + // has been overridden for the `documentActionsTest` type, and is not visible without opening the + // document actions menu. + await page.waitForTimeout(1_000) + await publishKeypress() + await expect(documentStatus).toContainText('Published just now') + + // Ensure the custom publish action succeeded in setting the `publishedAt` field. + await expect(publishedAtInput).toHaveValue(/.*/) +})