]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: store.subscribe (#29)
authorPatrick <trick_stival@hotmail.com>
Mon, 23 Dec 2019 20:45:06 +0000 (17:45 -0300)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Mon, 23 Dec 2019 20:45:06 +0000 (21:45 +0100)
* added subscription tests and unsubscribe function

* added condition to unsubscribe only if listener exists

* added test to multiple unsubscriptions

* using spies to watch subscriptions being called

__tests__/subscriptions.spec.ts [new file with mode: 0644]
src/store.ts
src/types.ts

diff --git a/__tests__/subscriptions.spec.ts b/__tests__/subscriptions.spec.ts
new file mode 100644 (file)
index 0000000..4dbbb9a
--- /dev/null
@@ -0,0 +1,39 @@
+import { createStore } from '../src'
+
+describe('Subscriptions', () => {
+  const useStore = createStore('main', () => ({
+    name: 'Eduardo',
+  })).bind(null, true)
+
+  let store: ReturnType<typeof useStore>
+  beforeEach(() => {
+    store = useStore()
+  })
+
+  it('fires callback when patch is applied', () => {
+    const spy = jest.fn()
+    store.subscribe(spy)
+    store.state.name = 'Cleiton'
+    expect(spy).toHaveBeenCalledTimes(1)
+  })
+
+  it('unsubscribes callback when unsubscribe is called', () => {
+    const spy = jest.fn()
+    const unsubscribe = store.subscribe(spy)
+    unsubscribe()
+    store.state.name = 'Cleiton'
+    expect(spy).not.toHaveBeenCalled()
+  })
+
+  it('listeners are not affected when unsubscribe is called multiple times', () => {
+    const func1 = jest.fn()
+    const func2 = jest.fn()
+    const unsubscribe1 = store.subscribe(func1)
+    store.subscribe(func2)
+    unsubscribe1()
+    unsubscribe1()
+    store.state.name = 'Cleiton'
+    expect(func1).not.toHaveBeenCalled()
+    expect(func2).toHaveBeenCalledTimes(1)
+  })
+})
index 958f8a33bc6dcd3e8ae412235155d838b7a5c2ff..9cabaef7365cba5406059931a0f688e1a4aedad5 100644 (file)
@@ -89,9 +89,14 @@ export function buildStore<
     })
   }
 
-  function subscribe(callback: SubscriptionCallback<S>): void {
+  function subscribe(callback: SubscriptionCallback<S>) {
     subscriptions.push(callback)
-    // TODO: return function to remove subscription
+    return () => {
+      const idx = subscriptions.indexOf(callback)
+      if (idx > -1) {
+        subscriptions.splice(idx, 1)
+      }
+    }
   }
 
   function reset() {
index f3c6f51d3426c6c249b43ba54fed40d5b0c72726..58052fb97c828f8a8a8afbd31e3c0553ac7aedbc 100644 (file)
@@ -88,9 +88,10 @@ export interface Store<Id extends string, S extends StateTree> {
 
   /**
    * Setups a callback to be called whenever the state changes.
-   * @param callback callback that is called whenever the state changes
+   * @param callback callback that is called whenever the state
+   * @returns function that removes callback from subscriptions
    */
-  subscribe(callback: SubscriptionCallback<S>): void
+  subscribe(callback: SubscriptionCallback<S>): () => void
 }
 
 export interface DevtoolHook {