浏览代码

Add tests for extracted data and reactive functions

Simone Todaro 5 年之前
父节点
当前提交
864fcd8c87
共有 1 个文件被更改,包括 34 次插入0 次删除
  1. 34 0
      test/data.spec.js

+ 34 - 0
test/data.spec.js

@@ -30,3 +30,37 @@ test('x-data attribute value is optional', async () => {
 
 
     expect(document.querySelector('span').innerText).toEqual('foo')
     expect(document.querySelector('span').innerText).toEqual('foo')
 })
 })
+
+test('x-data can use attributes from a reusable function', async () => {
+    document.body.innerHTML = `
+        <div x-data="test()">
+            <span x-text="foo"></span>
+        </div>
+    `
+        test = function() {
+            return {
+                foo: 'bar',
+            }
+        }
+
+    Alpine.start()
+
+    expect(document.querySelector('span').innerText).toEqual('bar')
+})
+
+test('functions in x-data are reactive', async () => {
+    document.body.innerHTML = `
+        <div x-data="{ foo: 'bar', getFoo() {return this.foo}}">
+            <span x-text="getFoo()"></span>
+
+            <button x-on:click="foo = 'baz'"></button>
+        </div>
+    `
+    Alpine.start()
+
+    expect(document.querySelector('span').innerText).toEqual('bar')
+
+    document.querySelector('button').click()
+
+    await wait(() => { expect(document.querySelector('span').innerText).toEqual('baz') })
+})