@@ -10,6 +10,7 @@ import {
1010 defer ,
1111 EMPTY ,
1212 from ,
13+ lastValueFrom ,
1314 merge ,
1415 NEVER ,
1516 Observable ,
@@ -34,6 +35,8 @@ import {
3435 useStateObservable ,
3536} from "../"
3637import { TestErrorBoundary } from "../test-helpers/TestErrorBoundary"
38+ import { renderToPipeableStream } from "react-dom/server"
39+ import { pipeableStreamToObservable } from "../test-helpers/pipeableStreamToObservable"
3740
3841const wait = ( ms : number ) => new Promise ( ( res ) => setTimeout ( res , ms ) )
3942
@@ -939,4 +942,49 @@ describe("connectObservable", () => {
939942 } )
940943 expect ( queryByText ( "Result 10" ) ) . not . toBeNull ( )
941944 } )
945+
946+ describe ( "The hook on SSR" , ( ) => {
947+ // Testing-library doesn't support SSR yet https://github.com/testing-library/react-testing-library/issues/561
948+
949+ it ( "returns the value if the state observable has a subscription" , async ( ) => {
950+ const [ useState , state$ ] = bind ( of ( 5 ) )
951+ state$ . subscribe ( )
952+ const Component = ( ) => {
953+ const value = useState ( )
954+ return < div > Value: { value } </ div >
955+ }
956+ const stream = renderToPipeableStream ( < Component /> )
957+ const result = await lastValueFrom ( pipeableStreamToObservable ( stream ) )
958+
959+ // Sigh...
960+ expect ( result ) . toEqual ( "<div>Value: <!-- -->5</div>" )
961+ } )
962+
963+ it ( "throws Missing Subscribe if the state observable doesn't have a subscription nor a default value" , async ( ) => {
964+ const [ useState ] = bind ( of ( 5 ) )
965+ const Component = ( ) => {
966+ const value = useState ( )
967+ return < div > Value: { value } </ div >
968+ }
969+ const stream = renderToPipeableStream ( < Component /> )
970+ try {
971+ await lastValueFrom ( pipeableStreamToObservable ( stream ) )
972+ } catch ( ex : any ) {
973+ expect ( ex . message ) . to . equal ( "Missing Subscribe!" )
974+ }
975+ expect . assertions ( 1 )
976+ } )
977+
978+ it ( "returns the default value if the observable didn't emit yet" , async ( ) => {
979+ const [ useState ] = bind ( of ( 5 ) , 3 )
980+ const Component = ( ) => {
981+ const value = useState ( )
982+ return < div > Value: { value } </ div >
983+ }
984+ const stream = renderToPipeableStream ( < Component /> )
985+ const result = await lastValueFrom ( pipeableStreamToObservable ( stream ) )
986+
987+ expect ( result ) . toEqual ( "<div>Value: <!-- -->3</div>" )
988+ } )
989+ } )
942990} )
0 commit comments