@@ -17,7 +17,6 @@ import {
17
17
} from './helpers'
18
18
import { server } from './mocks/server'
19
19
import { rest } from 'msw'
20
- import * as utils from '../utils'
21
20
22
21
const originalEnv = process . env . NODE_ENV
23
22
beforeAll ( ( ) => void ( ( process . env as any ) . NODE_ENV = 'development' ) )
@@ -765,17 +764,9 @@ test('providesTags and invalidatesTags can use baseQueryMeta', async () => {
765
764
expect ( 'request' in _meta ! && 'response' in _meta ! ) . toBe ( true )
766
765
} )
767
766
768
- describe ( 'strucutralSharing flag behaviors' , ( ) => {
769
- const mockCopyFn = jest . spyOn ( utils , 'copyWithStructuralSharing' )
770
-
771
- beforeEach ( ( ) => {
772
- mockCopyFn . mockClear ( )
773
- } )
774
-
767
+ describe ( 'structuralSharing flag behaviors' , ( ) => {
775
768
type SuccessResponse = { value : 'success' }
776
769
777
- const apiSuccessResponse : SuccessResponse = { value : 'success' }
778
-
779
770
const api = createApi ( {
780
771
baseQuery : fetchBaseQuery ( { baseUrl : 'https://example.com' } ) ,
781
772
tagTypes : [ 'success' ] ,
@@ -793,17 +784,30 @@ describe('strucutralSharing flag behaviors', () => {
793
784
const storeRef = setupApiStore ( api )
794
785
795
786
it ( 'enables structural sharing for query endpoints by default' , async ( ) => {
796
- const result = await storeRef . store . dispatch (
797
- api . endpoints . enabled . initiate ( )
787
+ await storeRef . store . dispatch ( api . endpoints . enabled . initiate ( ) )
788
+ const firstRef = api . endpoints . enabled . select ( ) ( storeRef . store . getState ( ) )
789
+
790
+ await storeRef . store . dispatch (
791
+ api . endpoints . enabled . initiate ( undefined , { forceRefetch : true } )
798
792
)
799
- expect ( mockCopyFn ) . toHaveBeenCalledTimes ( 1 )
800
- expect ( result . data ) . toMatchObject ( apiSuccessResponse )
793
+
794
+ const secondRef = api . endpoints . enabled . select ( ) ( storeRef . store . getState ( ) )
795
+
796
+ expect ( firstRef . requestId ) . not . toEqual ( secondRef . requestId )
797
+ expect ( firstRef . data === secondRef . data ) . toBeTruthy ( )
801
798
} )
799
+
802
800
it ( 'allows a query endpoint to opt-out of structural sharing' , async ( ) => {
803
- const result = await storeRef . store . dispatch (
804
- api . endpoints . disabled . initiate ( )
801
+ await storeRef . store . dispatch ( api . endpoints . disabled . initiate ( ) )
802
+ const firstRef = api . endpoints . disabled . select ( ) ( storeRef . store . getState ( ) )
803
+
804
+ await storeRef . store . dispatch (
805
+ api . endpoints . disabled . initiate ( undefined , { forceRefetch : true } )
805
806
)
806
- expect ( mockCopyFn ) . toHaveBeenCalledTimes ( 0 )
807
- expect ( result . data ) . toMatchObject ( apiSuccessResponse )
807
+
808
+ const secondRef = api . endpoints . disabled . select ( ) ( storeRef . store . getState ( ) )
809
+
810
+ expect ( firstRef . requestId ) . not . toEqual ( secondRef . requestId )
811
+ expect ( firstRef . data === secondRef . data ) . toBeFalsy ( )
808
812
} )
809
813
} )
0 commit comments