Skip to content

Commit 1c2a937

Browse files
committed
Added willwade's tests for processor and validator constructors
1 parent d410c69 commit 1c2a937

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

test/core/baseConfig.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import AdmZip from 'adm-zip';
2+
import { BaseProcessor } from '../../src/core/baseProcessor';
3+
import { BaseValidator } from '../../src/validation/baseValidator';
4+
import { ValidationResult } from '../../src/validation/validationTypes';
5+
6+
class TestProcessor extends BaseProcessor {
7+
async extractTexts(): Promise<string[]> {
8+
return [];
9+
}
10+
11+
async loadIntoTree(): Promise<any> {
12+
return {};
13+
}
14+
15+
async processTexts(): Promise<Buffer> {
16+
return Buffer.from([]);
17+
}
18+
19+
async saveFromTree(): Promise<void> {
20+
return;
21+
}
22+
}
23+
24+
class TestValidator extends BaseValidator {
25+
async validate(_content: any, _filename: string, _filesize: number): Promise<ValidationResult> {
26+
return this.buildResult('file', 0, 'test');
27+
}
28+
}
29+
30+
describe('base defaults', () => {
31+
it('BaseProcessor provides a default zipAdapter', async () => {
32+
const processor = new TestProcessor();
33+
const zip = new AdmZip();
34+
zip.addFile('hello.txt', Buffer.from('hello', 'utf8'));
35+
const adapter = await (processor as any).options.zipAdapter(zip.toBuffer());
36+
37+
expect(adapter.listFiles()).toContain('hello.txt');
38+
const contents = await adapter.readFile('hello.txt');
39+
expect(Buffer.from(contents).toString('utf8')).toBe('hello');
40+
});
41+
42+
it('BaseValidator provides a default zipAdapter', async () => {
43+
const validator = new TestValidator();
44+
const zip = new AdmZip();
45+
zip.addFile('world.txt', Buffer.from('world', 'utf8'));
46+
const adapter = await (validator as any)._options.zipAdapter(zip.toBuffer());
47+
48+
expect(adapter.listFiles()).toContain('world.txt');
49+
const contents = await adapter.readFile('world.txt');
50+
expect(Buffer.from(contents).toString('utf8')).toBe('world');
51+
});
52+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import JSZip from 'jszip';
2+
3+
async function getBrowserZipAdapter() {
4+
jest.resetModules();
5+
jest.doMock('../../src/utils/io', () => {
6+
const actual = jest.requireActual('../../src/utils/io');
7+
return {
8+
...actual,
9+
isNodeRuntime: () => false,
10+
};
11+
});
12+
13+
const module = await import('../../src/utils/zip');
14+
return module.getZipAdapter;
15+
}
16+
17+
describe('zip adapter (browser)', () => {
18+
it('does not include directories in listFiles', async () => {
19+
const zip = new JSZip();
20+
zip.folder('dir');
21+
zip.file('dir/nested.txt', 'nested');
22+
const buffer = await zip.generateAsync({ type: 'uint8array' });
23+
24+
const getZipAdapter = await getBrowserZipAdapter();
25+
const adapter = await getZipAdapter(buffer);
26+
const entries = adapter.listFiles();
27+
28+
expect(entries).toContain('dir/nested.txt');
29+
expect(entries).not.toContain('dir/');
30+
});
31+
});

0 commit comments

Comments
 (0)