Ryan Chandler 4 years ago
parent
commit
d5a4af6aa6
1 changed files with 49 additions and 0 deletions
  1. 49 0
      tests/persistence.spec.js

+ 49 - 0
tests/persistence.spec.js

@@ -0,0 +1,49 @@
+import Alpine from 'alpinejs'
+import Spruce from '../dist/spruce'
+import { waitFor } from '@testing-library/dom'
+
+beforeEach(() => {
+    Spruce.subscribers = []
+})
+
+beforeAll(() => {
+    window.Spruce = Spruce
+    window.Alpine = Alpine
+
+    window.localStorage = {
+        storage: {},
+        getItem(key) {
+            return this.storage[key]
+        },
+        setItem(key, value) {
+            this.storage[key] = value
+        }
+    }
+})
+
+test('persisted stores are correctly persisted', async () => {
+    document.body.innerHTML = `
+        <div x-data>
+            <input type="text" x-model="$store.persisted.foo">
+        </div>
+    `
+    
+    Spruce.store('persisted', {
+        foo: 'bar',
+    }, true)
+
+    await Spruce.start()
+
+    Alpine.start()
+
+    expect(document.querySelector('input').value).toEqual('bar')
+
+    document.querySelector('input').value = 'car'
+
+    document.querySelector('input').dispatchEvent(new Event('input'))
+
+    await waitFor(() => {
+        expect(document.querySelector('input').value).toEqual('car')
+        expect(Spruce.retrieveFromLocalStorage('persisted').foo).toEqual('car')
+    })
+})