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.4 KB
Raw Blame
import { LiquidRenderer } from './liquid'

describe('LiquidRenderer', () => {
    const context = { person: { properties: { first_name: 'Ada', company: null } } }

    // Unlayer stores `>` as `>`, so a renderer that skips the decode step compares the
    // literal entity and silently takes the wrong branch.
    it.each([
        ['on one line', '{% if person.properties.first_name != "" %}'],
        ['across lines', '{% if person.properties.first_name\n  != "" %}'],
        // The email editor saves a link's spaces as %20, inside the tag too.
        ['in a link', '{%%20if%20person.properties.first_name%20!=%20""%20%}'],
    ])('renders a tag the editor has escaped %s', (_, openTag) => {
        const template = `${openTag}Hi {{ person.properties.first_name }}{% endif %}`
        expect(LiquidRenderer.render(template, context)).toBe('Hi Ada')
    })

    it.each([
        ['person.properties.first_name', true],
        ['person.properties.company', false],
        ['person.properties.missing', false],
        ['person.properties.first_name | upcase', true],
    ])('resolves(%s) is %s', (expression, expected) => {
        expect(LiquidRenderer.resolves(expression, context)).toBe(expected)
    })

    it.each([
        ['{{%20person.properties.company%20|%20default:%20"a%20b"%20}}', 'a%20b'],
        ['{{%20"a\\"b%20c"%20|%20size%20}}', '7'],
    ])('keeps %%20 inside a quoted string, where it is part of the value: %s', (template, expected) => {
        expect(LiquidRenderer.render(template, context)).toBe(expected)
    })

    it('formats money with the currency filter, like the worker does at send time', () => {
        expect(LiquidRenderer.render('{{ amount | currency: "USD" }}', { amount: 1234.5 })).toBe('$1,234.50')
    })

    it.each(['{% unsubscribe_url %}', '{%- unsubscribe_url -%}'])(
        'renders the Customer.io tag %s like the unsubscribe_url variable',
        (tag) => {
            const unsubscribe = { unsubscribe_url: 'https://example.com/preferences?token=abc&team=1' }
            expect(LiquidRenderer.render(`<a href="${tag}">x</a>`, unsubscribe)).toBe(
                LiquidRenderer.render('<a href="{{ unsubscribe_url }}">x</a>', unsubscribe)
            )
        }
    )

    it('treats an unparseable expression as unresolved rather than throwing', () => {
        expect(LiquidRenderer.resolves('person.properties[', context)).toBe(false)
    })
})