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.3 KB
Raw Blame
import { addressFromLocalPart, addressLocalPart, senderAddressError } from './emailSenderFields'

describe('emailSenderFields', () => {
    const withDomain = { id: 1, config: { email: '[email protected]', domain: 'example.com' } }
    // Senders without a stored domain are checked against their own address's domain.
    const withoutDomain = { id: 2, config: { email: '[email protected]' } }

    test.each([
        ['an empty address', '', withDomain],
        ['an address on the verified domain', '[email protected]', withDomain],
        ['an address whose domain differs only in case', '[email protected]', withDomain],
        ['a templated address', '{{ variables.alias }}@example.org', withDomain],
    ])('accepts %s', (_name, address, sender) => {
        expect(senderAddressError(address, sender)).toBeNull()
    })

    test.each([
        ['an address on another domain', '[email protected]', withDomain, 'Use an address on example.com'],
        ['an address off the domain of the sender address', '[email protected]', withoutDomain, 'example.com'],
        ['a name instead of an address', 'Security team', withDomain, 'Enter one email address'],
        ['two addresses', '[email protected], [email protected]', withDomain, 'Enter one email address'],
        ['an address too long to deliver', `${'a'.repeat(250)}@example.com`, withDomain, 'at most 254 characters'],
    ])('rejects %s', (_name, address, sender, expected) => {
        expect(senderAddressError(address, sender)).toContain(expected)
    })

    test.each([
        ['adds the domain to a local part', 'security', '[email protected]'],
        ['keeps a templated local part on the domain', '{{ variables.team }}', '{{ variables.team }}@example.com'],
        ['keeps an address that already has a domain', '[email protected]', '[email protected]'],
        ['clears the address when the input is empty', '  ', undefined],
    ])('%s', (_name, localPart, expected) => {
        expect(addressFromLocalPart(localPart, 'example.com')).toEqual(expected)
    })

    test.each([
        ['strips the verified domain', '[email protected]', 'security'],
        ['shows an off-domain address whole so its error is visible', '[email protected]', '[email protected]'],
    ])('%s', (_name, address, expected) => {
        expect(addressLocalPart(address, 'example.com')).toEqual(expected)
    })
})