Samuel Štancl 4 år sedan
förälder
incheckning
baa2176383
1 ändrade filer med 19 tillägg och 0 borttagningar
  1. 19 0
      test/lifecycle.spec.js

+ 19 - 0
test/lifecycle.spec.js

@@ -103,3 +103,22 @@ test('x-init is capable of dispatching an event', async () => {
         expect(document.querySelector('span').textContent).toEqual('baz')
     })
 })
+
+test('onBeforeComponentInitialized is capable of modifying the element', async () => {
+    document.body.innerHTML = `
+        <div x-data="{ init() { window.foo = 'bar' }"></div>
+    `
+    
+    Alpine.onBeforeComponentInitialized(component => {
+        if (! component.$el.hasAttribute('x-init') && component.$data.init)
+            component.$el.setAttribute('x-init', 'init()')
+    })
+    
+    Alpine.start()
+    
+    await wait(() => {
+        expect(document.querySelector('div').getAttribute('x-init')).toEqual('init()')
+        
+        expect(window.foo).toEqual('bar')
+    })
+})