forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
105 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 |
---|---|---|
@@ -1,62 +1,59 @@ | ||
import { PDFDocument, PDFImage } from "src/api"; | ||
import { PngEmbedder } from "src/core"; | ||
import { toUint8Array } from "src/utils"; | ||
import { PDFDocument, PDFImage } from 'src/api'; | ||
import { PngEmbedder } from 'src/core'; | ||
import { toUint8Array } from 'src/utils'; | ||
|
||
|
||
const examplePngImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TxaoVBzuIdMhQnSyIijhKFYtgobQVWnUwufQLmjQkKS6OgmvBwY/FqoOLs64OroIg+AHi5uak6CIl/i8ptIjx4Lgf7+497t4BQqPCVLNrAlA1y0jFY2I2tyr2vKIfAgLoRVhipp5IL2bgOb7u4ePrXZRneZ/7cwwoeZMBPpF4jumGRbxBPLNp6Zz3iUOsJCnE58TjBl2Q+JHrsstvnIsOCzwzZGRS88QhYrHYwXIHs5KhEk8TRxRVo3wh67LCeYuzWqmx1j35C4N5bSXNdZphxLGEBJIQIaOGMiqwEKVVI8VEivZjHv4Rx58kl0yuMhg5FlCFCsnxg//B727NwtSkmxSMAd0vtv0xCvTsAs26bX8f23bzBPA/A1da219tALOfpNfbWuQIGNwGLq7bmrwH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ANtuirrux08HM0euisjDEMKC7RQuq+C+pVJqpzx3NZ3+eeBza9I0rWJgyHnxg2sAJrqnaHUzFcyN60Jox13hprv8aNopZBS4GcqWWVHM+lAkN0zY7ncgkYBukRoKLPpiXVj9UFkfV4Bdl8Jf60u3IMZZAG/6iLuhkDvaSZ74VqtUx3kp3NN7gUZt8RmA43a2eEY1OCfQ04AcBpAGkAKwpkBLIG8BfQE/eNJsvG/G4VlARj0BfjDBx2ECEIAABCAAAQhAAAIQgAAE+P/tN8YvpvbTDBOlAAAAAElFTkSuQmCC'; | ||
|
||
describe(`PDFImage`, () => { | ||
|
||
describe(`embed() method`, () => { | ||
it(`The embedder field should be cleared after the first call to embed()`, async () => { | ||
it(`clears the 'embedder' field after the first call`, async () => { | ||
const pdfDoc = await PDFDocument.create(); | ||
|
||
const bytes = toUint8Array(examplePngImage); | ||
const embedder = await PngEmbedder.for(bytes); | ||
const ref = pdfDoc.context.nextRef(); | ||
const pdfImage = PDFImage.of(ref, pdfDoc, embedder); | ||
|
||
const embedderVariable = "embedder"; | ||
const embedderVariable = 'embedder'; | ||
expect(pdfImage[embedderVariable]).toBeDefined(); | ||
await pdfImage.embed(); | ||
expect(pdfImage[embedderVariable]).toBeUndefined(); | ||
}); | ||
it(`The method embed() may be called several times without an error`, async () => { | ||
|
||
it(`may be called multiple times without causing an error`, async () => { | ||
const pdfDoc = await PDFDocument.create(); | ||
|
||
const bytes = toUint8Array(examplePngImage); | ||
const embedder = await PngEmbedder.for(bytes); | ||
const ref = pdfDoc.context.nextRef(); | ||
const pdfImage = PDFImage.of(ref, pdfDoc, embedder); | ||
|
||
const noErrorFunc = async () => { | ||
await pdfImage.embed(); | ||
await pdfImage.embed(); | ||
} | ||
|
||
await expect(noErrorFunc()).resolves.not.toThrowError(); | ||
await expect(pdfImage.embed()).resolves.not.toThrowError(); | ||
await expect(pdfImage.embed()).resolves.not.toThrowError(); | ||
}); | ||
it(`The method embed() may be called parallel without causing an error`, async () => { | ||
|
||
it(`may be called in parallel without causing an error`, async () => { | ||
const pdfDoc = await PDFDocument.create(); | ||
|
||
const bytes = toUint8Array(examplePngImage); | ||
const embedder = await PngEmbedder.for(bytes); | ||
const ref = pdfDoc.context.nextRef(); | ||
const pdfImage = PDFImage.of(ref, pdfDoc, embedder); | ||
|
||
const noErrorFunc = async () => { | ||
const embedderTaskVariable = "embedderTask"; | ||
const undefinedTask = pdfImage[embedderTaskVariable]; | ||
expect(undefinedTask).toBeUndefined(); | ||
const task1 = pdfImage.embed(); | ||
const firstTask = pdfImage[embedderTaskVariable]; | ||
const task2 = pdfImage.embed(); | ||
const secondTask = pdfImage[embedderTaskVariable]; | ||
await Promise.all([task1, task2]); | ||
expect(firstTask).toEqual(secondTask); | ||
} | ||
|
||
await expect(noErrorFunc()).resolves.not.toThrowError(); | ||
const task = () => pdfImage['embedTask']; | ||
|
||
expect(task()).toBeUndefined(); | ||
|
||
const task1 = pdfImage.embed(); | ||
const firstTask = task(); | ||
|
||
const task2 = pdfImage.embed(); | ||
const secondTask = task(); | ||
|
||
await Promise.all([task1, task2]); | ||
|
||
expect(firstTask).toEqual(secondTask); | ||
}); | ||
}); | ||
}); | ||
}); |