16
global.TextEncoder = require('util').TextEncoder;
17
global.TextDecoder = require('util').TextDecoder;
18
19
-if (typeof Blob === 'undefined') {
20
- global.Blob = require('buffer').Blob;
21
-}
22
-if (typeof File === 'undefined') {
23
- global.File = require('buffer').File;
19
+global.Blob = require('buffer').Blob;
20
+if (typeof File === 'undefined' || typeof FormData === 'undefined') {
21
+ global.File = require('buffer').File || require('undici').File;
22
+ global.FormData = require('undici').FormData;
23
}
24
25
// let serverExports;
43
ReactServerDOMClient = require('react-server-dom-webpack/client.edge');
44
});
45
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
-
46
it('can encode a reply', async () => {
47
const body = await ReactServerDOMClient.encodeReply({some: 'object'});
48
const decoded = await ReactServerDOMServer.decodeReply(
81
);
82
83
expect(result).toEqual(buffers);
84
+ // Array buffers can't use the toEqual helper.
85
+ expect(new Uint8Array(result[0])).toEqual(new Uint8Array(buffers[0]));
86
});
87
88
// @gate enableBinaryFlight
103
expect(await result.arrayBuffer()).toEqual(await blob.arrayBuffer());
104
});
105
112
- if (typeof FormData !== 'undefined' && typeof File !== 'undefined') {
113
- it('can transport FormData (blobs)', async () => {
114
- const bytes = new Uint8Array([
115
- 123, 4, 10, 5, 100, 255, 244, 45, 56, 67, 43, 124, 67, 89, 100, 20,
116
- ]);
117
- const blob = new Blob([bytes, bytes], {
118
- type: 'application/x-test',
119
- });
120
-
121
- const formData = new FormData();
122
- formData.append('hi', 'world');
123
- formData.append('file', blob, 'filename.test');
124
-
125
- expect(formData.get('file') instanceof File).toBe(true);
126
- expect(formData.get('file').name).toBe('filename.test');
127
-
128
- const body = await ReactServerDOMClient.encodeReply(formData);
129
- const result = await ReactServerDOMServer.decodeReply(
130
- body,
131
- webpackServerMap,
132
- );
133
-
134
- expect(result instanceof FormData).toBe(true);
135
- expect(result.get('hi')).toBe('world');
136
- const resultBlob = result.get('file');
137
- expect(resultBlob instanceof Blob).toBe(true);
138
- expect(resultBlob.name).toBe('filename.test'); // In this direction we allow file name to pass through but not other direction.
139
- expect(resultBlob.size).toBe(bytes.length * 2);
140
- expect(await resultBlob.arrayBuffer()).toEqual(await blob.arrayBuffer());
106
+ it('can transport FormData (blobs)', async () => {
107
+ const bytes = new Uint8Array([
108
+ 123, 4, 10, 5, 100, 255, 244, 45, 56, 67, 43, 124, 67, 89, 100, 20,
109
+ ]);
110
+ const blob = new Blob([bytes, bytes], {
111
+ type: 'application/x-test',
112
});
142
- }
113
+
114
+ const formData = new FormData();
115
+ formData.append('hi', 'world');
116
+ formData.append('file', blob, 'filename.test');
117
+
118
+ expect(formData.get('file') instanceof File).toBe(true);
119
+ expect(formData.get('file').name).toBe('filename.test');
120
+
121
+ const body = await ReactServerDOMClient.encodeReply(formData);
122
+ const result = await ReactServerDOMServer.decodeReply(
123
+ body,
124
+ webpackServerMap,
125
+ );
126
+
127
+ expect(result instanceof FormData).toBe(true);
128
+ expect(result.get('hi')).toBe('world');
129
+ const resultBlob = result.get('file');
130
+ expect(resultBlob instanceof Blob).toBe(true);
131
+ expect(resultBlob.name).toBe('filename.test'); // In this direction we allow file name to pass through but not other direction.
132
+ expect(resultBlob.size).toBe(bytes.length * 2);
133
+ expect(await resultBlob.arrayBuffer()).toEqual(await blob.arrayBuffer());
134
+ });
135
});