Skip to content

Commit e7820d1

Browse files
committed
Rewrite tests to expect on reference equality
1 parent 738faa6 commit e7820d1

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

packages/toolkit/src/query/tests/createApi.test.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
} from './helpers'
1818
import { server } from './mocks/server'
1919
import { rest } from 'msw'
20-
import * as utils from '../utils'
2120

2221
const originalEnv = process.env.NODE_ENV
2322
beforeAll(() => void ((process.env as any).NODE_ENV = 'development'))
@@ -765,17 +764,9 @@ test('providesTags and invalidatesTags can use baseQueryMeta', async () => {
765764
expect('request' in _meta! && 'response' in _meta!).toBe(true)
766765
})
767766

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', () => {
775768
type SuccessResponse = { value: 'success' }
776769

777-
const apiSuccessResponse: SuccessResponse = { value: 'success' }
778-
779770
const api = createApi({
780771
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
781772
tagTypes: ['success'],
@@ -793,17 +784,30 @@ describe('strucutralSharing flag behaviors', () => {
793784
const storeRef = setupApiStore(api)
794785

795786
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 })
798792
)
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()
801798
})
799+
802800
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 })
805806
)
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()
808812
})
809813
})

0 commit comments

Comments
 (0)