How to use alpinejs - 9 common examples

To help you get started, we’ve selected a few alpinejs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github alpinejs / alpine / test / ref.spec.js View on Github external
test('can reference elements from event listeners', async () => {
    document.body.innerHTML = `
        <div>
            <span></span>

            <button></button>
        </div>
    `

    Alpine.start()

    expect(document.querySelector('span').innerText).toEqual(undefined)

    document.querySelector('button').click()

    await wait(() =&gt; { expect(document.querySelector('span').innerText).toEqual('lob') })
})
github alpinejs / alpine / test / constructor.spec.js View on Github external
test('auto-detect x-data property changes at run-time', async () =&gt; {
    var runObservers = []

    global.MutationObserver = class {
        constructor(callback) { runObservers.push(callback) }
        observe() {}
    }

    document.body.innerHTML = `
        <div>
            <span></span>
        </div>
    `

    Alpine.start()

    expect(document.querySelector('span').innerText).toEqual(0)

    document.querySelector('div').setAttribute('x-data', '{ count: 1 }')

    runObservers[0]([
        {
            addedNodes: [],
            type: 'attributes',
            attributeName: 'x-data',
            target: document.querySelector('div')
        }
    ])

    await wait(() =&gt; { expect(document.querySelector('span').innerText).toEqual(1) })
})
github alpinejs / alpine / test / nesting.spec.js View on Github external
test('can access parent properties after nested components', async () =&gt; {
    document.body.innerHTML = `
        <div>
            <div>
                <button>Something</button>
            </div>

            <span></span>
        </div>
    `

    Alpine.start()

    expect(document.querySelector('span').innerText).toEqual('bar')
})
github alpinejs / alpine / test / cloak.spec.js View on Github external
test('x-cloak is removed', async () =&gt; {
    document.body.innerHTML = `
        <div>
            <span></span>
        </div>
    `

    expect(document.querySelector('span').getAttribute('x-cloak')).not.toBeNull()

    Alpine.start()

    await wait(() =&gt; { expect(document.querySelector('span').getAttribute('x-cloak')).toBeNull() })
})
github alpinejs / alpine / test / show.spec.js View on Github external
test('x-show toggles display: none; with no other style attributes', async () =&gt; {
    document.body.innerHTML = `
        <div>
            <span></span>

            <button></button>
        </div>
    `

    Alpine.start()

    expect(document.querySelector('span').getAttribute('style')).toEqual(null)

    document.querySelector('button').click()

    await wait(() =&gt; { expect(document.querySelector('span').getAttribute('style')).toEqual('display: none;') })
})
github alpinejs / alpine / test / bind.spec.js View on Github external
test('boolean attributes set to false are removed from element', async () =&gt; {
    document.body.innerHTML = `
        <div>
            <input>
            <input>
            <input>
            <input>
            <input>
        </div>
    `

    Alpine.start()

    expect(document.querySelectorAll('input')[0].disabled).toBeFalsy()
    expect(document.querySelectorAll('input')[1].checked).toBeFalsy()
    expect(document.querySelectorAll('input')[2].required).toBeFalsy()
    expect(document.querySelectorAll('input')[3].readOnly).toBeFalsy()
    expect(document.querySelectorAll('input')[4].hidden).toBeFalsy()
})
github alpinejs / alpine / test / data.spec.js View on Github external
test('data manipulated on component object is reactive', async () =&gt; {
    document.body.innerHTML = `
        <div>
            <span></span>
        </div>
    `

    Alpine.start()

    document.querySelector('div').__x.data.foo = 'baz'

    await wait(() =&gt; { expect(document.querySelector('span').innerText).toEqual('baz') })
})
github alpinejs / alpine / test / text.spec.js View on Github external
test('x-text on triggered update', async () =&gt; {
    document.body.innerHTML = `
        <div>
            <button></button>

            <span></span>
        </div>
    `

    Alpine.start()

    await wait(() =&gt; { expect(document.querySelector('span').innerText).toEqual('') })

    document.querySelector('button').click()

    await wait(() =&gt; { expect(document.querySelector('span').innerText).toEqual('bar') })
})
github alpinejs / alpine / test / model.spec.js View on Github external
test('x-model trims value if trim modifier is present', async () =&gt; {
    document.body.innerHTML = `
        <div>
            <input>

            <span></span>
        </div>
    `

    Alpine.start()

    fireEvent.input(document.querySelector('input'), { target: { value: 'bar   ' }})

    await wait(() =&gt; { expect(document.querySelector('span').innerText).toEqual('bar') })
})

alpinejs

The rugged, minimal JavaScript framework

MIT
Latest version published 16 days ago

Package Health Score

88 / 100
Full package analysis

Popular alpinejs functions