Closed shaoshiva closed 6 months ago
Mock the unsubscribe callback returned by onSnapshot and export the mock as mockQueryOnSnapshotUnsubscribe.
onSnapshot
mockQueryOnSnapshotUnsubscribe
A typical usage is to test whether the unsubscribe callback is called during your application's lifecycle.
For example, here is a React component that subscribes on mount and unsubscribes on unmount:
const MyComponent = () => { useEffect(() => { const unsubscribe = firestore().collection('test').onSnapshot(() => { console.log('Got a snapshot'); }); return () => { unsubscribe(); }; }, []); return (...); }
We want to test that the component unsubscribes on unmount. Thanks to mockQueryOnSnapshotUnsubscribe we can achieve this like this:
import { mockQueryOnSnapshotUnsubscribe } from 'firestore-jest-mock/mocks/firestore'; describe('MyComponent', () => { it('should unsubscribe on umount', () => { const { unmount } = render(<MyComponent />); expect(mockQueryOnSnapshotUnsubscribe).toHaveBeenCalledTimes(0); unmount(); expect(mockQueryOnSnapshotUnsubscribe).toHaveBeenCalledTimes(1); }); });
Addresses #189
Unit tests full-setup-library-firestore.test.js and full-setup.test.js have been updated in order to assert that the unsubscribe callback returned by onSnapshot is mockQueryOnSnapshotUnsubscribe.
full-setup-library-firestore.test.js
full-setup.test.js
Merged and released in version 0.25.0. Thanks!
Awesome, thank you @sbatson5 for you reactivity 🙏
Description
Mock the unsubscribe callback returned by
onSnapshot
and export the mock asmockQueryOnSnapshotUnsubscribe
.A typical usage is to test whether the unsubscribe callback is called during your application's lifecycle.
Usage
For example, here is a React component that subscribes on mount and unsubscribes on unmount:
We want to test that the component unsubscribes on unmount. Thanks to
mockQueryOnSnapshotUnsubscribe
we can achieve this like this:Related issues
Addresses #189
How to test
Unit tests
full-setup-library-firestore.test.js
andfull-setup.test.js
have been updated in order to assert that the unsubscribe callback returned byonSnapshot
ismockQueryOnSnapshotUnsubscribe
.