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.

2.0 KB
Raw Blame
import { ApiError } from 'lib/api'

import { billingAlertSaveErrorMessage, billingAlertWritePayload, formErrorsFromApiError } from './billingAlertFormLogic'

describe('billing alert form server errors', () => {
    it('maps DRF field errors onto the form', () => {
        const error = new ApiError('Bad request', 400, undefined, {
            name: ['Ensure this field has no more than 160 characters.'],
            threshold_value: ['A valid number is required.'],
        })

        expect(formErrorsFromApiError(error)).toEqual({
            name: 'Ensure this field has no more than 160 characters.',
            thresholdValue: 'A valid number is required.',
        })
    })

    it('sends pending destinations in the atomic configuration write', () => {
        const payload = billingAlertWritePayload(
            {
                name: 'Period spend cap',
                description: '',
                enabled: true,
                metric: 'projected_spend',
                thresholdValue: 100,
            },
            [
                {
                    key: 'webhook',
                    label: 'Webhook',
                    payload: { type: 'webhook', webhook_url: 'https://example.com/alerts' },
                },
            ]
        )

        expect(payload.enabled).toBe(true)
        expect(payload.metric).toBe('projected_spend')
        expect(payload.threshold_type).toBe('absolute_value')
        expect(payload.destination_changes).toEqual({
            create: [{ type: 'webhook', webhook_url: 'https://example.com/alerts' }],
        })
    })

    it('surfaces nested destination validation instead of a generic transport error', () => {
        const error = new ApiError('Bad request', 400, undefined, {
            destination_changes: {
                create: [{ webhook_url: ['Enter a supported Microsoft Teams webhook URL.'] }],
            },
        })

        expect(billingAlertSaveErrorMessage(error)).toBe('Enter a supported Microsoft Teams webhook URL.')
    })
})