Skip to content

Commit ab76510

Browse files
committed
Add tests
1 parent ca8b7df commit ab76510

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/index.js

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,14 +1864,27 @@ test("fetch method", (t) => {
18641864
reactNative: { textStreaming: true },
18651865
});
18661866
const clone = res.clone();
1867-
const stream = await clone.body;
1868-
const text = new TextDecoder().decode(await drainStream(stream));
1867+
1868+
const resStream = await res.body;
1869+
const cloneStream = await clone.body;
1870+
const resText = new TextDecoder().decode(
1871+
await drainStream(resStream)
1872+
);
1873+
const cloneText = new TextDecoder().decode(
1874+
await drainStream(cloneStream)
1875+
);
18691876

18701877
t.ok(
1871-
stream instanceof ReadableStream,
1878+
resStream instanceof ReadableStream,
18721879
"Response implements streaming body"
18731880
);
1874-
t.eq(text, "Hello world!");
1881+
t.eq(resText, "Hello world!");
1882+
1883+
t.ok(
1884+
cloneStream instanceof ReadableStream,
1885+
"Response implements streaming body"
1886+
);
1887+
t.eq(cloneText, "Hello world!");
18751888
});
18761889

18771890
t.test("cloning blob response", async (t) => {
@@ -1882,8 +1895,11 @@ test("fetch method", (t) => {
18821895
},
18831896
});
18841897
const clone = res.clone();
1885-
const json = await clone.json();
1886-
t.eq(json.headers.accept, "application/json");
1898+
const resJson = res.json();
1899+
const cloneJson = await clone.json();
1900+
1901+
t.eq(resJson.headers.accept, "application/json");
1902+
t.eq(cloneJson.headers.accept, "application/json");
18871903
});
18881904

18891905
t.test("cloning array buffer response", async (t) => {
@@ -1894,15 +1910,28 @@ test("fetch method", (t) => {
18941910
},
18951911
});
18961912
const clone = res.clone();
1897-
const buf = await clone.arrayBuffer();
1913+
const resBuf = await res.arrayBuffer();
1914+
const cloneBuf = await clone.arrayBuffer();
18981915

1899-
t.ok(buf instanceof ArrayBuffer, "buf is an ArrayBuffer instance");
1900-
t.eq(buf.byteLength, 256, "buf.byteLength is correct");
1916+
t.ok(
1917+
resBuf instanceof ArrayBuffer,
1918+
"buf is an ArrayBuffer instance"
1919+
);
1920+
t.eq(resBuf.byteLength, 256, "buf.byteLength is correct");
1921+
1922+
t.ok(
1923+
cloneBuf instanceof ArrayBuffer,
1924+
"buf is an ArrayBuffer instance"
1925+
);
1926+
t.eq(cloneBuf.byteLength, 256, "buf.byteLength is correct");
19011927

19021928
const expected = Array.from({ length: 256 }, (_, i) => i);
1903-
const actual = Array.from(new Uint8Array(buf));
19041929

1905-
t.eq(actual, expected);
1930+
const resActual = Array.from(new Uint8Array(resBuf));
1931+
const cloneActual = Array.from(new Uint8Array(cloneBuf));
1932+
1933+
t.eq(resActual, expected);
1934+
t.eq(cloneActual, expected);
19061935
});
19071936
});
19081937
});

0 commit comments

Comments
 (0)