]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: getRootState
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 16:50:35 +0000 (17:50 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:21:46 +0000 (19:21 +0100)
__tests__/rootState.spec.ts [new file with mode: 0644]
src/store.ts

diff --git a/__tests__/rootState.spec.ts b/__tests__/rootState.spec.ts
new file mode 100644 (file)
index 0000000..ce3881d
--- /dev/null
@@ -0,0 +1,48 @@
+import { createStore, getRootState } from '../src'
+
+describe('Store', () => {
+  const useA = createStore({
+    id: 'a',
+    state: () => ({ a: 'a' }),
+  })
+
+  const useB = createStore({
+    id: 'b',
+    state: () => ({ b: 'b' }),
+  })
+
+  it('works with no stores', () => {
+    expect(getRootState({})).toEqual({})
+  })
+
+  it('retrieves the root state of one store', () => {
+    const req = {}
+    useA(req)
+    expect(getRootState(req)).toEqual({
+      a: { a: 'a' },
+    })
+  })
+
+  it('does not mix up different requests', () => {
+    const req1 = {}
+    const req2 = {}
+    useA(req1)
+    useB(req2)
+    expect(getRootState(req1)).toEqual({
+      a: { a: 'a' },
+    })
+    expect(getRootState(req2)).toEqual({
+      b: { b: 'b' },
+    })
+  })
+
+  it('can hold multiple stores', () => {
+    const req1 = {}
+    useA(req1)
+    useB(req1)
+    expect(getRootState(req1)).toEqual({
+      a: { a: 'a' },
+      b: { b: 'b' },
+    })
+  })
+})
index 0c1f59488cfca3cae756fc139a716589a4fc8ebc..6db5db61d60e5e1885d5a415d33f23db819a5111 100644 (file)
@@ -228,14 +228,11 @@ function setInitialState(store: Store<string, StateTree, any, any>): void {
   if (provider) provider.set(store)
 }
 
-type StoreGetterWithGetters<
-  G extends Record<string, StoreGetter<StateTree>>
-> = {
-  [k in keyof G]: G[k] extends StoreGetter<infer S, infer V>
-    ? (state: S, getters: G) => V
-    : never
-}
-
+/**
+ * Gets the root state of all active stores. This is useful when reporting an application crash by
+ * retrieving the problematic state and send it to your error tracking service.
+ * @param req request key
+ */
 export function getRootState(req: NonNullObject): Record<string, StateTree> {
   const stores = storesMap.get(req)
   if (!stores) return {}
@@ -245,8 +242,6 @@ export function getRootState(req: NonNullObject): Record<string, StateTree> {
     rootState[store.id] = store.state
   }
 
-  console.log('global state', rootState)
-
   return rootState
 }