-
Notifications
You must be signed in to change notification settings - Fork 22
test(connectObservable): add test combining ErrorBoundary and Suspense #53
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
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,5 +1,5 @@ | ||
| import { Observable, of, Subscription, Subject, race } from "rxjs" | ||
| import { delay, takeUntil, take, filter, tap } from "rxjs/operators" | ||
| import { delay, takeUntil, take, filter, tap, catchError } from "rxjs/operators" | ||
| import { SUSPENSE } from "../SUSPENSE" | ||
| import { BehaviorObservable } from "./BehaviorObservable" | ||
| import { EMPTY_VALUE } from "./empty-value" | ||
|
|
@@ -57,15 +57,23 @@ const reactEnhancer = <T>( | |
| }) as BehaviorObservable<T> | ||
|
|
||
| let promise: any | ||
| let hasError = false | ||
| let error: unknown = undefined | ||
| const getValue = () => { | ||
| if (hasError) { | ||
| throw error | ||
| } | ||
| try { | ||
| return (source$ as BehaviorObservable<T>).getValue() | ||
| } catch (e) { | ||
| if (promise) throw promise | ||
|
|
||
| if (!IS_SSR && e !== SUSPENSE) { | ||
| source$ | ||
| .pipe(takeUntil(race(onSubscribe, of(true).pipe(delay(60000))))) | ||
| .pipe( | ||
| takeUntil(race(onSubscribe, of(true).pipe(delay(60000)))), | ||
| catchError(() => of(true)), | ||
| ) | ||
| .subscribe() | ||
|
Comment on lines
+73
to
77
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. This was needed because otherwise this subscription would receive an error without handling it - Raising an uncaught exception that jest would pick up as |
||
| try { | ||
| return (source$ as BehaviorObservable<T>).getValue() | ||
|
|
@@ -76,9 +84,15 @@ const reactEnhancer = <T>( | |
| .pipe( | ||
| filter(value => value !== (SUSPENSE as any)), | ||
| take(1), | ||
| tap(() => { | ||
| promise = undefined | ||
| }), | ||
| tap( | ||
| () => { | ||
| promise = undefined | ||
| }, | ||
| err => { | ||
| hasError = true | ||
| error = err | ||
| }, | ||
| ), | ||
| ) | ||
| .toPromise() | ||
| throw promise | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -266,6 +266,39 @@ describe("connectObservable", () => { | |
| ) | ||
| }) | ||
|
|
||
| it("allows errors to be caught in error boundaries with suspense", async () => { | ||
| const errStream = new Subject() | ||
| const [useError] = connectObservable(errStream) | ||
|
|
||
| const ErrorComponent = () => { | ||
| const value = useError() | ||
|
|
||
| return <>{value}</> | ||
| } | ||
|
|
||
| const errorCallback = jest.fn() | ||
| render( | ||
| <TestErrorBoundary onError={errorCallback}> | ||
| <Suspense fallback={<div>Loading...</div>}> | ||
| <ErrorComponent /> | ||
| </Suspense> | ||
| </TestErrorBoundary>, | ||
| ) | ||
|
|
||
| componentAct(() => { | ||
| errStream.error("controlled error") | ||
| }) | ||
|
|
||
| await componentAct(async () => { | ||
| await wait(0) | ||
| }) | ||
|
Comment on lines
+292
to
+294
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. Ewww. I couldn't find any doc on this, but by experimenting, it seems that when a thrown promise fails in Suspense:
It looks like the single Do you smell that smelly smell? |
||
|
|
||
| expect(errorCallback).toHaveBeenCalledWith( | ||
| "controlled error", | ||
| expect.any(Object), | ||
| ) | ||
| }) | ||
|
|
||
| it("doesn't throw errors on components that will get unmounted on the next cycle", () => { | ||
| const valueStream = new BehaviorSubject(1) | ||
| const [useValue, value$] = connectObservable(valueStream) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I compared with the way that this is solved in React's example and within their
read(ourgetValue()equivalent) they either return the value, throw the promise, or throw the error.