@@ -5,15 +5,17 @@ import {
55 and ,
66 createLiveQueryCollection ,
77 eq ,
8+ ilike ,
89 liveQueryCollectionOptions ,
910} from "../../src/query/index.js"
1011import { Query } from "../../src/query/builder/index.js"
1112import {
13+ flushPromises ,
1214 mockSyncCollectionOptions ,
1315 mockSyncCollectionOptionsNoInitialState ,
1416} from "../utils.js"
1517import { createDeferred } from "../../src/deferred"
16- import type { ChangeMessage } from "../../src/types.js"
18+ import type { ChangeMessage , LoadSubsetOptions } from "../../src/types.js"
1719
1820// Sample user type for tests
1921type User = {
@@ -1939,4 +1941,105 @@ describe(`createLiveQueryCollection`, () => {
19391941 throw new Error ( `Expected DuplicateKeySyncError to be thrown` )
19401942 } )
19411943 } )
1944+
1945+ describe ( `where clauses passed to loadSubset` , ( ) => {
1946+ it ( `passes eq where clause to loadSubset` , async ( ) => {
1947+ const capturedOptions : Array < LoadSubsetOptions > = [ ]
1948+ let resolveLoadSubset : ( ) => void
1949+ const loadSubsetPromise = new Promise < void > ( ( resolve ) => {
1950+ resolveLoadSubset = resolve
1951+ } )
1952+
1953+ const baseCollection = createCollection < { id : number ; name : string } > ( {
1954+ id : `test-base` ,
1955+ getKey : ( item ) => item . id ,
1956+ syncMode : `on-demand` ,
1957+ sync : {
1958+ sync : ( { markReady } ) => {
1959+ markReady ( )
1960+ return {
1961+ loadSubset : ( options : LoadSubsetOptions ) => {
1962+ capturedOptions . push ( options )
1963+ return loadSubsetPromise
1964+ } ,
1965+ }
1966+ } ,
1967+ } ,
1968+ } )
1969+
1970+ // Create a live query collection with a where clause
1971+ // This will go through convertToBasicExpression
1972+ const liveQueryCollection = createLiveQueryCollection ( ( q ) =>
1973+ q . from ( { item : baseCollection } ) . where ( ( { item } ) => eq ( item . id , 2 ) )
1974+ )
1975+
1976+ // Trigger sync which will call loadSubset
1977+ await liveQueryCollection . preload ( )
1978+ await flushPromises ( )
1979+
1980+ expect ( capturedOptions . length ) . toBeGreaterThan ( 0 )
1981+ const lastCall = capturedOptions [ capturedOptions . length - 1 ]
1982+ expect ( lastCall ?. where ) . toBeDefined ( )
1983+ // The where clause should be normalized (alias removed), so it should be eq(ref(['id']), 2)
1984+ expect ( lastCall ?. where ?. type ) . toBe ( `func` )
1985+ if ( lastCall ?. where ?. type === `func` ) {
1986+ expect ( lastCall . where . name ) . toBe ( `eq` )
1987+ }
1988+
1989+ resolveLoadSubset ! ( )
1990+ await flushPromises ( )
1991+ } )
1992+
1993+ it ( `passes ilike where clause to loadSubset` , async ( ) => {
1994+ const capturedOptions : Array < LoadSubsetOptions > = [ ]
1995+ let resolveLoadSubset : ( ) => void
1996+ const loadSubsetPromise = new Promise < void > ( ( resolve ) => {
1997+ resolveLoadSubset = resolve
1998+ } )
1999+
2000+ const baseCollection = createCollection < { id : number ; name : string } > ( {
2001+ id : `test-base` ,
2002+ getKey : ( item ) => item . id ,
2003+ syncMode : `on-demand` ,
2004+ sync : {
2005+ sync : ( { markReady } ) => {
2006+ markReady ( )
2007+ return {
2008+ loadSubset : ( options : LoadSubsetOptions ) => {
2009+ capturedOptions . push ( options )
2010+ return loadSubsetPromise
2011+ } ,
2012+ }
2013+ } ,
2014+ } ,
2015+ } )
2016+
2017+ // Create a live query collection with an ilike where clause
2018+ // This will go through convertToBasicExpression
2019+ const liveQueryCollection = createLiveQueryCollection ( ( q ) =>
2020+ q
2021+ . from ( { item : baseCollection } )
2022+ . where ( ( { item } ) => ilike ( item . name , `%test%` ) )
2023+ )
2024+
2025+ // Trigger sync which will call loadSubset
2026+ await liveQueryCollection . preload ( )
2027+ await flushPromises ( )
2028+
2029+ expect ( capturedOptions . length ) . toBeGreaterThan ( 0 )
2030+ const lastCall = capturedOptions [ capturedOptions . length - 1 ]
2031+ // Without the fix: where would be undefined/null
2032+ // With the fix: where should be defined with the ilike expression
2033+ expect ( lastCall ?. where ) . toBeDefined ( )
2034+ expect ( lastCall ?. where ) . not . toBeNull ( )
2035+ // The where clause should be normalized (alias removed), so it should be ilike(ref(['name']), '%test%')
2036+ expect ( lastCall ?. where ?. type ) . toBe ( `func` )
2037+ if ( lastCall ?. where ?. type === `func` ) {
2038+ expect ( lastCall . where . name ) . toBe ( `ilike` )
2039+ }
2040+
2041+ resolveLoadSubset ! ( )
2042+ await flushPromises ( )
2043+ } )
2044+ } )
19422045} )
0 commit comments