Skip to content

Commit

Permalink
Fix onDownloadProgress test commented out in test/browser.ts (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
komura-c committed Jun 2, 2024
1 parent eab5049 commit 585ebcb
Showing 1 changed file with 38 additions and 40 deletions.
78 changes: 38 additions & 40 deletions test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import busboy from 'busboy';
import express from 'express';
import {chromium, webkit, type Page} from 'playwright';
import type ky from '../source/index.js';
import type {DownloadProgress} from '../source/index.js';
import {createHttpTestServer, type ExtendedHttpTestServer, type HttpServerOptions} from './helpers/create-http-test-server.js';
import {parseRawBody} from './helpers/parse-body.js';
import {browserTest, defaultBrowsersTest} from './helpers/with-page.js';
Expand Down Expand Up @@ -248,46 +249,43 @@ defaultBrowsersTest(
},
);

// TODO: Fix.
// browserTest('onDownloadProgress works', [chromium, webkit], async (t: ExecutionContext, page: Page) => {
// server.get('/', (_request, response) => {
// response.writeHead(200, {
// 'content-length': '4',
// });

// response.write('me');
// setTimeout(() => {
// response.end('ow');
// }, 1000);
// });

// await page.goto(server.url);
// await addKyScriptToPage(page);

// const result = await page.evaluate(async (url: string) => {
// // `new TextDecoder('utf-8').decode` hangs up?
// const decodeUtf8 = (array: Uint8Array) => String.fromCodePoint(...array);

// const data: any[] = [];
// const text = await window
// .ky(url, {
// onDownloadProgress(progress, chunk) {
// const stringifiedChunk = decodeUtf8(chunk);
// data.push([progress, stringifiedChunk]);
// },
// })
// .text();

// return {data, text};
// }, server.url);

// t.deepEqual(result.data, [
// [{percent: 0, transferredBytes: 0, totalBytes: 4}, ''],
// [{percent: 0.5, transferredBytes: 2, totalBytes: 4}, 'me'],
// [{percent: 1, transferredBytes: 4, totalBytes: 4}, 'ow'],
// ]);
// t.is(result.text, 'meow');
// });
browserTest('onDownloadProgress works', [chromium, webkit], async (t: ExecutionContext, page: Page) => {
server.get('/', (_request, response) => {
response.writeHead(200, {
'content-length': '4',
});

response.write('me');
setTimeout(() => {
response.end('ow');
}, 1000);
});

await page.goto(server.url);
await addKyScriptToPage(page);

const result = await page.evaluate(async (url: string) => {
const data: Array<Array<(DownloadProgress | string)>> = [];
const text = await window
.ky(url, {
onDownloadProgress(progress, chunk) {
// Decode Utf8
const stringifiedChunk = String.fromCodePoint(...chunk);
data.push([progress, stringifiedChunk]);
},
})
.text();

return {data, text};
}, server.url);

t.deepEqual(result.data, [
[{percent: 0, transferredBytes: 0, totalBytes: 4}, ''],
[{percent: 0.5, transferredBytes: 2, totalBytes: 4}, 'me'],
[{percent: 1, transferredBytes: 4, totalBytes: 4}, 'ow'],
]);
t.is(result.text, 'meow');
});

defaultBrowsersTest('throws if onDownloadProgress is not a function', async (t: ExecutionContext, page: Page) => {
server.get('/', (_request, response) => {
Expand Down

0 comments on commit 585ebcb

Please sign in to comment.