Skip to content

Commit 8dc0498

Browse files
committed
Polyfill Blob methods
1 parent 804dddb commit 8dc0498

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMForm-test.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ global.ReadableStream =
1717
global.TextEncoder = require('util').TextEncoder;
1818
global.TextDecoder = require('util').TextDecoder;
1919

20+
// Polyfill stream methods on JSDOM.
21+
global.Blob.prototype.stream = function () {
22+
const impl = Object.getOwnPropertySymbols(this)[0];
23+
const buffer = this[impl]._buffer;
24+
return new ReadableStream({
25+
start(c) {
26+
c.enqueue(new Uint8Array(buffer));
27+
c.close();
28+
},
29+
});
30+
};
31+
32+
global.Blob.prototype.text = async function () {
33+
const impl = Object.getOwnPropertySymbols(this)[0];
34+
return this[impl]._buffer.toString('utf8');
35+
};
36+
2037
// Don't wait before processing work on the server.
2138
// TODO: we can replace this with FlightServer.act().
2239
global.setTimeout = cb => cb();
@@ -975,10 +992,12 @@ describe('ReactFlightDOMForm', () => {
975992

976993
function Form({action}) {
977994
const [errorMsg, dispatch] = useActionState(action, null);
995+
let text;
978996
if (errorMsg) {
979997
blob = errorMsg;
998+
text = React.use(blob.text());
980999
}
981-
return <form action={dispatch} />;
1000+
return <form action={dispatch}>{text}</form>;
9821001
}
9831002

9841003
const FormRef = await clientExports(Form);
@@ -1008,21 +1027,19 @@ describe('ReactFlightDOMForm', () => {
10081027
container.innerHTML = '';
10091028

10101029
const postbackRscStream = ReactServerDOMServer.renderToReadableStream(
1011-
<FormRef action={serverAction} />,
1030+
{formState, root: <FormRef action={serverAction} />},
10121031
webpackMap,
10131032
);
1014-
const postbackResponse = ReactServerDOMClient.createFromReadableStream(
1015-
postbackRscStream,
1016-
{
1033+
const postbackResponse =
1034+
await ReactServerDOMClient.createFromReadableStream(postbackRscStream, {
10171035
ssrManifest: {
10181036
moduleMap: null,
10191037
moduleLoading: null,
10201038
},
1021-
},
1022-
);
1039+
});
10231040
const postbackSsrStream = await ReactDOMServer.renderToReadableStream(
1024-
postbackResponse,
1025-
{formState: formState},
1041+
postbackResponse.root,
1042+
{formState: postbackResponse.formState},
10261043
);
10271044
await readIntoContainer(postbackSsrStream);
10281045
}
@@ -1034,5 +1051,8 @@ describe('ReactFlightDOMForm', () => {
10341051

10351052
expect(blob instanceof Blob).toBe(true);
10361053
expect(blob.size).toBe(2);
1054+
1055+
const form2 = container.getElementsByTagName('form')[0];
1056+
expect(form2.textContent).toBe('hi');
10371057
});
10381058
});

0 commit comments

Comments
 (0)