@@ -82,6 +82,114 @@ describe('BatchLoader', () => {
8282 expect ( batchFetchSpy ) . toHaveBeenNthCalledWith ( 2 , [ 'c' ] )
8383 } )
8484
85+ it ( 'should work optimisticUpdate' , async ( ) => {
86+ let salt = '1'
87+
88+ const options : IBatchLoaderOptions < string , { test : string } > = {
89+ batchFetch : ( ids ) => new Promise ( ( resolve ) => {
90+ setTimeout ( ( ) => {
91+ resolve (
92+ ids . map ( ( id ) => ( {
93+ test : `test_${ id } _${ salt } ` ,
94+ } ) ) ,
95+ )
96+ } , TEST_TIMEOUT )
97+ } ) ,
98+ refetchStrategy : 'refresh' ,
99+ }
100+
101+ const batchFetchSpy = jest . spyOn ( options , 'batchFetch' )
102+ const testBatchLoader = new BatchLoader < string , { test : string } > ( options )
103+
104+ expect ( testBatchLoader . getStatus ( 'a' ) ) . toStrictEqual ( 'unrequested' )
105+ expect ( testBatchLoader . getState ( 'a' ) ) . toStrictEqual ( {
106+ status : 'unrequested' ,
107+ result : undefined ,
108+ } )
109+
110+ testBatchLoader . optimisticUpdate ( 'a' , { test : 'optimistic_value_before_load_a' } )
111+ testBatchLoader . optimisticUpdate ( 'b' , { test : 'optimistic_value_before_load_b' } )
112+
113+ expect ( testBatchLoader . getState ( 'a' ) ) . toStrictEqual ( {
114+ status : 'resolved' ,
115+ result : { test : 'optimistic_value_before_load_a' } ,
116+ } )
117+
118+ const promiseA1 = testBatchLoader . load ( 'a' )
119+
120+ void testBatchLoader . load ( 'b' )
121+
122+ expect ( testBatchLoader . getState ( 'a' ) ) . toStrictEqual ( {
123+ status : 'scheduled' ,
124+ result : { test : 'optimistic_value_before_load_a' } ,
125+ } )
126+
127+ testBatchLoader . optimisticUpdate ( 'a' , { test : 'optimistic_value_after_scheduled_a' } )
128+
129+ expect ( testBatchLoader . getState ( 'a' ) ) . toStrictEqual ( {
130+ status : 'resolved' ,
131+ result : { test : 'optimistic_value_after_scheduled_a' } ,
132+ } )
133+
134+ await timeout ( )
135+
136+ const promiseA2 = testBatchLoader . load ( 'a' )
137+
138+ expect ( promiseA2 === promiseA1 ) . toStrictEqual ( true )
139+ expect ( testBatchLoader . getState ( 'a' ) ) . toStrictEqual ( {
140+ status : 'fetching' ,
141+ result : { test : 'optimistic_value_after_scheduled_a' } ,
142+ } )
143+ expect ( testBatchLoader . getStatus ( 'a' ) ) . toStrictEqual ( 'fetching' )
144+
145+ testBatchLoader . optimisticUpdate ( 'a' , { test : 'optimistic_value_after_fetching_a' } )
146+
147+ expect ( testBatchLoader . getState ( 'a' ) ) . toStrictEqual ( {
148+ status : 'resolved' ,
149+ result : { test : 'optimistic_value_after_fetching_a' } ,
150+ } )
151+
152+ expect ( await promiseA1 ) . toStrictEqual ( { test : 'test_a_1' } )
153+
154+ expect ( testBatchLoader . getState ( 'a' ) ) . toStrictEqual ( {
155+ status : 'resolved' ,
156+ result : { test : 'test_a_1' } ,
157+ } )
158+
159+ testBatchLoader . optimisticUpdate ( 'a' , { test : 'optimistic_value_after_await_a' } )
160+
161+ expect ( testBatchLoader . getState ( 'a' ) ) . toStrictEqual ( {
162+ status : 'resolved' ,
163+ result : { test : 'optimistic_value_after_await_a' } ,
164+ } )
165+
166+ const promiseA3 = testBatchLoader . load ( 'a' )
167+ const promiseC = testBatchLoader . load ( 'c' )
168+
169+ expect ( promiseA3 === promiseA1 ) . toStrictEqual ( false )
170+ expect ( testBatchLoader . getState ( 'a' ) ) . toStrictEqual ( {
171+ status : 'scheduled' ,
172+ result : { test : 'optimistic_value_after_await_a' } ,
173+ } )
174+ expect ( testBatchLoader . getState ( 'c' ) ) . toStrictEqual ( {
175+ status : 'scheduled' ,
176+ result : undefined ,
177+ } )
178+
179+ salt = '2'
180+
181+ expect ( await promiseC ) . toStrictEqual ( { test : 'test_c_2' } )
182+ expect ( await promiseA3 ) . toStrictEqual ( { test : 'test_a_2' } )
183+ expect ( testBatchLoader . getState ( 'a' ) ) . toStrictEqual ( {
184+ status : 'resolved' ,
185+ result : { test : 'test_a_2' } ,
186+ } )
187+
188+ expect ( batchFetchSpy ) . toHaveBeenCalledTimes ( 2 )
189+ expect ( batchFetchSpy ) . toHaveBeenNthCalledWith ( 1 , [ 'a' , 'b' ] )
190+ expect ( batchFetchSpy ) . toHaveBeenNthCalledWith ( 2 , [ 'a' , 'c' ] )
191+ } )
192+
85193 it ( 'should work when batchFetch throw Error' , async ( ) => {
86194 const options : IBatchLoaderOptions < string , { test : string } > = {
87195 batchFetch : ( ) => Promise . reject ( new Error ( 'test error' ) ) ,
0 commit comments