Skip to content

Commit

Permalink
test(e2e): ensure custom publish action can patch document before pub…
Browse files Browse the repository at this point in the history
…lication

Co-authored-by: Bjørge Næss <bjoerge@gmail.com>
  • Loading branch information
juice49 and bjoerge committed Jun 18, 2024
1 parent fa459a4 commit 17d0bbf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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?.()
},
}
}
}
5 changes: 4 additions & 1 deletion dev/test-studio/documentActions/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
})
}
Expand Down
1 change: 1 addition & 0 deletions dev/test-studio/schema/debug/documentActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default {
name: 'title',
title: 'Title',
},
{type: 'datetime', name: 'publishedAt', title: 'Published at'},
],
}
27 changes: 27 additions & 0 deletions test/e2e/tests/document-actions/publish.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/.*/)
})

0 comments on commit 17d0bbf

Please sign in to comment.