Skip to content

Commit 0aef3b3

Browse files
committed
Add tests for .values() and Symbol.iterator methods.
1 parent 9b6a0d8 commit 0aef3b3

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

lib/Encoder.test.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import {Readable} from "stream"
33

44
import test from "ava"
55

6-
import {FormData, File, fileFromPath} from "formdata-node"
6+
import {FormData, Blob, File, fileFromPath} from "formdata-node"
77

8+
import skipSync from "./__helper__/skipIterationsSync"
89
import readStream from "./__helper__/readStream"
910
import skip from "./__helper__/skipIterations"
1011
import readLine from "./__helper__/readLine"
@@ -87,6 +88,54 @@ test("Returns the length of the FormData content", async t => {
8788
t.is<number>(encoder.getContentLength(), expected)
8889
})
8990

91+
test(".values() yields headers as Uint8Array", t => {
92+
const fd = new FormData()
93+
94+
fd.set("field", "Some value")
95+
96+
const iterable = new Encoder(fd).values()
97+
98+
const {value: actual} = skipSync(iterable)
99+
100+
t.true(actual instanceof Uint8Array)
101+
})
102+
103+
test(".valeus() yields field as Uint8Array", t => {
104+
const fd = new FormData()
105+
106+
fd.set("field", "Some value")
107+
108+
const {value: actual} = skipSync(new Encoder(fd).values(), 2)
109+
110+
t.true(actual instanceof Uint8Array)
111+
})
112+
113+
test(".valeus() yields field's content", t => {
114+
const string = "Some value"
115+
const expected = new TextEncoder().encode(string)
116+
117+
const fd = new FormData()
118+
119+
fd.set("field", string)
120+
121+
const {value: actual} = skipSync(new Encoder(fd).values(), 2)
122+
123+
t.true(Buffer.from(actual as Uint8Array).equals(expected))
124+
})
125+
126+
test(".values() yields a file as is", async t => {
127+
const file = new File(["File content"], "name.txt")
128+
129+
const fd = new FormData()
130+
131+
fd.set("file", file)
132+
133+
const {value: actual} = skipSync(new Encoder(fd).values(), 2)
134+
135+
t.true(actual instanceof File)
136+
t.is(await (actual as File).text(), await file.text())
137+
})
138+
90139
test("Yields correct headers for a field", async t => {
91140
const fd = new FormData()
92141

@@ -216,7 +265,6 @@ test("Yields every appended File", async t => {
216265
})
217266

218267
fd.append("file", firstFile)
219-
220268
fd.append("file", secondFile)
221269

222270
const iterable = readLine(Readable.from(new Encoder(fd)))
@@ -246,6 +294,22 @@ test("Yields every appended File", async t => {
246294
t.is(secondFileContent, await secondFile.text())
247295
})
248296

297+
test("Can be read through using Blob", async t => {
298+
const fd = new FormData()
299+
300+
fd.set("field", "Some field")
301+
fd.set("file", await fileFromPath("license", {type: "text/plain"}))
302+
303+
const encoder = new Encoder(fd)
304+
const blob = new Blob([...encoder] as any[])
305+
306+
t.true(
307+
Buffer
308+
.from(await blob.arrayBuffer())
309+
.equals(await readStream(Readable.from(encoder)))
310+
)
311+
})
312+
249313
test(
250314
"Throws TypeError when the first argument is not a FormData instance",
251315
t => {

lib/__helper__/skipIterations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type Unwrap<T> = T extends AsyncGenerator<infer U> ? Unwrap<U> : T
1+
type Unwrap<T> = T extends AsyncGenerator<infer U> ? U : T
22

33
async function skipIterations<
44
T extends AsyncGenerator<any>

lib/__helper__/skipIterationsSync.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type Unwrap<T> = T extends Generator<infer Y> ? Y : T
2+
3+
function skipIterationsSync<
4+
T extends Generator<any>
5+
>(iterable: T, iterations = 1): IteratorResult<Unwrap<T>, void> {
6+
while (--iterations) {
7+
iterable.next()
8+
}
9+
10+
return iterable.next()
11+
}
12+
13+
export default skipIterationsSync

0 commit comments

Comments
 (0)