-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for valid input formats
- Loading branch information
Showing
5 changed files
with
246 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
import path from 'path' | ||
import fs from 'fs-extra' | ||
import pdfDiff from 'pdf-diff' | ||
import { PDFDocument } from 'pdf-lib' | ||
import { jest } from '@jest/globals' | ||
|
||
import PDFMerger from '../index' | ||
|
||
const __dirname = path.dirname(new URL(import.meta.url).pathname) | ||
const FIXTURES_DIR = path.join(__dirname, 'fixtures') | ||
const TMP_DIR = path.join(__dirname, 'tmp') | ||
|
||
jest.setTimeout(10000) | ||
|
||
describe('PDFMerger', () => { | ||
beforeAll(async () => { | ||
await fs.ensureDir(TMP_DIR) | ||
}) | ||
|
||
test('merge two simple files', async () => { | ||
const merger = new PDFMerger() | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_B.pdf')) | ||
await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) | ||
|
||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), | ||
path.join(TMP_DIR, 'Testfile_AB.pdf') | ||
) | ||
|
||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('reset the internal document', async () => { | ||
const merger = new PDFMerger() | ||
await merger.add(path.join(FIXTURES_DIR, 'MergeDemo.pdf')) | ||
merger.reset() | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_B.pdf')) | ||
await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) | ||
|
||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), | ||
path.join(TMP_DIR, 'Testfile_AB.pdf') | ||
) | ||
|
||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('set title and check if title is set properly', async () => { | ||
const merger = new PDFMerger() | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) | ||
|
||
const testTitle = 'Test Title' | ||
|
||
await merger.setMetadata({ | ||
title: testTitle | ||
}) | ||
|
||
await merger.save(path.join(TMP_DIR, 'Testfile_A_Metadata.pdf')) | ||
const pdfData = fs.readFileSync(path.join(TMP_DIR, 'Testfile_A_Metadata.pdf')) | ||
|
||
const outputDocument = await PDFDocument.load(pdfData, { ignoreEncryption: true }) | ||
const outputTitle = outputDocument.getTitle() | ||
|
||
expect(testTitle).toBe(outputTitle) | ||
}) | ||
|
||
describe('test valid inputs', () => { | ||
test('provide input as Uint8Array', async () => { | ||
const merger = new PDFMerger() | ||
const Testfile_A_Uint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) | ||
expect(Testfile_A_Uint8Array).toBeInstanceOf(Uint8Array) | ||
await merger.add(Testfile_A_Uint8Array) | ||
const Testfile_B_Uint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_B.pdf')) | ||
expect(Testfile_B_Uint8Array).toBeInstanceOf(Uint8Array) | ||
await merger.add(Testfile_B_Uint8Array) | ||
await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) | ||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), | ||
path.join(TMP_DIR, 'Testfile_AB.pdf') | ||
) | ||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('provide input as ArrayBuffer', async () => { | ||
const merger = new PDFMerger(); | ||
const Testfile_A_Uint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_A.pdf'), null); | ||
const Testfile_A_Buffer = Testfile_A_Uint8Array.buffer; | ||
expect(Testfile_A_Buffer).toBeInstanceOf(ArrayBuffer); | ||
const Testfile_B_Uint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_B.pdf'), null); | ||
const Testfile_B_Buffer = Testfile_B_Uint8Array.buffer; | ||
expect(Testfile_B_Buffer).toBeInstanceOf(ArrayBuffer); | ||
await merger.add(Testfile_A_Buffer); | ||
await merger.add(Testfile_B_Buffer); | ||
await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')); | ||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), | ||
path.join(TMP_DIR, 'Testfile_AB.pdf') | ||
); | ||
expect(diff).toBeFalsy(); | ||
}); | ||
|
||
test('provide input as Blob', async () => { | ||
const merger = new PDFMerger() | ||
const Testfile_A_Uint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) | ||
const Testfile_A_Blob = new Blob([Testfile_A_Uint8Array]) | ||
expect(Testfile_A_Blob).toBeInstanceOf(Blob) | ||
await merger.add(Testfile_A_Blob) | ||
const Testfile_B_Uint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_B.pdf')) | ||
const Testfile_B_Blob = new Blob([Testfile_B_Uint8Array]) | ||
expect(Testfile_B_Blob).toBeInstanceOf(Blob) | ||
await merger.add(Testfile_B_Blob) | ||
await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) | ||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), | ||
path.join(TMP_DIR, 'Testfile_AB.pdf') | ||
) | ||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('provide input as URL', async () => { | ||
const merger = new PDFMerger() | ||
const Testfile_A_URL = new URL('https://raw.githubusercontent.com/nbesli/pdf-merger-js/master/test/fixtures/Testfile_A.pdf') | ||
expect(Testfile_A_URL).toBeInstanceOf(URL) | ||
await merger.add(Testfile_A_URL) | ||
const Testfile_B_URL = new URL('https://raw.githubusercontent.com/nbesli/pdf-merger-js/master/test/fixtures/Testfile_B.pdf') | ||
expect(Testfile_B_URL).toBeInstanceOf(URL) | ||
await merger.add(Testfile_B_URL) | ||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), | ||
path.join(TMP_DIR, 'Testfile_AB.pdf') | ||
) | ||
expect(diff).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe('test valid output formats', () => { | ||
test('saveAsBuffer returns a Buffer', async () => { | ||
const merger = new PDFMerger() | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf')) | ||
const buffer = await merger.saveAsBuffer() | ||
expect(buffer instanceof Buffer).toEqual(true) | ||
}) | ||
}) | ||
|
||
describe('test valid page definitions', () => { | ||
test('combine pages from multiple books (array)', async () => { | ||
const merger = new PDFMerger() | ||
const tmpFile = 'MergeDemo1.pdf' | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) | ||
await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), [1, 2, 3]) | ||
await merger.save(path.join(TMP_DIR, tmpFile)) | ||
|
||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'MergeDemo.pdf'), | ||
path.join(TMP_DIR, tmpFile) | ||
) | ||
|
||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('combine pages from multiple books (string - array)', async () => { | ||
const merger = new PDFMerger() | ||
const tmpFile = 'MergeDemo1.pdf' | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), '1') | ||
await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), [1, 2, 3]) | ||
await merger.save(path.join(TMP_DIR, tmpFile)) | ||
|
||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'MergeDemo.pdf'), | ||
path.join(TMP_DIR, tmpFile) | ||
) | ||
|
||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('combine pages from multiple books (plain number - array)', async () => { | ||
const merger = new PDFMerger() | ||
const tmpFile = 'MergeDemo1.pdf' | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), 1) | ||
await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), [1, 2, 3]) | ||
await merger.save(path.join(TMP_DIR, tmpFile)) | ||
|
||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'MergeDemo.pdf'), | ||
path.join(TMP_DIR, tmpFile) | ||
) | ||
|
||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('combine pages from multiple books (start - end)', async () => { | ||
const merger = new PDFMerger() | ||
const tmpFile = 'MergeDemo2.pdf' | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) | ||
await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), '1 - 3') | ||
await merger.save(path.join(TMP_DIR, tmpFile)) | ||
|
||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'MergeDemo.pdf'), | ||
path.join(TMP_DIR, tmpFile) | ||
) | ||
|
||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('combine pages from multiple books (start to end)', async () => { | ||
const merger = new PDFMerger() | ||
const tmpFile = 'MergeDemo2.pdf' | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) | ||
await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), '1 to 3') | ||
await merger.save(path.join(TMP_DIR, tmpFile)) | ||
|
||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'MergeDemo.pdf'), | ||
path.join(TMP_DIR, tmpFile) | ||
) | ||
|
||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('combine pages from multiple books (start-end)', async () => { | ||
const merger = new PDFMerger() | ||
const tmpFile = 'MergeDemo2.pdf' | ||
await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) | ||
await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), '1-3') | ||
await merger.save(path.join(TMP_DIR, tmpFile)) | ||
|
||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'MergeDemo.pdf'), | ||
path.join(TMP_DIR, tmpFile) | ||
) | ||
|
||
expect(diff).toBeFalsy() | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
await fs.remove(TMP_DIR) | ||
}) | ||
}) |
Oops, something went wrong.