]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: refactor
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 8 Mar 2021 13:13:41 +0000 (14:13 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 8 Mar 2021 13:13:41 +0000 (14:13 +0100)
__tests__/rootState.spec.ts
jest.config.js
package.json
src/devtools.ts
src/index.ts
src/rootStore.ts
src/store.ts
yarn.lock

index 6511d37ee58f5d27e59417de40f3dead0e7c2ff6..3103756d147d645e01488487a86fa90d76c74964 100644 (file)
@@ -1,6 +1,8 @@
 import { createPinia, defineStore } from '../src'
+import { mockWarn } from 'jest-mock-warn'
 
 describe('Root State', () => {
+  mockWarn()
   const useA = defineStore({
     id: 'a',
     state: () => ({ a: 'a' }),
@@ -11,6 +13,11 @@ describe('Root State', () => {
     state: () => ({ b: 'b' }),
   })
 
+  it('warns if creating a store without a pinia', () => {
+    expect(() => useA()).toThrow()
+    expect('with no active Pinia').toHaveBeenWarned()
+  })
+
   it('works with no stores', () => {
     expect(createPinia().state.value).toEqual({})
   })
index 5c453457d60491364e39bf21b109c43a214b89aa..32b6e54459f298f312451a36df2a070c32d93140 100644 (file)
@@ -8,6 +8,7 @@ module.exports = {
     '/node_modules/',
     'src/index.ts',
     'src/devtools.ts',
+    'src/deprecated.ts',
   ],
   testMatch: ['<rootDir>/__tests__/**/*.spec.ts'],
   globals: {
index 5f763991bc7986fc13f141e0ae4d9f83651c7628..95877bf02e1dc9e038dd5a14caa9f4f43b72fb41 100644 (file)
@@ -68,6 +68,7 @@
     "codecov": "^3.6.1",
     "conventional-changelog-cli": "^2.1.0",
     "jest": "^26.4.2",
+    "jest-mock-warn": "^1.1.0",
     "lint-staged": "^10.2.11",
     "pascalcase": "^1.0.0",
     "prettier": "^2.1.2",
index aa7ae35245d3f24b83f7db1a5010e72f7250e908..d6e97242e06e73e36c8b148ff02eae6ddc2937e8 100644 (file)
@@ -4,7 +4,6 @@ import {
   setupDevtoolsPlugin,
 } from '@vue/devtools-api'
 import { App } from 'vue'
-import { getRegisteredStores, registerStore } from './rootStore'
 import { GenericStore } from './types'
 
 function formatDisplay(display: string) {
@@ -15,6 +14,15 @@ function formatDisplay(display: string) {
   }
 }
 
+/**
+ * Registered stores used for devtools.
+ */
+export const stores = /*#__PURE__*/ new Set<GenericStore>()
+
+export function registerStore(store: GenericStore) {
+  stores.add(store)
+}
+
 function toastMessage(
   message: string,
   type?: 'normal' | 'error' | 'warning' | undefined
@@ -112,7 +120,7 @@ export function addDevtools(app: App, store: GenericStore) {
 
       api.on.getInspectorTree((payload) => {
         if (payload.app === app && payload.inspectorId === piniaInspectorId) {
-          const stores = Array.from(getRegisteredStores())
+          const stores = Array.from(stores)
 
           payload.rootNodes = (payload.filter
             ? stores.filter((store) =>
@@ -125,7 +133,7 @@ export function addDevtools(app: App, store: GenericStore) {
 
       api.on.getInspectorState((payload) => {
         if (payload.app === app && payload.inspectorId === piniaInspectorId) {
-          const stores = Array.from(getRegisteredStores())
+          const stores = Array.from(stores)
           const store = stores.find((store) => store.$id === payload.nodeId)
 
           if (store) {
index 8fc6e85febbb8076e586c29082328ea20524ce93..2db614d86392d3d95c3dd2f815425b0af7708363 100644 (file)
@@ -13,4 +13,6 @@ export {
   StoreWithActions,
   StoreWithState,
 } from './types'
+
+// TODO: remove in beta
 export { createStore } from './deprecated'
index a217044afb790c35b999240a9f428d80e27e3dcd..9ca316aa098c551be5b4848dfa9651225231ab27 100644 (file)
@@ -1,11 +1,6 @@
 import { App, InjectionKey, Plugin, Ref, ref, warn } from 'vue'
 import { IS_CLIENT } from './env'
-import {
-  StateTree,
-  GenericStore,
-  StoreWithState,
-  StateDescriptor,
-} from './types'
+import { StateTree, StoreWithState, StateDescriptor } from './types'
 
 /**
  * setActivePinia must be called to handle SSR at the top of functions like
@@ -99,7 +94,8 @@ declare module '@vue/runtime-core' {
 
 export const piniaSymbol = (__DEV__
   ? Symbol('pinia')
-  : Symbol()) as InjectionKey<Pinia>
+  : /* istanbul ignore next */
+    Symbol()) as InjectionKey<Pinia>
 
 /**
  * Creates a Pinia instance to be used by the application
@@ -132,6 +128,7 @@ export function createPinia(): Pinia {
     },
 
     use(plugin) {
+      /* istanbul ignore next */
       if (__DEV__) {
         console.warn(
           `[🍍]: The plugin API has plans to change to bring better extensibility. "pinia.use()" signature will change in the next release. It is recommended to avoid using this API.`
@@ -156,14 +153,3 @@ export function createPinia(): Pinia {
  * Properties that are added to every store by `pinia.use()`
  */
 export interface PiniaCustomProperties {}
-
-/**
- * Registered stores used for devtools. TODO: move to devtools
- */
-export const stores = /*#__PURE__*/ new Set<GenericStore>()
-
-export function registerStore(store: GenericStore) {
-  stores.add(store)
-}
-
-export const getRegisteredStores = () => stores
index 68669ba5978289b3539cb778eef82c0c8d7fee1b..13fa213bae2dfaae31f7b846deaa2b9af14f1917 100644 (file)
@@ -282,6 +282,7 @@ export function defineStore<
         __DEV__ /*|| __FEATURE_PROD_DEVTOOLS__*/
       ) {
         const app = getClientApp()
+        /* istanbul ignore else */
         if (app) {
           addDevtools(app, store)
         } else if (!isDevWarned && !__TEST__) {
index dc9addd9aeb6e6319fa01bef68cf65ea1c1e1988..b97aa5dd3f80b3c844534f5ad72cd2c1073f8c1e 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
@@ -3800,6 +3800,11 @@ jest-message-util@^26.6.2:
     slash "^3.0.0"
     stack-utils "^2.0.2"
 
+jest-mock-warn@^1.1.0:
+  version "1.1.0"
+  resolved "https://registry.yarnpkg.com/jest-mock-warn/-/jest-mock-warn-1.1.0.tgz#cb1c8beaa6c6236da2be0f170fd5e6f595adb67b"
+  integrity sha512-Q0EjGIUowgcuH7K1v6KgZ/WtqQaA9kc/TxayKaZKKeTGBn9nC4uKI65nt0O3l8opaPi2VSvG18WcLPEqzowxrQ==
+
 jest-mock@^26.6.2:
   version "26.6.2"
   resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-26.6.2.tgz#d6cb712b041ed47fe0d9b6fc3474bc6543feb302"