@@ -686,59 +686,78 @@ describe(`Query Collections`, () => {
686686 } )
687687 } )
688688
689- it ( `optimistic state is dropped after commit` , ( ) => {
690- cleanup = $effect . root ( ( ) => {
691- const emitter = mitt ( )
692- // Track renders and states
693- const renderStates : Array < {
694- stateSize : number
695- hasTempKey : boolean
696- hasPermKey : boolean
697- timestamp : number
698- } > = [ ]
699-
700- // Create person collection
701- const personCollection = new Collection < Person > ( {
702- id : `person-collection-test-bug` ,
703- sync : {
704- sync : ( { begin, write, commit } ) => {
705- // @ts -expect-error Mitt typing doesn't match our usage
706- emitter . on ( `sync-person` , ( changes : Array < PendingMutation > ) => {
707- begin ( )
708- changes . forEach ( ( change ) => {
709- write ( {
710- key : change . key ,
711- type : change . type ,
712- value : change . changes as Person ,
713- } )
689+ it ( `optimistic state is dropped after commit` , async ( ) => {
690+ const emitter = mitt ( )
691+ // Track renders and states
692+ const renderStates : Array < {
693+ stateSize : number
694+ hasTempKey : boolean
695+ hasPermKey : boolean
696+ timestamp : number
697+ } > = [ ]
698+
699+ // Create person collection
700+ const personCollection = new Collection < Person > ( {
701+ id : `person-collection-test-bug` ,
702+ sync : {
703+ sync : ( { begin, write, commit } ) => {
704+ // @ts -expect-error Mitt typing doesn't match our usage
705+ emitter . on ( `sync-person` , ( changes : Array < PendingMutation > ) => {
706+ begin ( )
707+ changes . forEach ( ( change ) => {
708+ write ( {
709+ key : change . key ,
710+ type : change . type ,
711+ value : change . changes as Person ,
714712 } )
715- commit ( )
716713 } )
717- } ,
714+ commit ( )
715+ } )
718716 } ,
719- } )
717+ } ,
718+ } )
720719
721- // Create issue collection
722- const issueCollection = new Collection < Issue > ( {
723- id : `issue-collection-test-bug` ,
724- sync : {
725- sync : ( { begin, write, commit } ) => {
726- // @ts -expect-error Mitt typing doesn't match our usage
727- emitter . on ( `sync-issue` , ( changes : Array < PendingMutation > ) => {
728- begin ( )
729- changes . forEach ( ( change ) => {
730- write ( {
731- key : change . key ,
732- type : change . type ,
733- value : change . changes as Issue ,
734- } )
720+ // Create issue collection
721+ const issueCollection = new Collection < Issue > ( {
722+ id : `issue-collection-test-bug` ,
723+ sync : {
724+ sync : ( { begin, write, commit } ) => {
725+ // @ts -expect-error Mitt typing doesn't match our usage
726+ emitter . on ( `sync-issue` , ( changes : Array < PendingMutation > ) => {
727+ begin ( )
728+ changes . forEach ( ( change ) => {
729+ write ( {
730+ key : change . key ,
731+ type : change . type ,
732+ value : change . changes as Issue ,
735733 } )
736- commit ( )
737734 } )
738- } ,
735+ commit ( )
736+ } )
739737 } ,
740- } )
738+ } ,
739+ } )
740+
741+ // Create a transaction to perform an optimistic mutation
742+ const tx = createTransaction ( {
743+ mutationFn : async ( ) => {
744+ emitter . emit ( `sync-issue` , [
745+ {
746+ key : `4` ,
747+ type : `insert` ,
748+ changes : {
749+ id : `4` ,
750+ title : `New Issue` ,
751+ description : `New Issue Description` ,
752+ userId : `1` ,
753+ } ,
754+ } ,
755+ ] )
756+ return Promise . resolve ( )
757+ } ,
758+ } )
741759
760+ cleanup = $effect . root ( ( ) => {
742761 // Sync initial person data
743762 emitter . emit (
744763 `sync-person` ,
@@ -794,25 +813,6 @@ describe(`Query Collections`, () => {
794813 // Reset render states array for clarity in the remaining test
795814 renderStates . length = 0
796815
797- // Create a transaction to perform an optimistic mutation
798- const tx = createTransaction ( {
799- mutationFn : async ( ) => {
800- emitter . emit ( `sync-issue` , [
801- {
802- key : `4` ,
803- type : `insert` ,
804- changes : {
805- id : `4` ,
806- title : `New Issue` ,
807- description : `New Issue Description` ,
808- userId : `1` ,
809- } ,
810- } ,
811- ] )
812- return Promise . resolve ( )
813- } ,
814- } )
815-
816816 // Perform optimistic insert of a new issue
817817 tx . mutate ( ( ) =>
818818 issueCollection . insert (
@@ -833,27 +833,41 @@ describe(`Query Collections`, () => {
833833 name : `John Doe` ,
834834 title : `New Issue` ,
835835 } )
836+ } )
836837
837- // Wait for the transaction to be committed
838- // await tx.isPersisted.promise
839- // flushSync()
838+ // Wait for the transaction to be committed
839+ await tx . isPersisted . promise
840+ flushSync ( )
840841
841- // // Check if we had any render where the temp key was removed but the permanent key wasn't added yet
842- // const hadFlicker = renderStates.some(
843- // (state) =>
844- // !state.hasTempKey && !state.hasPermKey && state.stateSize === 3
845- // )
842+ $effect . root ( ( ) => {
843+ const result = useLiveQuery ( ( q ) =>
844+ q
845+ . from ( { issues : issueCollection } )
846+ . join ( {
847+ type : `inner` ,
848+ from : { persons : personCollection } ,
849+ on : [ `@persons.id` , `=` , `@issues.userId` ] ,
850+ } )
851+ . select ( `@issues.id` , `@issues.title` , `@persons.name` )
852+ . keyBy ( `@id` )
853+ )
846854
847- // expect(hadFlicker).toBe(false)
855+ // Check if we had any render where the temp key was removed but the permanent key wasn't added yet
856+ const hadFlicker = renderStates . some (
857+ ( state ) =>
858+ ! state . hasTempKey && ! state . hasPermKey && state . stateSize === 3
859+ )
848860
849- // // Verify the temporary key is replaced by the permanent one
850- // expect(result.state.size).toBe(4)
851- // expect(result.state.get(`temp-key`)).toBeUndefined()
852- // expect(result.state.get(`4`)).toEqual({
853- // id: `4`,
854- // name: `John Doe`,
855- // title: `New Issue`,
856- // })
861+ expect ( hadFlicker ) . toBe ( false )
862+
863+ // Verify the temporary key is replaced by the permanent one
864+ expect ( result . state . size ) . toBe ( 4 )
865+ expect ( result . state . get ( `temp-key` ) ) . toBeUndefined ( )
866+ expect ( result . state . get ( `4` ) ) . toEqual ( {
867+ id : `4` ,
868+ name : `John Doe` ,
869+ title : `New Issue` ,
870+ } )
857871 } )
858872 } )
859873} )
0 commit comments