Skip to content

Commit

Permalink
Tests: Test correct data from streams
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Apr 30, 2023
1 parent 9b49b4b commit d9fbf04
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// Modules
const pathJoin = require('path').join,
fs = require('fs'),
{finished} = require('stream/promises'),
fdSlicer = require('fd-slicer'),
ReadableStream = require('stream').Readable,
EventEmitter = require('events'),
Expand All @@ -23,6 +24,12 @@ const PATH = pathJoin(__dirname, 'fixtures/test.zip'),
BAD_PATH = pathJoin(__dirname, 'fixtures/does-not-exist.zip'),
FILES = ['test_files/', 'test_files/1.txt', 'test_files/2.txt', 'test_files/3.txt'];

const FILE_CONTENTS = Object.create(null);
for (const filename of FILES) {
if (filename.endsWith('/')) continue;
FILE_CONTENTS[filename] = fs.readFileSync(pathJoin(__dirname, 'fixtures', filename), 'utf8');
}

// Run tests for yauzl object created with all methods
describe('Default module', () => {
runTests(() => yauzl, Promise, true);
Expand Down Expand Up @@ -355,6 +362,28 @@ function runMainTests(methodName, method, getYauzl, Promise) {
it('resolves to Readable Stream', () => {
expect(stream).toBeInstanceOf(ReadableStream);
});

it('streams file data', () => {
expect(entry.fileName).toBe(FILES[0]); // Directory which we can skip

let p = Promise.resolve();
for (const filename of FILES.slice(1)) {
// eslint-disable-next-line jest/valid-expect-in-promise, no-loop-func
p = p.then(() => zipFile.readEntry())
.then((entry) => { // eslint-disable-line no-loop-func, no-shadow
expect(entry.fileName).toBe(filename);
return zipFile.openReadStream(entry)
.then(async (stream) => { // eslint-disable-line no-shadow
const chunks = [];
stream.on('data', chunk => chunks.push(chunk));
await finished(stream, {writable: false});
const data = Buffer.concat(chunks).toString();
expect(data).toBe(FILE_CONTENTS[filename]);
});
});
}
return p;
});
});

describe('entry.openReadStream()', () => {
Expand All @@ -378,6 +407,28 @@ function runMainTests(methodName, method, getYauzl, Promise) {
it('resolves to Readable Stream', () => {
expect(stream).toBeInstanceOf(ReadableStream);
});

it('streams file data', () => {
expect(entry.fileName).toBe(FILES[0]); // Directory which we can skip

let p = Promise.resolve();
for (const filename of FILES.slice(1)) {
// eslint-disable-next-line jest/valid-expect-in-promise, no-loop-func
p = p.then(() => zipFile.readEntry())
.then((entry) => { // eslint-disable-line no-shadow
expect(entry.fileName).toBe(filename);
return entry.openReadStream()
.then(async (stream) => { // eslint-disable-line no-shadow
const chunks = [];
stream.on('data', chunk => chunks.push(chunk));
await finished(stream, {writable: false});
const data = Buffer.concat(chunks).toString();
expect(data).toBe(FILE_CONTENTS[filename]);
});
});
}
return p;
});
});
});
}
1 change: 1 addition & 0 deletions test/fixtures/test_files/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0123456789
1 change: 1 addition & 0 deletions test/fixtures/test_files/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
01234567890123456789
1 change: 1 addition & 0 deletions test/fixtures/test_files/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
012345678901234567890123456789

0 comments on commit d9fbf04

Please sign in to comment.