]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: add plugins test
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 3 Mar 2021 16:29:16 +0000 (17:29 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 8 Mar 2021 14:50:05 +0000 (15:50 +0100)
__tests__/storePlugins.spec.ts [new file with mode: 0644]

diff --git a/__tests__/storePlugins.spec.ts b/__tests__/storePlugins.spec.ts
new file mode 100644 (file)
index 0000000..fb62ccf
--- /dev/null
@@ -0,0 +1,49 @@
+import { createPinia, defineStore, PiniaPlugin } from '../src'
+import { createLocalVue, mount } from '@vue/test-utils'
+import Vue from 'vue'
+
+declare module '../src' {
+  export interface PiniaCustomProperties {
+    n: number
+    // uid: App['_uid']
+    hasPinia: boolean
+  }
+}
+
+describe('store plugins', () => {
+  const useStore = defineStore({ id: 'test' })
+  const localVue = createLocalVue()
+  localVue.use(PiniaPlugin)
+
+  it('adds properties to stores', () => {
+    const pinia = createPinia()
+    pinia.Vue = Vue
+    mount({ template: '<p/>' }, { localVue })
+
+    // must call use after installing the plugin
+    pinia.use(() => {
+      return { n: 20 }
+    })
+
+    const store = useStore(pinia)
+
+    expect(store.n).toBe(20)
+  })
+
+  it('can install plugins before installing pinia', () => {
+    const pinia = createPinia()
+    pinia.Vue = Vue
+
+    pinia.use(() => ({ n: 1 }))
+    pinia.use((pinia) => ({ hasPinia: !!pinia }))
+
+    mount({ template: '<p/>' }, { localVue })
+
+    pinia.use((app) => ({ hasPinia: !!app }))
+
+    const store = useStore(pinia)
+
+    expect(store.n).toBe(1)
+    expect(store.hasPinia).toBe(true)
+  })
+})