import Alpine from 'alpinejs'
import { wait, fireEvent } from '@testing-library/dom'
global.MutationObserver = class {
observe() {}
}
test('x-model has value binding when initialized', async () => {
document.body.innerHTML = `
`
Alpine.start()
expect(document.querySelector('input').value).toEqual('bar')
})
test('x-model updates value when updated via input event', async () => {
document.body.innerHTML = `
`
Alpine.start()
fireEvent.input(document.querySelector('input'), { target: { value: 'baz' }})
await wait(() => { expect(document.querySelector('input').value).toEqual('baz') })
})
test('x-model reflects data changed elsewhere', async () => {
document.body.innerHTML = `
`
Alpine.start()
document.querySelector('button').click()
await wait(() => { expect(document.querySelector('input').value).toEqual('baz') })
})
test('x-model casts value to number if number modifier is present', async () => {
document.body.innerHTML = `
`
Alpine.start()
fireEvent.input(document.querySelector('input'), { target: { value: '123' }})
await wait(() => { expect(document.querySelector('[x-data]').__x.$data.foo).toEqual(123) })
})
test('x-model trims value if trim modifier is present', async () => {
document.body.innerHTML = `
`
Alpine.start()
fireEvent.input(document.querySelector('input'), { target: { value: 'bar ' }})
await wait(() => { expect(document.querySelector('span').innerText).toEqual('bar') })
})
test('x-model updates value when updated via changed event when lazy modifier is present', async () => {
document.body.innerHTML = `
`
Alpine.start()
fireEvent.change(document.querySelector('input'), { target: { value: 'baz' }})
await wait(() => { expect(document.querySelector('input').value).toEqual('baz') })
})
test('x-model binds checkbox value', async () => {
document.body.innerHTML = `
`
Alpine.start()
expect(document.querySelector('input').checked).toEqual(true)
expect(document.querySelector('span').getAttribute('bar')).toEqual("true")
fireEvent.change(document.querySelector('input'), { target: { checked: false }})
await wait(() => { expect(document.querySelector('span').getAttribute('bar')).toEqual("false") })
})
test('x-model binds checkbox value to array', async () => {
document.body.innerHTML = `
`
Alpine.start()
expect(document.querySelectorAll('input')[0].checked).toEqual(true)
expect(document.querySelectorAll('input')[1].checked).toEqual(false)
expect(document.querySelector('span').getAttribute('bar')).toEqual("bar")
fireEvent.change(document.querySelectorAll('input')['1'], { target: { checked: true }})
await wait(() => {
expect(document.querySelectorAll('input')[0].checked).toEqual(true)
expect(document.querySelectorAll('input')[1].checked).toEqual(true)
expect(document.querySelector('span').getAttribute('bar')).toEqual("bar,baz")
})
})
test('x-model binds radio value', async () => {
document.body.innerHTML = `
`
Alpine.start()
expect(document.querySelectorAll('input')[0].checked).toEqual(true)
expect(document.querySelectorAll('input')[1].checked).toEqual(false)
expect(document.querySelector('span').getAttribute('bar')).toEqual('bar')
fireEvent.change(document.querySelectorAll('input')[1], { target: { checked: true }})
await wait(() => {
expect(document.querySelectorAll('input')[0].checked).toEqual(false)
expect(document.querySelectorAll('input')[1].checked).toEqual(true)
expect(document.querySelector('span').getAttribute('bar')).toEqual('baz')
})
})
test('x-model binds select dropdown', async () => {
document.body.innerHTML = `
Please select one
bar
baz
`
Alpine.start()
expect(document.querySelectorAll('option')[0].selected).toEqual(false)
expect(document.querySelectorAll('option')[1].selected).toEqual(true)
expect(document.querySelectorAll('option')[2].selected).toEqual(false)
expect(document.querySelector('span').innerText).toEqual('bar')
fireEvent.change(document.querySelector('select'), { target: { value: 'baz' }});
await wait(() => {
expect(document.querySelectorAll('option')[0].selected).toEqual(false)
expect(document.querySelectorAll('option')[1].selected).toEqual(false)
expect(document.querySelectorAll('option')[2].selected).toEqual(true)
expect(document.querySelector('span').innerText).toEqual('baz')
})
})
test('x-model binds multiple select dropdown', async () => {
document.body.innerHTML = `
Please select one
bar
baz
`
Alpine.start()
expect(document.querySelectorAll('option')[0].selected).toEqual(false)
expect(document.querySelectorAll('option')[1].selected).toEqual(true)
expect(document.querySelectorAll('option')[2].selected).toEqual(false)
expect(document.querySelector('span').innerText).toEqual(['bar'])
document.querySelectorAll('option')[2].selected = true
fireEvent.change(document.querySelector('select'));
await wait(() => {
expect(document.querySelectorAll('option')[0].selected).toEqual(false)
expect(document.querySelectorAll('option')[1].selected).toEqual(true)
expect(document.querySelectorAll('option')[2].selected).toEqual(true)
expect(document.querySelector('span').innerText).toEqual(['bar', 'baz'])
})
})
test('x-model binds nested keys', async () => {
document.body.innerHTML = `
`
Alpine.start()
expect(document.querySelector('input').value).toEqual('foo')
expect(document.querySelector('span').innerText).toEqual('foo')
fireEvent.input(document.querySelector('input'), { target: { value: 'bar' }})
await wait(() => {
expect(document.querySelector('input').value).toEqual('bar')
expect(document.querySelector('span').innerText).toEqual('bar')
})
})