Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(workflows): cover the draft publish, discard and resume listeners (#128886)

Michael Reichenbach861e8f1fe685
diff --git a/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts b/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
index d4c57d6aaced..a9f0e9c163e9 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
@@ -3,7 +3,10 @@ import posthog from 'posthog-js'
import { lemonToast } from '@posthog/lemon-ui'
+import { LemonDialog, LemonDialogProps } from 'lib/lemon-ui/LemonDialog'
+
import { useMocks } from '~/mocks/jest'
+import { MockResolverInfo, MockSignature } from '~/mocks/utils'
import { initKeaTests } from '~/test/init'
import { resourceEditedLogic } from 'products/notifications/frontend/resourceEditedLogic'
@@ -894,6 +897,208 @@ describe('workflowLogic auto-save', () => {
await expectLogic(logic).toDispatchActions(['loadWorkflowSuccess'])
})
+ describe('publish, discard and resume email sending', () => {
+ const PUBLISH_URL = '/api/environments/:team_id/hog_flows/:id/publish/'
+ const preview = {
+ published: false,
+ in_flight_runs: 2,
+ draft_updated_at: staged.draft_updated_at,
+ confirm_token: 'preview-token',
+ impact: null,
+ }
+ let dialogs: LemonDialogProps[]
+ let reloads: number
+ let publishBodies: Record<string, any>[]
+
+ const serverMocks = (post: Record<string, MockSignature>): Parameters<typeof useMocks>[0] => ({
+ get: {
+ '/api/environments/:team_id/hog_flows/:id/': () => {
+ reloads += 1
+ return [200, staged]
+ },
+ },
+ post,
+ })
+ const recordPublish =
+ (respond: (body: Record<string, any>) => unknown): MockSignature =>
+ async ({ request }: MockResolverInfo) => {
+ const body = (await request.json()) as Record<string, any>
+ publishBodies.push(body)
+ return respond(body)
+ }
+ const clickConfirm = (title: string): void => {
+ const onConfirm = dialogs.find((d) => d.title === title)?.primaryButton?.onClick
+ if (!onConfirm) {
+ throw new Error(`No open dialog titled "${title}"`)
+ }
+ ;(onConfirm as () => void)()
+ }
+
+ beforeEach(() => {
+ dialogs = []
+ reloads = 0
+ publishBodies = []
+ jest.spyOn(LemonDialog, 'open').mockImplementation((props) => {
+ dialogs.push(props)
+ })
+ })
+
+ afterEach(() => {
+ jest.restoreAllMocks()
+ })
+
+ it('publishes with the token its impact preview minted, then reloads the workflow', async () => {
+ useMocks(
+ serverMocks({
+ [PUBLISH_URL]: recordPublish((body) => [200, { ...preview, published: !!body.confirm }]),
+ })
+ )
+ const successToast = jest.spyOn(lemonToast, 'success')
+
+ logic.actions.publishDraft()
+ expect(logic.values.draftActionPending).toBe('publish')
+ await expectLogic(logic).toFinishAllListeners()
+ expect(publishBodies).toEqual([{ confirm: false }])
+ expect(logic.values.draftActionPending).toBeNull()
+
+ clickConfirm('Publish staged changes?')
+ await expectLogic(logic).toDispatchActions(['loadWorkflowSuccess']).toFinishAllListeners()
+
+ expect(publishBodies).toEqual([{ confirm: false }, { confirm: true, confirm_token: 'preview-token' }])
+ expect(successToast).toHaveBeenCalledWith('Changes published')
+ expect(reloads).toBe(1)
+ expect(logic.values.draftActionPending).toBeNull()
+ })
+
+ it('shows no publish dialog and frees the buttons when the preview fails', async () => {
+ useMocks(serverMocks({ [PUBLISH_URL]: recordPublish(() => [500, { detail: 'boom' }]) }))
+ const errorToast = jest.spyOn(lemonToast, 'error')
+
+ await expectLogic(logic, () => logic.actions.publishDraft()).toFinishAllListeners()
+
+ expect(dialogs).toEqual([])
+ expect(errorToast).toHaveBeenCalledWith('Could not load the publish preview. Please try again.')
+ expect(publishBodies).toEqual([{ confirm: false }])
+ expect(logic.values.draftActionPending).toBeNull()
+ expect(logic.values.discardDisabledReason).toBeUndefined()
+ })
+
+ it('reloads to show the current draft when the confirmed publish is refused', async () => {
+ // A refused confirm is usually a stale token: the draft moved after the preview.
+ useMocks(serverMocks({ [PUBLISH_URL]: recordPublish(() => [409, { detail: 'stale' }]) }))
+ const errorToast = jest.spyOn(lemonToast, 'error')
+
+ logic.actions.confirmPublishDraft('old-token')
+ await expectLogic(logic).toDispatchActions(['loadWorkflowSuccess']).toFinishAllListeners()
+
+ expect(errorToast).toHaveBeenCalledWith('Publishing failed. Review the staged changes and try again.')
+ expect(reloads).toBe(1)
+ expect(logic.values.draftActionPending).toBeNull()
+ })
+
+ it('sends one publish and starts no other draft action while a publish is in flight', async () => {
+ let release: () => void = () => {}
+ const held = new Promise<void>((resolve) => {
+ release = resolve
+ })
+ useMocks(
+ serverMocks({
+ [PUBLISH_URL]: recordPublish(async () => {
+ await held
+ return [200, { ...preview, published: true }]
+ }),
+ })
+ )
+
+ logic.actions.confirmPublishDraft('preview-token')
+ logic.actions.confirmPublishDraft('preview-token')
+ expect(logic.values.draftActionPending).toBe('publish')
+ expect(logic.values.discardDisabledReason).toBe('Publishing is in progress')
+ logic.actions.publishDraft()
+ logic.actions.discardDraft()
+ expect(dialogs).toEqual([])
+
+ release()
+ await expectLogic(logic).toDispatchActions(['loadWorkflowSuccess']).toFinishAllListeners()
+
+ expect(publishBodies).toEqual([{ confirm: true, confirm_token: 'preview-token' }])
+ })
+
+ it.each([
+ {
+ action: 'discardDraft' as const,
+ title: 'Discard staged changes?',
+ url: '/api/environments/:team_id/hog_flows/:id/discard_draft/',
+ ok: true,
+ toast: ['success', 'Staged changes discarded'] as const,
+ expectedReloads: 1,
+ inFlight: { draftActionPending: 'discard' },
+ },
+ {
+ action: 'discardDraft' as const,
+ title: 'Discard staged changes?',
+ url: '/api/environments/:team_id/hog_flows/:id/discard_draft/',
+ ok: false,
+ toast: ['error', 'Could not discard the staged changes. Please try again.'] as const,
+ expectedReloads: 0,
+ inFlight: { draftActionPending: 'discard' },
+ },
+ {
+ action: 'resumeEmailSending' as const,
+ title: 'Resume email sending?',
+ url: '/api/projects/:team_id/hog_flows/:id/resume_email_sending/',
+ ok: true,
+ toast: ['success', 'Email sending resumed'] as const,
+ expectedReloads: 1,
+ inFlight: { resumeEmailSendingPending: true },
+ },
+ {
+ // Another editor may have resumed it already, so the banner state is re-read.
+ action: 'resumeEmailSending' as const,
+ title: 'Resume email sending?',
+ url: '/api/projects/:team_id/hog_flows/:id/resume_email_sending/',
+ ok: false,
+ toast: ['error', 'Could not resume email sending. Please try again.'] as const,
+ expectedReloads: 1,
+ inFlight: { resumeEmailSendingPending: true },
+ },
+ ])(
+ '$action posts once, only after the dialog is confirmed (ok: $ok)',
+ async ({ action, title, url, ok, toast, expectedReloads, inFlight }) => {
+ let posts = 0
+ useMocks(
+ serverMocks({
+ [url]: () => {
+ posts += 1
+ return ok ? [200, staged] : [500, { detail: 'boom' }]
+ },
+ })
+ )
+ const [kind, message] = toast
+ const toastSpy = jest.spyOn(lemonToast, kind)
+
+ logic.actions[action]()
+ await expectLogic(logic).toFinishAllListeners()
+ expect(posts).toBe(0)
+
+ // A double-click lands during the dialog's close animation, and a third click
+ // on the action button while the request runs must not open another dialog.
+ clickConfirm(title)
+ clickConfirm(title)
+ expect(logic.values).toMatchObject(inFlight)
+ logic.actions[action]()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(dialogs).toHaveLength(1)
+ expect(posts).toBe(1)
+ expect(toastSpy).toHaveBeenCalledWith(message)
+ expect(reloads).toBe(expectedReloads)
+ expect(logic.values.draftActionPending).toBeNull()
+ expect(logic.values.resumeEmailSendingPending).toBe(false)
+ }
+ )
+ })
+
it('keeps the draft actions mounted and blocks publish while edits are unsaved', () => {
logic.actions.setWorkflowValue('name', 'Still typing')