@@ -3,6 +3,7 @@ import {delay, filter, firstValueFrom, Observable, of, Subject} from 'rxjs'
33import { beforeEach , describe , expect , it , vi } from 'vitest'
44
55import { getClientState } from '../client/clientStore'
6+ import { sourceFor as getSource } from '../config/sanityConfig'
67import { createSanityInstance , type SanityInstance } from '../store/createSanityInstance'
78import { type StateSource } from '../store/createStateSourceAction'
89import { getQueryState , resolveQuery } from './queryStore'
@@ -17,6 +18,7 @@ vi.mock('../client/clientStore', () => ({
1718} ) )
1819
1920describe ( 'queryStore' , ( ) => {
21+ const source = getSource ( { projectId : 'test' , dataset : 'test' } )
2022 let instance : SanityInstance
2123 let liveEvents : Subject < LiveEvent >
2224 let fetch : SanityClient [ 'observable' ] [ 'fetch' ]
@@ -61,7 +63,7 @@ describe('queryStore', () => {
6163
6264 it ( 'initializes query state and cleans up after unsubscribe' , async ( ) => {
6365 const query = '*[_type == "movie"]'
64- const state = getQueryState ( instance , { query} )
66+ const state = getQueryState ( instance , { query, source , perspective : 'drafts' } )
6567
6668 // Initially undefined before subscription
6769 expect ( state . getCurrent ( ) ) . toBeUndefined ( )
@@ -90,7 +92,7 @@ describe('queryStore', () => {
9092
9193 it ( 'maintains state when multiple subscribers exist' , async ( ) => {
9294 const query = '*[_type == "movie"]'
93- const state = getQueryState ( instance , { query} )
95+ const state = getQueryState ( instance , { query, source , perspective : 'drafts' } )
9496
9597 // Add two subscribers
9698 const unsubscribe1 = state . subscribe ( )
@@ -127,13 +129,13 @@ describe('queryStore', () => {
127129 it ( 'resolveQuery works without affecting subscriber cleanup' , async ( ) => {
128130 const query = '*[_type == "movie"]'
129131
130- const state = getQueryState ( instance , { query} )
132+ const state = getQueryState ( instance , { query, source , perspective : 'drafts' } )
131133
132134 // Check that getQueryState starts undefined
133135 expect ( state . getCurrent ( ) ) . toBeUndefined ( )
134136
135137 // Use resolveQuery which should not add a subscriber
136- const result = await resolveQuery ( instance , { query} )
138+ const result = await resolveQuery ( instance , { query, source , perspective : 'drafts' } )
137139 expect ( result ) . toEqual ( [
138140 { _id : 'movie1' , _type : 'movie' , title : 'Movie 1' } ,
139141 { _id : 'movie2' , _type : 'movie' , title : 'Movie 2' } ,
@@ -160,7 +162,12 @@ describe('queryStore', () => {
160162 const abortController = new AbortController ( )
161163
162164 // Create a promise that will reject when aborted
163- const queryPromise = resolveQuery ( instance , { query, signal : abortController . signal } )
165+ const queryPromise = resolveQuery ( instance , {
166+ query,
167+ source,
168+ perspective : 'drafts' ,
169+ signal : abortController . signal ,
170+ } )
164171
165172 // Abort the request
166173 abortController . abort ( )
@@ -169,7 +176,9 @@ describe('queryStore', () => {
169176 await expect ( queryPromise ) . rejects . toThrow ( 'The operation was aborted.' )
170177
171178 // Verify state is cleared after abort
172- expect ( getQueryState ( instance , { query} ) . getCurrent ( ) ) . toBeUndefined ( )
179+ expect (
180+ getQueryState ( instance , { query, source, perspective : 'drafts' } ) . getCurrent ( ) ,
181+ ) . toBeUndefined ( )
173182 } )
174183
175184 it ( 'refetches query when receiving live event with matching sync tag' , async ( ) => {
@@ -188,7 +197,11 @@ describe('queryStore', () => {
188197 )
189198
190199 const query = '*[_type == "movie"]'
191- const state = getQueryState < { _id : string ; _type : string ; title : string } [ ] > ( instance , { query} )
200+ const state = getQueryState < { _id : string ; _type : string ; title : string } [ ] > ( instance , {
201+ query,
202+ source,
203+ perspective : 'drafts' ,
204+ } )
192205
193206 const unsubscribe = state . subscribe ( )
194207 await firstValueFrom ( state . observable . pipe ( filter ( ( i ) => i !== undefined ) ) )
@@ -219,7 +232,7 @@ describe('queryStore', () => {
219232 )
220233
221234 const query = '*[_type == "movie"]'
222- const state = getQueryState ( instance , { query} )
235+ const state = getQueryState ( instance , { query, source , perspective : 'drafts' } )
223236
224237 const unsubscribe = state . subscribe ( )
225238 await firstValueFrom ( state . observable . pipe ( filter ( ( i ) => i !== undefined ) ) )
@@ -252,7 +265,7 @@ describe('queryStore', () => {
252265 )
253266
254267 const query = '*[_type == "movie"]'
255- const state = getQueryState ( instance , { query} )
268+ const state = getQueryState ( instance , { query, source , perspective : 'drafts' } )
256269
257270 const unsubscribe = state . subscribe ( )
258271 await firstValueFrom ( state . observable . pipe ( filter ( ( i ) => i !== undefined ) ) )
@@ -289,7 +302,7 @@ describe('queryStore', () => {
289302 )
290303
291304 const query = '*[_type == "movie"]'
292- const state = getQueryState ( instance , { query} )
305+ const state = getQueryState ( instance , { query, source , perspective : 'drafts' } )
293306 const unsubscribe = state . subscribe ( )
294307
295308 // Verify error is thrown when accessing state
@@ -300,7 +313,7 @@ describe('queryStore', () => {
300313
301314 it ( 'delays query state removal after unsubscribe' , async ( ) => {
302315 const query = '*[_type == "movie"]'
303- const state = getQueryState ( instance , { query} )
316+ const state = getQueryState ( instance , { query, source , perspective : 'drafts' } )
304317 const unsubscribe = state . subscribe ( )
305318
306319 await firstValueFrom ( state . observable . pipe ( filter ( ( i ) => i !== undefined ) ) )
@@ -316,7 +329,7 @@ describe('queryStore', () => {
316329
317330 it ( 'preserves query state if a new subscriber subscribes before cleanup delay' , async ( ) => {
318331 const query = '*[_type == "movie"]'
319- const state = getQueryState ( instance , { query} )
332+ const state = getQueryState ( instance , { query, source , perspective : 'drafts' } )
320333 const unsubscribe1 = state . subscribe ( )
321334
322335 await firstValueFrom ( state . observable . pipe ( filter ( ( i ) => i !== undefined ) ) )
@@ -352,22 +365,16 @@ describe('queryStore', () => {
352365 SanityClient [ 'observable' ] [ 'fetch' ]
353366 >
354367 } ) as SanityClient [ 'observable' ] [ 'fetch' ] )
355-
356- const draftsInstance = createSanityInstance ( {
357- projectId : 'test ' ,
358- dataset : 'test' ,
368+ // Same query/options, different implicit perspectives via instance.config
369+ const sDrafts = getQueryState < { _id : string } [ ] > ( instance , {
370+ query : '*[_type == "movie"] ' ,
371+ source ,
359372 perspective : 'drafts' ,
360373 } )
361- const publishedInstance = createSanityInstance ( {
362- projectId : 'test' ,
363- dataset : 'test' ,
364- perspective : 'published' ,
365- } )
366-
367- // Same query/options, different implicit perspectives via instance.config
368- const sDrafts = getQueryState < { _id : string } [ ] > ( draftsInstance , { query : '*[_type == "movie"]' } )
369- const sPublished = getQueryState < { _id : string } [ ] > ( publishedInstance , {
374+ const sPublished = getQueryState < { _id : string } [ ] > ( instance , {
370375 query : '*[_type == "movie"]' ,
376+ source,
377+ perspective : 'published' ,
371378 } )
372379
373380 const unsubDrafts = sDrafts . subscribe ( )
@@ -385,9 +392,6 @@ describe('queryStore', () => {
385392
386393 unsubDrafts ( )
387394 unsubPublished ( )
388-
389- draftsInstance . dispose ( )
390- publishedInstance . dispose ( )
391395 } )
392396
393397 it ( 'separates cache entries by explicit perspective in options' , async ( ) => {
@@ -403,10 +407,12 @@ describe('queryStore', () => {
403407
404408 const sDrafts = getQueryState < { _id : string } [ ] > ( base , {
405409 query : '*[_type == "movie"]' ,
410+ source,
406411 perspective : 'drafts' ,
407412 } )
408413 const sPublished = getQueryState < { _id : string } [ ] > ( base , {
409414 query : '*[_type == "movie"]' ,
415+ source,
410416 perspective : 'published' ,
411417 } )
412418
0 commit comments