56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
/**
|
|
* AuthProvider.logout must clear the query cache.
|
|
*
|
|
* Without it, a signed-out (or deleted) account's orders, addresses, coupons
|
|
* and profile stay cached and are served to the next session on the same
|
|
* device. Uses the REAL provider (tests/setup.ts stubs AuthContext globally).
|
|
*/
|
|
|
|
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
|
|
jest.unmock('@/src/contexts/AuthContext')
|
|
|
|
import React from 'react'
|
|
import { Text, TouchableOpacity } from 'react-native'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import { AuthProvider, useAuth } from '@/src/contexts/AuthContext'
|
|
import { setupApiMocks } from '../mocks/api'
|
|
import { createQueryClient, renderWithProviders } from '../helpers/render'
|
|
|
|
const Probe = () => {
|
|
const { logout } = useAuth()
|
|
return (
|
|
<TouchableOpacity
|
|
testID="logout"
|
|
onPress={() => {
|
|
void logout()
|
|
}}
|
|
>
|
|
<Text>logout</Text>
|
|
</TouchableOpacity>
|
|
)
|
|
}
|
|
|
|
describe('AuthProvider logout', () => {
|
|
it('removes cached queries so the next session starts clean', async () => {
|
|
setupApiMocks()
|
|
const queryClient = createQueryClient()
|
|
|
|
// Stand in for the user-scoped data cached during a session.
|
|
queryClient.setQueryData(['user-scoped-data'], { orders: [1, 2, 3] })
|
|
expect(queryClient.getQueryData(['user-scoped-data'])).toEqual({ orders: [1, 2, 3] })
|
|
|
|
renderWithProviders(
|
|
<AuthProvider>
|
|
<Probe />
|
|
</AuthProvider>,
|
|
{ queryClient, centralStores: false }
|
|
)
|
|
|
|
fireEvent.press(screen.getByTestId('logout'))
|
|
|
|
await waitFor(() =>
|
|
expect(queryClient.getQueryData(['user-scoped-data'])).toBeUndefined()
|
|
)
|
|
})
|
|
})
|