From: Eduardo San Martin Morote Date: Mon, 8 Mar 2021 13:13:41 +0000 (+0100) Subject: test: refactor X-Git-Tag: v2.0.0-alpha.8~46 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fe885f673923d2f6d511afc10d9e12a95b35df37;p=thirdparty%2Fvuejs%2Fpinia.git test: refactor --- diff --git a/__tests__/rootState.spec.ts b/__tests__/rootState.spec.ts index 6511d37e..3103756d 100644 --- a/__tests__/rootState.spec.ts +++ b/__tests__/rootState.spec.ts @@ -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({}) }) diff --git a/jest.config.js b/jest.config.js index 5c453457..32b6e544 100644 --- a/jest.config.js +++ b/jest.config.js @@ -8,6 +8,7 @@ module.exports = { '/node_modules/', 'src/index.ts', 'src/devtools.ts', + 'src/deprecated.ts', ], testMatch: ['/__tests__/**/*.spec.ts'], globals: { diff --git a/package.json b/package.json index 5f763991..95877bf0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/devtools.ts b/src/devtools.ts index aa7ae352..d6e97242 100644 --- a/src/devtools.ts +++ b/src/devtools.ts @@ -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() + +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) { diff --git a/src/index.ts b/src/index.ts index 8fc6e85f..2db614d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,4 +13,6 @@ export { StoreWithActions, StoreWithState, } from './types' + +// TODO: remove in beta export { createStore } from './deprecated' diff --git a/src/rootStore.ts b/src/rootStore.ts index a217044a..9ca316aa 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -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 + : /* istanbul ignore next */ + Symbol()) as InjectionKey /** * 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() - -export function registerStore(store: GenericStore) { - stores.add(store) -} - -export const getRegisteredStores = () => stores diff --git a/src/store.ts b/src/store.ts index 68669ba5..13fa213b 100644 --- a/src/store.ts +++ b/src/store.ts @@ -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__) { diff --git a/yarn.lock b/yarn.lock index dc9addd9..b97aa5dd 100644 --- 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"