123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import Alpine from 'alpinejs'
- import { wait } from '@testing-library/dom'
- global.MutationObserver = class {
- observe() {}
- }
- test('x-init', async () => {
- var spanValue
- window.setSpanValue = (el) => {
- spanValue = el.innerHTML
- }
- document.body.innerHTML = `
- <div x-data="{ foo: 'bar' }" x-init="window.setSpanValue($refs.foo)">
- <span x-text="foo" x-ref="foo">baz</span>
- </div>
- `
- Alpine.start()
- expect(spanValue).toEqual('baz')
- })
- test('x-created', async () => {
- var spanValue
- window.setSpanValue = (el) => {
- spanValue = el.innerHTML
- }
- document.body.innerHTML = `
- <div x-data="{ foo: 'bar' }" x-created="window.setSpanValue($refs.foo)">
- <span x-text="foo" x-ref="foo">baz</span>
- </div>
- `
- Alpine.start()
- expect(spanValue).toEqual('baz')
- })
- test('x-mounted', async () => {
- var spanValue
- window.setSpanValue = (el) => {
- spanValue = el.innerText
- }
- document.body.innerHTML = `
- <div x-data="{ foo: 'bar' }" x-mounted="window.setSpanValue($refs.foo)">
- <span x-text="foo" x-ref="foo">baz</span>
- </div>
- `
- Alpine.start()
- expect(spanValue).toEqual('bar')
- })
- test('callbacks registered within x-created can affect reactive data changes', async () => {
- document.body.innerHTML = `
- <div x-data="{ bar: 'baz', foo() { this.$refs.foo.addEventListener('click', () => { this.bar = 'bob' }) } }" x-created="foo()">
- <button x-ref="foo"></button>
- <span x-text="bar"></span>
- </div>
- `
- Alpine.start()
- expect(document.querySelector('span').innerText).toEqual('baz')
- document.querySelector('button').click()
- await wait(() => { expect(document.querySelector('span').innerText).toEqual('bob') })
- })
- test('callbacks registered within x-mounted can affect reactive data changes', async () => {
- document.body.innerHTML = `
- <div x-data="{ bar: 'baz', foo() { this.$refs.foo.addEventListener('click', () => { this.bar = 'bob' }) } }" x-mounted="foo()">
- <button x-ref="foo"></button>
- <span x-text="bar"></span>
- </div>
- `
- Alpine.start()
- expect(document.querySelector('span').innerText).toEqual('baz')
- document.querySelector('button').click()
- await wait(() => { expect(document.querySelector('span').innerText).toEqual('bob') })
- })
|