Ryan Chandler 5 gadi atpakaļ
vecāks
revīzija
d71d270cee
2 mainītis faili ar 52 papildinājumiem un 0 dzēšanām
  1. 17 0
      examples/index.html
  2. 35 0
      tests/set.spec.js

+ 17 - 0
examples/index.html

@@ -0,0 +1,17 @@
+<html>
+    <head>
+        <script src="../dist/spruce.umd.js"></script>
+        <script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js"></script>
+    </head>
+    <body>
+        <div x-data x-subscribe>
+            <span x-text="$store.application.name"></span>
+            <button @click="$store.application.name = 'Example'">Click</button>
+        </div>
+        <script>
+            Spruce.store('application', {
+                name: 'Application'
+            })
+        </script>
+    </body>
+</html>

+ 35 - 0
tests/set.spec.js

@@ -0,0 +1,35 @@
+import Alpine from 'alpinejs'
+import Spruce from '../dist/spruce'
+import { waitFor } from '@testing-library/dom'
+
+beforeEach(() => {
+    Spruce.subscribers = []
+})
+
+beforeAll(() => {
+    window.Spruce = Spruce
+})
+
+test('$store > data can be set inside component', async () => {
+    document.body.innerHTML = `
+        <div x-data x-subscribe>
+            <span x-text="$store.foo.bar"></span>
+            <button @click="$store.foo.bar = 'car'"></button>
+        </div>
+    `
+
+    Spruce.store('foo', {
+        bar: 'bob'
+    })
+
+    await Spruce.start()
+    
+    Alpine.start()
+
+    document.querySelector('button').click()
+
+    await waitFor(() => {
+        expect(Spruce.stores.foo.bar).toEqual('car')
+        expect(document.querySelector('span').innerText).toEqual('car')
+    })
+})