@@ -35,9 +35,7 @@ describe('useObservable', () => {
35
35
const observableVal = "y'all" ;
36
36
const observable$ = new Subject < any > ( ) ;
37
37
38
- const { result, waitForNextUpdate } = renderHook ( ( ) =>
39
- useObservable ( observable$ , 'test-2' , startVal )
40
- ) ;
38
+ const { result, waitForNextUpdate } = renderHook ( ( ) => useObservable ( observable$ , 'test-2' , startVal ) ) ;
41
39
42
40
expect ( result . current ) . toEqual ( startVal ) ;
43
41
@@ -118,9 +116,7 @@ describe('useObservable', () => {
118
116
const actualComponentId = 'actual-component' ;
119
117
const fallbackComponentId = 'fallback-component' ;
120
118
121
- const FallbackComponent = ( ) => (
122
- < h1 data-testid = { fallbackComponentId } > Fallback</ h1 >
123
- ) ;
119
+ const FallbackComponent = ( ) => < h1 data-testid = { fallbackComponentId } > Fallback</ h1 > ;
124
120
125
121
const Component = ( ) => {
126
122
const val = useObservable ( observable$ , 'test-suspense' ) ;
@@ -142,9 +138,7 @@ describe('useObservable', () => {
142
138
143
139
// make sure Suspense correctly renders its child after the observable emits a value
144
140
expect ( getByTestId ( actualComponentId ) ) . toBeInTheDocument ( ) ;
145
- expect ( getByTestId ( actualComponentId ) ) . toHaveTextContent (
146
- observableFinalVal
147
- ) ;
141
+ expect ( getByTestId ( actualComponentId ) ) . toHaveTextContent ( observableFinalVal ) ;
148
142
expect ( queryByTestId ( fallbackComponentId ) ) . toBeNull ( ) ;
149
143
} ) ;
150
144
@@ -153,9 +147,7 @@ describe('useObservable', () => {
153
147
const values = [ 'a' , 'b' , 'c' ] ;
154
148
const observable$ = new Subject ( ) ;
155
149
156
- const { result } = renderHook ( ( ) =>
157
- useObservable ( observable$ , 'test-changes' , startVal )
158
- ) ;
150
+ const { result } = renderHook ( ( ) => useObservable ( observable$ , 'test-changes' , startVal ) ) ;
159
151
160
152
expect ( result . current ) . toEqual ( startVal ) ;
161
153
@@ -182,16 +174,12 @@ describe('useObservable', () => {
182
174
return (
183
175
< React . Suspense fallback = "loading" >
184
176
< ObservableConsumer data-testid = { firstComponentId } />
185
- { renderSecondComponent ? (
186
- < ObservableConsumer data-testid = { secondComponentId } />
187
- ) : null }
177
+ { renderSecondComponent ? < ObservableConsumer data-testid = { secondComponentId } /> : null }
188
178
</ React . Suspense >
189
179
) ;
190
180
} ;
191
181
192
- const { getByTestId, rerender } = render (
193
- < Component renderSecondComponent = { false } />
194
- ) ;
182
+ const { getByTestId, rerender } = render ( < Component renderSecondComponent = { false } /> ) ;
195
183
196
184
// emit one value to the first component (second one isn't rendered yet)
197
185
act ( ( ) => observable$ . next ( values [ 0 ] ) ) ;
@@ -211,4 +199,45 @@ describe('useObservable', () => {
211
199
const comp2 = await waitForElement ( ( ) => getByTestId ( secondComponentId ) ) ;
212
200
expect ( comp2 ) . toHaveTextContent ( values [ 1 ] ) ;
213
201
} ) ;
202
+
203
+ it ( `emits the new observable's value if the observable is swapped out` , async ( ) => {
204
+ const obs1$ = new Subject ( ) ;
205
+ const obs2$ = new Subject ( ) ;
206
+
207
+ let currentObs$ = obs1$ ;
208
+ let currentObsId = 'observable-1' ;
209
+
210
+ const ObservableConsumer = props => {
211
+ const val = useObservable ( currentObs$ , currentObsId ) ;
212
+
213
+ return < h1 { ...props } > { val } </ h1 > ;
214
+ } ;
215
+
216
+ const Component = ( ) => {
217
+ return (
218
+ < React . Suspense fallback = { < span data-testid = "fallback" > Loading...</ span > } >
219
+ < ObservableConsumer data-testid = { 'consumer' } />
220
+ </ React . Suspense >
221
+ ) ;
222
+ } ;
223
+
224
+ const { getByTestId, rerender } = render ( < Component /> ) ;
225
+
226
+ act ( ( ) => obs1$ . next ( 'Jeff' ) ) ;
227
+ const comp = await waitForElement ( ( ) => getByTestId ( 'consumer' ) ) ;
228
+ expect ( comp ) . toBeInTheDocument ( ) ;
229
+
230
+ currentObs$ = obs2$ ;
231
+ currentObsId = 'observable-2' ;
232
+
233
+ rerender ( < Component /> ) ;
234
+ expect ( getByTestId ( 'fallback' ) ) . toBeInTheDocument ( ) ;
235
+
236
+ act ( ( ) => obs2$ . next ( 'James' ) ) ;
237
+ const refreshedComp = await waitForElement ( ( ) => getByTestId ( 'consumer' ) ) ;
238
+ expect ( refreshedComp ) . toBeInTheDocument ( ) ;
239
+
240
+ // if useObservable doesn't re-emit, the value here will still be "Jeff"
241
+ expect ( refreshedComp ) . toHaveTextContent ( 'James' ) ;
242
+ } ) ;
214
243
} ) ;
0 commit comments