@@ -7,94 +7,98 @@ import { ReactFireOptions } from '..';
77import * as React from 'react' ;
88import 'jest-dom/extend-expect' ;
99
10- afterEach ( cleanup ) ;
10+ describe ( 'useObservable' , ( ) => {
11+ afterEach ( cleanup ) ;
1112
12- it ( 'throws a promise if the observable has no initial value' , ( ) => {
13- const observable$ : Subject < any > = new Subject ( ) ;
13+ it ( 'throws a promise if the observable has no initial value' , ( ) => {
14+ const observable$ : Subject < any > = new Subject ( ) ;
1415
15- try {
16- useObservable ( observable$ , 'test' ) ;
17- } catch ( thingThatWasThrown ) {
18- expect ( thingThatWasThrown ) . toBeInstanceOf ( Promise ) ;
19- }
20- } ) ;
16+ try {
17+ useObservable ( observable$ , 'test' ) ;
18+ } catch ( thingThatWasThrown ) {
19+ expect ( thingThatWasThrown ) . toBeInstanceOf ( Promise ) ;
20+ }
21+ } ) ;
2122
22- it ( 'can return a startval and then the observable once it is ready' , ( ) => {
23- const startVal = 'howdy' ;
24- const observableVal = "y'all" ;
25- const observable$ : Subject < any > = new Subject ( ) ;
23+ it ( 'can return a startval and then the observable once it is ready' , ( ) => {
24+ const startVal = 'howdy' ;
25+ const observableVal = "y'all" ;
26+ const observable$ : Subject < any > = new Subject ( ) ;
2627
27- const { result, waitForNextUpdate } = renderHook ( ( ) =>
28- useObservable ( observable$ , 'test' , startVal )
29- ) ;
28+ const { result, waitForNextUpdate } = renderHook ( ( ) =>
29+ useObservable ( observable$ , 'test' , startVal )
30+ ) ;
3031
31- expect ( result . current ) . toEqual ( startVal ) ;
32+ expect ( result . current ) . toEqual ( startVal ) ;
3233
33- // prove that it actually does emit the value from the observable too
34- act ( ( ) => observable$ . next ( observableVal ) ) ;
35- expect ( result . current ) . toEqual ( observableVal ) ;
36- } ) ;
34+ // prove that it actually does emit the value from the observable too
35+ act ( ( ) => observable$ . next ( observableVal ) ) ;
36+ expect ( result . current ) . toEqual ( observableVal ) ;
37+ } ) ;
3738
38- it ( 'ignores provided initial value if the observable is ready right away' , ( ) => {
39- const startVal = 'howdy' ;
40- const observableVal = "y'all" ;
41- const observable$ = of ( observableVal ) ;
39+ it ( 'ignores provided initial value if the observable is ready right away' , ( ) => {
40+ const startVal = 'howdy' ;
41+ const observableVal = "y'all" ;
42+ const observable$ = of ( observableVal ) ;
4243
43- const { result, waitForNextUpdate } = renderHook ( ( ) =>
44- useObservable ( observable$ , 'test' , startVal )
45- ) ;
44+ const { result, waitForNextUpdate } = renderHook ( ( ) =>
45+ useObservable ( observable$ , 'test' , startVal )
46+ ) ;
4647
47- expect ( result . current ) . toEqual ( observableVal ) ;
48- } ) ;
48+ expect ( result . current ) . toEqual ( observableVal ) ;
49+ } ) ;
4950
50- it ( 'works with Suspense' , async ( ) => {
51- const observableFinalVal = "y'all" ;
52- const observable$ = new Subject ( ) ;
53- const actualComponentId = 'actual-component' ;
54- const fallbackComponentId = 'fallback-component' ;
55-
56- const FallbackComponent = ( ) => (
57- < h1 data-testid = { fallbackComponentId } > Fallback</ h1 >
58- ) ;
59-
60- const Component = ( ) => {
61- const val = useObservable ( observable$ , 'test-suspense' ) ;
62- return < h1 data-testid = { actualComponentId } > { val } } </ h1 > ;
63- } ;
64-
65- const { queryByTestId, getByTestId } = render (
66- < React . Suspense fallback = { < FallbackComponent /> } >
67- < Component />
68- </ React . Suspense >
69- ) ;
70-
71- // make sure Suspense renders the fallback component if the observable has not emitted a value
72- expect ( getByTestId ( fallbackComponentId ) ) . toBeInTheDocument ( ) ;
73- expect ( queryByTestId ( actualComponentId ) ) . toBeNull ( ) ;
74-
75- act ( ( ) => observable$ . next ( observableFinalVal ) ) ;
76- await waitForElement ( ( ) => getByTestId ( actualComponentId ) ) ;
77-
78- // make sure Suspense correctly renders its child after the observable emits a value
79- expect ( getByTestId ( actualComponentId ) ) . toBeInTheDocument ( ) ;
80- expect ( getByTestId ( actualComponentId ) ) . toHaveTextContent ( observableFinalVal ) ;
81- expect ( queryByTestId ( fallbackComponentId ) ) . toBeNull ( ) ;
82- } ) ;
51+ it ( 'works with Suspense' , async ( ) => {
52+ const observableFinalVal = "y'all" ;
53+ const observable$ = new Subject ( ) ;
54+ const actualComponentId = 'actual-component' ;
55+ const fallbackComponentId = 'fallback-component' ;
56+
57+ const FallbackComponent = ( ) => (
58+ < h1 data-testid = { fallbackComponentId } > Fallback</ h1 >
59+ ) ;
60+
61+ const Component = ( ) => {
62+ const val = useObservable ( observable$ , 'test-suspense' ) ;
63+ return < h1 data-testid = { actualComponentId } > { val } } </ h1 > ;
64+ } ;
65+
66+ const { queryByTestId, getByTestId } = render (
67+ < React . Suspense fallback = { < FallbackComponent /> } >
68+ < Component />
69+ </ React . Suspense >
70+ ) ;
71+
72+ // make sure Suspense renders the fallback component if the observable has not emitted a value
73+ expect ( getByTestId ( fallbackComponentId ) ) . toBeInTheDocument ( ) ;
74+ expect ( queryByTestId ( actualComponentId ) ) . toBeNull ( ) ;
75+
76+ act ( ( ) => observable$ . next ( observableFinalVal ) ) ;
77+ await waitForElement ( ( ) => getByTestId ( actualComponentId ) ) ;
78+
79+ // make sure Suspense correctly renders its child after the observable emits a value
80+ expect ( getByTestId ( actualComponentId ) ) . toBeInTheDocument ( ) ;
81+ expect ( getByTestId ( actualComponentId ) ) . toHaveTextContent (
82+ observableFinalVal
83+ ) ;
84+ expect ( queryByTestId ( fallbackComponentId ) ) . toBeNull ( ) ;
85+ } ) ;
8386
84- it ( 'emits new values as the observable changes' , async ( ) => {
85- const startVal = 'start' ;
86- const values = [ 'a' , 'b' , 'c' ] ;
87- const observableSecondValue = 'b' ;
88- const observable$ = new Subject ( ) ;
87+ it ( 'emits new values as the observable changes' , async ( ) => {
88+ const startVal = 'start' ;
89+ const values = [ 'a' , 'b' , 'c' ] ;
90+ const observableSecondValue = 'b' ;
91+ const observable$ = new Subject ( ) ;
8992
90- const { result } = renderHook ( ( ) =>
91- useObservable ( observable$ , 'test' , startVal )
92- ) ;
93+ const { result } = renderHook ( ( ) =>
94+ useObservable ( observable$ , 'test' , startVal )
95+ ) ;
9396
94- expect ( result . current ) . toEqual ( startVal ) ;
97+ expect ( result . current ) . toEqual ( startVal ) ;
9598
96- values . forEach ( value => {
97- act ( ( ) => observable$ . next ( value ) ) ;
98- expect ( result . current ) . toEqual ( value ) ;
99+ values . forEach ( value => {
100+ act ( ( ) => observable$ . next ( value ) ) ;
101+ expect ( result . current ) . toEqual ( value ) ;
102+ } ) ;
99103 } ) ;
100104} ) ;
0 commit comments