: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { expectLogic } from 'kea-test-utils'
import { resumeKeaLoadersErrors, silenceKeaLoadersErrors } from '~/initKea'
import { initKeaTests } from '~/test/init'
import { actionsLogic } from '~/toolbar/shared/actions/actionsLogic'
import { actionsTabLogic } from '~/toolbar/shared/actions/actionsTabLogic'
import { toolbarLogic } from '~/toolbar/shared/bar/toolbarLogic'
import { toolbarConfigLogic } from '~/toolbar/shared/toolbarConfigLogic'
// The toolbar logger mirrors intentional error/auth paths to the console (its job on
// customer pages); tests exercise those paths on purpose, so stub the boundary.
jest.mock('~/toolbar/shared/toolbarLogger', () => ({
toolbarLogger: { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() },
}))
const mockAction = {
id: 42,
name: 'Test Action',
steps: [],
created_at: '',
created_by: null,
pinned_at: null,
}
describe('actionsTabLogic form submission', () => {
// These suites intentionally reject requests to exercise error states; the
// failures are asserted via authStatus/values, so skip the global loader logging.
beforeAll(silenceKeaLoadersErrors)
afterAll(resumeKeaLoadersErrors)
let logic: ReturnType<typeof actionsTabLogic.build>
beforeEach(() => {
initKeaTests()
toolbarConfigLogic
.build({
apiURL: 'http://localhost',
accessToken: 'test-token',
refreshToken: 'test-refresh',
clientId: 'test-client',
})
.mount()
toolbarLogic().mount()
actionsLogic().mount()
logic = actionsTabLogic()
logic.mount()
})
it('creates a new action via POST', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockAction),
} as any as Response)
)
logic.actions.newAction()
logic.actions.setActionFormValue('name', 'Test Action')
await expectLogic(logic, () => {
logic.actions.submitActionForm()
})
.delay(0)
.toDispatchActions(['submitActionForm', 'submitActionFormSuccess'])
expect(global.fetch).toHaveBeenCalledWith(
expect.stringContaining('/api/projects/@current/actions/'),
expect.objectContaining({ method: 'POST' })
)
})
it('updates an existing action via PATCH', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockAction),
} as any as Response)
)
logic.actions.selectAction(42)
logic.actions.setActionFormValue('name', 'Updated Action')
await expectLogic(logic, () => {
logic.actions.submitActionForm()
})
.delay(0)
.toDispatchActions(['submitActionForm', 'submitActionFormSuccess'])
expect(global.fetch).toHaveBeenCalledWith(
expect.stringContaining('/api/projects/@current/actions/42/'),
expect.objectContaining({ method: 'PATCH' })
)
})
it('handles error response with detail message', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: false,
status: 400,
json: () => Promise.resolve({ detail: 'Validation error' }),
} as any as Response)
)
logic.actions.newAction()
logic.actions.setActionFormValue('name', 'Bad Action')
await expectLogic(logic, () => {
logic.actions.submitActionForm()
})
.delay(0)
.toDispatchActions(['submitActionForm', 'submitActionFormFailure'])
.toNotHaveDispatchedActions(['submitActionFormSuccess'])
})
it('handles error response when json parsing fails', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: false,
status: 500,
json: () => Promise.reject(new Error('parse error')),
} as any as Response)
)
logic.actions.newAction()
logic.actions.setActionFormValue('name', 'Server Error Action')
await expectLogic(logic, () => {
logic.actions.submitActionForm()
})
.delay(0)
.toDispatchActions(['submitActionForm', 'submitActionFormFailure'])
.toNotHaveDispatchedActions(['submitActionFormSuccess'])
})
it('sets creation_context when automatic action creation is enabled', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockAction),
} as any as Response)
)
logic.actions.setAutomaticActionCreationEnabled(true, 'Auto Action')
logic.actions.newAction()
await expectLogic(logic, () => {
logic.actions.submitActionForm()
})
.delay(0)
.toDispatchActions(['submitActionForm', 'submitActionFormSuccess'])
const fetchCall = (global.fetch as jest.Mock).mock.calls[0]
const body = JSON.parse(fetchCall[1].body)
expect(body.creation_context).toBe('onboarding')
})
})