-
Notifications
You must be signed in to change notification settings - Fork 403
fix: surface observable errors via status instead of re-throwing #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b113bc3
776c993
74aee91
26cd552
c7d3d6a
13bfb96
91ea1ac
c6ddca8
e021670
0971021
7286c57
11286bf
0fde812
bacf14b
a26a9c0
7b2adce
f5a0561
b17d52c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| CLAUDE.local* | ||
| .DS_Store | ||
| npm-debug.log | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ import '@testing-library/jest-dom/extend-expect'; | |
| import { act, cleanup, render, renderHook, waitFor } from '@testing-library/react'; | ||
| import * as React from 'react'; | ||
| import { of, Subject, BehaviorSubject, throwError } from 'rxjs'; | ||
| import { useObservable } from '../src/index'; | ||
| import { useObservable, FirebaseAppProvider } from '../src/index'; | ||
| import { initializeApp } from 'firebase/app'; | ||
| import { baseConfig } from './appConfig'; | ||
|
|
||
| describe('useObservable', () => { | ||
| afterEach(cleanup); | ||
|
|
@@ -124,10 +126,46 @@ describe('useObservable', () => { | |
|
|
||
| act(() => observable$.next('val')); | ||
| expect(result.current.isComplete).toEqual(false); | ||
|
|
||
| act(() => observable$.complete()); | ||
| await waitFor(() => expect(result.current.isComplete).toEqual(true)); | ||
| }); | ||
|
|
||
| it('surfaces errors via status in non-suspense mode', async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The #535 marquee scenario (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. Had to pair it with a |
||
| const error = new Error('I am an error'); | ||
| const observable$ = throwError(error); | ||
|
|
||
| const { result } = renderHook(() => useObservable('test-error-non-suspense', observable$, { suspense: false })); | ||
|
|
||
| await waitFor(() => expect(result.current.status).toEqual('error')); | ||
| expect(result.current.error).toEqual(error); | ||
| }); | ||
|
|
||
| it('surfaces errors via status when no suspense option is provided', async () => { | ||
| const error = new Error('default mode error'); | ||
| const observable$ = throwError(error); | ||
|
|
||
| const { result } = renderHook(() => useObservable('test-error-default-mode', observable$)); | ||
|
|
||
| await waitFor(() => expect(result.current.status).toEqual('error')); | ||
| expect(result.current.error).toEqual(error); | ||
| }); | ||
|
|
||
| it('retains last emitted data when observable errors after emitting', async () => { | ||
| const subject$ = new Subject<string>(); | ||
| const error = new Error('late error'); | ||
|
|
||
| const { result } = renderHook(() => useObservable('test-late-error', subject$, { suspense: false })); | ||
|
|
||
| act(() => subject$.next('good value')); | ||
| await waitFor(() => expect(result.current.status).toEqual('success')); | ||
| expect(result.current.data).toEqual('good value'); | ||
|
|
||
| act(() => subject$.error(error)); | ||
| await waitFor(() => expect(result.current.status).toEqual('error')); | ||
| expect(result.current.error).toEqual(error); | ||
| expect(result.current.data).toEqual('good value'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Suspense Mode', () => { | ||
|
|
@@ -328,5 +366,29 @@ describe('useObservable', () => { | |
| // if useObservable doesn't re-emit, the value here will still be "Jeff" | ||
| expect(refreshedComp).toHaveTextContent('James'); | ||
| }); | ||
| it('throws an error via FirebaseAppProvider suspense context path', () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified this exercises the context branch (hook called with no config, so the provider's |
||
| const spy = vi.spyOn(console, 'error'); | ||
| spy.mockImplementation(() => {}); | ||
|
|
||
| const onError = (e: ErrorEvent) => e.preventDefault(); | ||
| window.addEventListener('error', onError); | ||
|
|
||
| const app = initializeApp(baseConfig, 'suspense-context-test'); | ||
| const error = new Error('context-path error'); | ||
| const observable$ = throwError(error); | ||
|
|
||
| const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
| <FirebaseAppProvider firebaseApp={app} suspense={true}> | ||
| {children} | ||
| </FirebaseAppProvider> | ||
| ); | ||
|
|
||
| expect(() => renderHook(() => useObservable('test-context-suspense-error', observable$), { wrapper })).toThrow( | ||
| expect.objectContaining({ message: 'context-path error' }) | ||
| ); | ||
|
|
||
| spy.mockRestore(); | ||
| window.removeEventListener('error', onError); | ||
| }); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.