瀏覽代碼

tests(Spruce.watch)

Ryan Chandler 5 年之前
父節點
當前提交
031ad14f03
共有 9 個文件被更改,包括 25 次插入6 次删除
  1. 0 0
      dist/spruce.js
  2. 0 0
      dist/spruce.js.map
  3. 0 0
      dist/spruce.module.js
  4. 0 0
      dist/spruce.module.js.map
  5. 0 0
      dist/spruce.umd.js
  6. 0 0
      dist/spruce.umd.js.map
  7. 5 1
      src/bus.js
  8. 0 5
      src/index.js
  9. 20 0
      tests/bus.spec.js

File diff suppressed because it is too large
+ 0 - 0
dist/spruce.js


File diff suppressed because it is too large
+ 0 - 0
dist/spruce.js.map


File diff suppressed because it is too large
+ 0 - 0
dist/spruce.module.js


File diff suppressed because it is too large
+ 0 - 0
dist/spruce.module.js.map


File diff suppressed because it is too large
+ 0 - 0
dist/spruce.umd.js


File diff suppressed because it is too large
+ 0 - 0
dist/spruce.umd.js.map


+ 5 - 1
src/bus.js

@@ -37,6 +37,10 @@ export default {
     runWatchers(stores, target, key) {
         const self = this
 
+        if (self.watchers[key]) {
+            return self.watchers[key].forEach(callback => callback(target[key]))
+        }
+
         Object.keys(self.watchers)
             .filter(watcher => watcher.includes('.'))
             .forEach(fullDotNotationKey => {
@@ -45,7 +49,7 @@ export default {
                 if (key !== dotNotationParts[dotNotationParts.length - 1]) return
 
                 dotNotationParts.reduce((comparison, part) => {
-                    if (Object.is(target, comparison)) {
+                    if (comparison[key] === target[key] || Object.is(target, comparison)) {
                         self.watchers[fullDotNotationKey].forEach(callback => callback(target[key]))
                     }
 

+ 0 - 5
src/index.js

@@ -4,7 +4,6 @@ import EventBus from './bus'
 
 const Spruce = {
     options: {
-        enableUpdateEvents: true,
         globalStoreVariable: false,
     },
 
@@ -27,10 +26,6 @@ const Spruce = {
                 this.events.runWatchers(this.stores, target, key)
 
                 this.updateSubscribers(key, value)
-
-                if (this.options.enableUpdateEvents) {
-                    this.emit('updated')
-                }
             }
         })
 

+ 20 - 0
tests/bus.spec.js

@@ -64,4 +64,24 @@ test('.emit() > will dispatch browser event to window with spruce: prefix', asyn
     await waitFor(() => {
         expect(document.querySelector('span').innerText).toEqual('car')
     })
+})
+
+test('.watch() > can listen for changes to property', async () => {
+    let fixture = undefined
+    
+    Spruce.store('example', {
+        cool: 'stuff'
+    })
+
+    Spruce.watch('example.cool', (value) => {
+        fixture = value
+    })
+
+    await Spruce.start()
+
+    expect(fixture).toBeUndefined()
+
+    Spruce.stores.example.cool = 'amazing'
+
+    expect(fixture).toEqual('amazing')
 })

Some files were not shown because too many files changed in this diff