Skip to content

Commit a45e2c9

Browse files
committed
Add SSR tests
1 parent e0a377c commit a45e2c9

File tree

5 files changed

+128
-10
lines changed

5 files changed

+128
-10
lines changed

package-lock.json

+8-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@babel/preset-env": "^7.22.7",
4545
"@babel/preset-typescript": "^7.22.5",
4646
"@testing-library/react": "^14.0.0",
47+
"@types/node": "^20.4.7",
4748
"@types/react": "^18.2.14",
4849
"@types/react-dom": "^18.2.6",
4950
"@vitest/coverage-v8": "^0.33.0",

packages/core/src/Subscribe.test.tsx

+30-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,20 @@ import {
77
} from "@rx-state/core"
88
import { act, render, screen } from "@testing-library/react"
99
import React, { StrictMode, useEffect, useState } from "react"
10-
import { defer, EMPTY, NEVER, Observable, of, startWith, Subject } from "rxjs"
11-
import { describe, it, expect, vi } from "vitest"
12-
import { bind, RemoveSubscribe, Subscribe as OriginalSubscribe } from "./"
10+
import { renderToPipeableStream } from "react-dom/server"
11+
import {
12+
defer,
13+
EMPTY,
14+
lastValueFrom,
15+
NEVER,
16+
Observable,
17+
of,
18+
startWith,
19+
Subject,
20+
} from "rxjs"
21+
import { describe, expect, it, vi } from "vitest"
22+
import { bind, Subscribe as OriginalSubscribe, RemoveSubscribe } from "./"
23+
import { pipeableStreamToObservable } from "./test-helpers/pipeableStreamToObservable"
1324
import { TestErrorBoundary } from "./test-helpers/TestErrorBoundary"
1425
import { useStateObservable } from "./useStateObservable"
1526

@@ -432,6 +443,22 @@ describe("Subscribe", () => {
432443
unmount()
433444
})
434445
})
446+
447+
describe("On SSR", () => {
448+
// Testing-library doesn't support SSR yet https://github.com/testing-library/react-testing-library/issues/561
449+
450+
it("Renders the fallback", async () => {
451+
const stream = renderToPipeableStream(
452+
<Subscribe fallback={<div>Loading</div>}>
453+
<div>Content</div>
454+
</Subscribe>,
455+
)
456+
const result = await lastValueFrom(pipeableStreamToObservable(stream))
457+
458+
expect(result).toContain("<div>Loading</div>")
459+
expect(result).not.toContain("<div>Content</div>")
460+
})
461+
})
435462
})
436463

437464
describe("RemoveSubscribe", () => {

packages/core/src/bind/connectObservable.test.tsx

+48
Original file line numberDiff line numberDiff line change
@@ -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 "../"
3637
import { TestErrorBoundary } from "../test-helpers/TestErrorBoundary"
38+
import { renderToPipeableStream } from "react-dom/server"
39+
import { pipeableStreamToObservable } from "../test-helpers/pipeableStreamToObservable"
3740

3841
const 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
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { PipeableStream } from "react-dom/server"
2+
import { Observable, scan } from "rxjs"
3+
import { PassThrough } from "stream"
4+
5+
export function pipeableStreamToObservable(
6+
stream: PipeableStream,
7+
): Observable<string> {
8+
return new Observable((subscriber) => {
9+
const passthrough = new PassThrough()
10+
const sub = readStream$<string>(passthrough)
11+
.pipe(scan((acc, v) => acc + v, ""))
12+
.subscribe(subscriber)
13+
14+
stream.pipe(passthrough)
15+
16+
return () => {
17+
sub.unsubscribe()
18+
}
19+
})
20+
}
21+
22+
function readStream$<T>(stream: NodeJS.ReadableStream) {
23+
return new Observable<T>((subscriber) => {
24+
const dataHandler = (data: T) => subscriber.next(data)
25+
stream.addListener("data", dataHandler)
26+
27+
const errorHandler = (error: any) => subscriber.error(error)
28+
stream.addListener("error", errorHandler)
29+
30+
const closeHandler = () => subscriber.complete()
31+
stream.addListener("close", closeHandler)
32+
stream.addListener("end", closeHandler)
33+
34+
return () => {
35+
stream.removeListener("data", dataHandler)
36+
stream.removeListener("error", errorHandler)
37+
stream.removeListener("close", closeHandler)
38+
stream.removeListener("end", closeHandler)
39+
}
40+
})
41+
}

0 commit comments

Comments
 (0)