|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { createPinia, defineStore, StoreDefinition } from '../src' |
| 3 | +import { mount } from '@vue/test-utils' |
| 4 | +import { ref } from 'vue' |
| 5 | + |
| 6 | +// Extend the types to test the new functionality |
| 7 | +declare module '../src' { |
| 8 | + export interface DefineStoreOptionsBase<S, Store> { |
| 9 | + stores?: Record<string, StoreDefinition> |
| 10 | + customOption?: string |
| 11 | + debounce?: Record<string, number> |
| 12 | + } |
| 13 | + |
| 14 | + export interface PiniaCustomProperties<Id, S, G, A> { |
| 15 | + readonly stores: any |
| 16 | + readonly customOption: any |
| 17 | + readonly debounce: any |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +describe('Store Options Access', () => { |
| 22 | + it('allows plugins to access custom store options with proper typing', async () => { |
| 23 | + // Create some stores to be used in the stores option |
| 24 | + const useCounterStore = defineStore('counter', { |
| 25 | + state: () => ({ count: 0 }), |
| 26 | + actions: { |
| 27 | + increment() { |
| 28 | + this.count++ |
| 29 | + }, |
| 30 | + }, |
| 31 | + }) |
| 32 | + |
| 33 | + const useUserStore = defineStore('user', { |
| 34 | + state: () => ({ name: 'John' }), |
| 35 | + actions: { |
| 36 | + setName(name: string) { |
| 37 | + this.name = name |
| 38 | + }, |
| 39 | + }, |
| 40 | + }) |
| 41 | + |
| 42 | + // Create a store with custom options |
| 43 | + const useMainStore = defineStore('main', { |
| 44 | + state: () => ({ value: 0 }), |
| 45 | + actions: { |
| 46 | + setValue(val: number) { |
| 47 | + this.value = val |
| 48 | + }, |
| 49 | + }, |
| 50 | + // Custom options that should be accessible in plugins |
| 51 | + stores: { |
| 52 | + counter: useCounterStore, |
| 53 | + user: useUserStore, |
| 54 | + }, |
| 55 | + customOption: 'test-value', |
| 56 | + debounce: { |
| 57 | + setValue: 300, |
| 58 | + }, |
| 59 | + }) |
| 60 | + |
| 61 | + const pinia = createPinia() |
| 62 | + mount({ template: 'none' }, { global: { plugins: [pinia] } }) |
| 63 | + |
| 64 | + let mainStorePluginContext: any = null |
| 65 | + |
| 66 | + // Plugin that accesses the custom options |
| 67 | + pinia.use((context) => { |
| 68 | + // Only capture the context for the main store (which has custom options) |
| 69 | + if (context.store.$id === 'main') { |
| 70 | + mainStorePluginContext = context |
| 71 | + } |
| 72 | + |
| 73 | + // Access the stores option from context.options |
| 74 | + const storesOption = context.options.stores |
| 75 | + const customOptionValue = context.options.customOption |
| 76 | + const debounceOption = context.options.debounce |
| 77 | + |
| 78 | + return { |
| 79 | + get stores() { |
| 80 | + if (!storesOption) return {} |
| 81 | + return Object.freeze( |
| 82 | + Object.entries(storesOption).reduce<Record<string, any>>( |
| 83 | + (acc, [name, definition]) => { |
| 84 | + acc[name] = definition() |
| 85 | + return acc |
| 86 | + }, |
| 87 | + {} |
| 88 | + ) |
| 89 | + ) |
| 90 | + }, |
| 91 | + get customOption() { |
| 92 | + return customOptionValue |
| 93 | + }, |
| 94 | + get debounce() { |
| 95 | + return debounceOption |
| 96 | + }, |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + const store = useMainStore() |
| 101 | + |
| 102 | + // Verify that the plugin context has access to the options for the main store |
| 103 | + expect(mainStorePluginContext).toBeTruthy() |
| 104 | + expect(mainStorePluginContext.options.stores).toBeDefined() |
| 105 | + expect(mainStorePluginContext.options.stores.counter).toBe(useCounterStore) |
| 106 | + expect(mainStorePluginContext.options.stores.user).toBe(useUserStore) |
| 107 | + expect(mainStorePluginContext.options.customOption).toBe('test-value') |
| 108 | + expect(mainStorePluginContext.options.debounce).toEqual({ setValue: 300 }) |
| 109 | + |
| 110 | + // Verify that the store has access to the custom properties |
| 111 | + expect(store.stores).toBeDefined() |
| 112 | + expect(store.stores.counter).toBeDefined() |
| 113 | + expect(store.stores.user).toBeDefined() |
| 114 | + expect(store.customOption).toBe('test-value') |
| 115 | + expect(store.debounce).toEqual({ setValue: 300 }) |
| 116 | + |
| 117 | + // Verify that the stores are properly instantiated |
| 118 | + expect(store.stores.counter.count).toBe(0) |
| 119 | + expect(store.stores.user.name).toBe('John') |
| 120 | + |
| 121 | + // Test that the stores work correctly |
| 122 | + store.stores.counter.increment() |
| 123 | + expect(store.stores.counter.count).toBe(1) |
| 124 | + |
| 125 | + store.stores.user.setName('Jane') |
| 126 | + expect(store.stores.user.name).toBe('Jane') |
| 127 | + }) |
| 128 | + |
| 129 | + it('works with setup stores', async () => { |
| 130 | + const useHelperStore = defineStore('helper', () => { |
| 131 | + const value = ref(42) |
| 132 | + return { value } |
| 133 | + }) |
| 134 | + |
| 135 | + const useSetupStore = defineStore( |
| 136 | + 'setup', |
| 137 | + () => { |
| 138 | + const count = ref(0) |
| 139 | + const increment = () => count.value++ |
| 140 | + return { count, increment } |
| 141 | + }, |
| 142 | + { |
| 143 | + stores: { |
| 144 | + helper: useHelperStore, |
| 145 | + }, |
| 146 | + customOption: 'setup-test', |
| 147 | + } |
| 148 | + ) |
| 149 | + |
| 150 | + const pinia = createPinia() |
| 151 | + mount({ template: 'none' }, { global: { plugins: [pinia] } }) |
| 152 | + |
| 153 | + let setupStorePluginContext: any = null |
| 154 | + |
| 155 | + pinia.use((context) => { |
| 156 | + // Only capture the context for the setup store (which has custom options) |
| 157 | + if (context.store.$id === 'setup') { |
| 158 | + setupStorePluginContext = context |
| 159 | + } |
| 160 | + |
| 161 | + const storesOption = context.options.stores |
| 162 | + const customOptionValue = context.options.customOption |
| 163 | + |
| 164 | + return { |
| 165 | + get stores() { |
| 166 | + if (!storesOption) return {} |
| 167 | + return Object.freeze( |
| 168 | + Object.entries(storesOption).reduce<Record<string, any>>( |
| 169 | + (acc, [name, definition]) => { |
| 170 | + acc[name] = definition() |
| 171 | + return acc |
| 172 | + }, |
| 173 | + {} |
| 174 | + ) |
| 175 | + ) |
| 176 | + }, |
| 177 | + get customOption() { |
| 178 | + return customOptionValue |
| 179 | + }, |
| 180 | + } |
| 181 | + }) |
| 182 | + |
| 183 | + const store = useSetupStore() |
| 184 | + |
| 185 | + // Verify plugin context |
| 186 | + expect(setupStorePluginContext.options.stores).toBeDefined() |
| 187 | + expect(setupStorePluginContext.options.stores.helper).toBe(useHelperStore) |
| 188 | + expect(setupStorePluginContext.options.customOption).toBe('setup-test') |
| 189 | + |
| 190 | + // Verify store properties |
| 191 | + expect(store.stores).toBeDefined() |
| 192 | + expect(store.stores.helper).toBeDefined() |
| 193 | + expect(store.stores.helper.value).toBe(42) |
| 194 | + expect(store.customOption).toBe('setup-test') |
| 195 | + }) |
| 196 | + |
| 197 | + it('handles stores without custom options', async () => { |
| 198 | + const useSimpleStore = defineStore('simple', { |
| 199 | + state: () => ({ value: 1 }), |
| 200 | + }) |
| 201 | + |
| 202 | + const pinia = createPinia() |
| 203 | + mount({ template: 'none' }, { global: { plugins: [pinia] } }) |
| 204 | + |
| 205 | + pinia.use((context) => { |
| 206 | + const storesOption = context.options.stores |
| 207 | + const customOptionValue = context.options.customOption |
| 208 | + |
| 209 | + return { |
| 210 | + get stores() { |
| 211 | + return storesOption |
| 212 | + ? Object.freeze( |
| 213 | + Object.entries(storesOption).reduce<Record<string, any>>( |
| 214 | + (acc, [name, definition]) => { |
| 215 | + acc[name] = definition() |
| 216 | + return acc |
| 217 | + }, |
| 218 | + {} |
| 219 | + ) |
| 220 | + ) |
| 221 | + : {} |
| 222 | + }, |
| 223 | + get customOption() { |
| 224 | + return customOptionValue |
| 225 | + }, |
| 226 | + } |
| 227 | + }) |
| 228 | + |
| 229 | + const store = useSimpleStore() |
| 230 | + |
| 231 | + // Should have empty stores and undefined customOption |
| 232 | + expect(store.stores).toEqual({}) |
| 233 | + expect(store.customOption).toBeUndefined() |
| 234 | + }) |
| 235 | + |
| 236 | + it('maintains backward compatibility', async () => { |
| 237 | + const useCompatStore = defineStore('compat', { |
| 238 | + state: () => ({ count: 0 }), |
| 239 | + actions: { |
| 240 | + increment() { |
| 241 | + this.count++ |
| 242 | + }, |
| 243 | + }, |
| 244 | + }) |
| 245 | + |
| 246 | + const pinia = createPinia() |
| 247 | + mount({ template: 'none' }, { global: { plugins: [pinia] } }) |
| 248 | + |
| 249 | + // Plugin that doesn't use the new functionality |
| 250 | + pinia.use(({ store }) => { |
| 251 | + return { |
| 252 | + pluginProperty: 'test', |
| 253 | + } as any |
| 254 | + }) |
| 255 | + |
| 256 | + const store = useCompatStore() |
| 257 | + |
| 258 | + // Should work as before |
| 259 | + expect((store as any).pluginProperty).toBe('test') |
| 260 | + expect(store.count).toBe(0) |
| 261 | + store.increment() |
| 262 | + expect(store.count).toBe(1) |
| 263 | + }) |
| 264 | +}) |
0 commit comments