5
5
* LICENSE file in the root directory of this source tree.
6
6
*
7
7
* @emails react-core
8
+ * @jest -environment ./scripts/jest/ReactDOMServerIntegrationEnvironment
8
9
*/
9
10
10
11
'use strict' ;
@@ -15,6 +16,13 @@ global.ReadableStream =
15
16
global . TextEncoder = require ( 'util' ) . TextEncoder ;
16
17
global . TextDecoder = require ( 'util' ) . TextDecoder ;
17
18
19
+ if ( typeof Blob === 'undefined' ) {
20
+ global . Blob = require ( 'buffer' ) . Blob ;
21
+ }
22
+ if ( typeof File === 'undefined' ) {
23
+ global . File = require ( 'buffer' ) . File ;
24
+ }
25
+
18
26
// let serverExports;
19
27
let webpackServerMap ;
20
28
let ReactServerDOMServer ;
@@ -36,6 +44,13 @@ describe('ReactFlightDOMReplyEdge', () => {
36
44
ReactServerDOMClient = require ( 'react-server-dom-webpack/client.edge' ) ;
37
45
} ) ;
38
46
47
+ if ( typeof FormData === 'undefined' ) {
48
+ // We can't test if we don't have a native FormData implementation because the JSDOM one
49
+ // is missing the arrayBuffer() method.
50
+ it ( 'cannot test' , ( ) => { } ) ;
51
+ return ;
52
+ }
53
+
39
54
it ( 'can encode a reply' , async ( ) => {
40
55
const body = await ReactServerDOMClient . encodeReply ( { some : 'object' } ) ;
41
56
const decoded = await ReactServerDOMServer . decodeReply (
@@ -45,4 +60,82 @@ describe('ReactFlightDOMReplyEdge', () => {
45
60
46
61
expect ( decoded ) . toEqual ( { some : 'object' } ) ;
47
62
} ) ;
63
+
64
+ // @gate enableBinaryFlight
65
+ it ( 'should be able to serialize any kind of typed array' , async ( ) => {
66
+ const buffer = new Uint8Array ( [
67
+ 123 , 4 , 10 , 5 , 100 , 255 , 244 , 45 , 56 , 67 , 43 , 124 , 67 , 89 , 100 , 20 ,
68
+ ] ) . buffer ;
69
+ const buffers = [
70
+ buffer ,
71
+ new Int8Array ( buffer , 1 ) ,
72
+ new Uint8Array ( buffer , 2 ) ,
73
+ new Uint8ClampedArray ( buffer , 2 ) ,
74
+ new Int16Array ( buffer , 2 ) ,
75
+ new Uint16Array ( buffer , 2 ) ,
76
+ new Int32Array ( buffer , 4 ) ,
77
+ new Uint32Array ( buffer , 4 ) ,
78
+ new Float32Array ( buffer , 4 ) ,
79
+ new Float64Array ( buffer , 0 ) ,
80
+ new BigInt64Array ( buffer , 0 ) ,
81
+ new BigUint64Array ( buffer , 0 ) ,
82
+ new DataView ( buffer , 3 ) ,
83
+ ] ;
84
+
85
+ const body = await ReactServerDOMClient . encodeReply ( buffers ) ;
86
+ const result = await ReactServerDOMServer . decodeReply (
87
+ body ,
88
+ webpackServerMap ,
89
+ ) ;
90
+
91
+ expect ( result ) . toEqual ( buffers ) ;
92
+ } ) ;
93
+
94
+ // @gate enableBinaryFlight
95
+ it ( 'should be able to serialize a blob' , async ( ) => {
96
+ const bytes = new Uint8Array ( [
97
+ 123 , 4 , 10 , 5 , 100 , 255 , 244 , 45 , 56 , 67 , 43 , 124 , 67 , 89 , 100 , 20 ,
98
+ ] ) ;
99
+ const blob = new Blob ( [ bytes , bytes ] , {
100
+ type : 'application/x-test' ,
101
+ } ) ;
102
+ const body = await ReactServerDOMClient . encodeReply ( blob ) ;
103
+ const result = await ReactServerDOMServer . decodeReply (
104
+ body ,
105
+ webpackServerMap ,
106
+ ) ;
107
+ expect ( result instanceof Blob ) . toBe ( true ) ;
108
+ expect ( result . size ) . toBe ( bytes . length * 2 ) ;
109
+ expect ( await result . arrayBuffer ( ) ) . toEqual ( await blob . arrayBuffer ( ) ) ;
110
+ } ) ;
111
+
112
+ it ( 'can transport FormData (blobs)' , async ( ) => {
113
+ const bytes = new Uint8Array ( [
114
+ 123 , 4 , 10 , 5 , 100 , 255 , 244 , 45 , 56 , 67 , 43 , 124 , 67 , 89 , 100 , 20 ,
115
+ ] ) ;
116
+ const blob = new Blob ( [ bytes , bytes ] , {
117
+ type : 'application/x-test' ,
118
+ } ) ;
119
+
120
+ const formData = new FormData ( ) ;
121
+ formData . append ( 'hi' , 'world' ) ;
122
+ formData . append ( 'file' , blob , 'filename.test' ) ;
123
+
124
+ expect ( formData . get ( 'file' ) instanceof File ) . toBe ( true ) ;
125
+ expect ( formData . get ( 'file' ) . name ) . toBe ( 'filename.test' ) ;
126
+
127
+ const body = await ReactServerDOMClient . encodeReply ( formData ) ;
128
+ const result = await ReactServerDOMServer . decodeReply (
129
+ body ,
130
+ webpackServerMap ,
131
+ ) ;
132
+
133
+ expect ( result instanceof FormData ) . toBe ( true ) ;
134
+ expect ( result . get ( 'hi' ) ) . toBe ( 'world' ) ;
135
+ const resultBlob = result . get ( 'file' ) ;
136
+ expect ( resultBlob instanceof Blob ) . toBe ( true ) ;
137
+ expect ( resultBlob . name ) . toBe ( 'filename.test' ) ; // In this direction we allow file name to pass through but not other direction.
138
+ expect ( resultBlob . size ) . toBe ( bytes . length * 2 ) ;
139
+ expect ( await resultBlob . arrayBuffer ( ) ) . toEqual ( await blob . arrayBuffer ( ) ) ;
140
+ } ) ;
48
141
} ) ;
0 commit comments