Skip to content

Commit e46e314

Browse files
committed
Add a test for pdfDocument::fieldObjects
1 parent 3fa3cb6 commit e46e314

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

test/unit/document_spec.js

+71-6
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,31 @@ describe("document", function () {
4545
});
4646

4747
describe("PDFDocument", function () {
48-
const pdfManager = {
49-
get docId() {
50-
return "d0";
51-
},
52-
};
5348
const stream = new StringStream("Dummy_PDF_data");
5449

5550
function getDocument(acroForm, xref = new XRefMock()) {
51+
const catalog = { acroForm };
52+
const pdfManager = {
53+
get docId() {
54+
return "d0";
55+
},
56+
ensureCatalog(prop, args) {
57+
return pdfManager.ensure(catalog, prop, args);
58+
},
59+
ensure(obj, prop, args) {
60+
return new Promise(function (resolve) {
61+
const value = obj[prop];
62+
if (typeof value === "function") {
63+
resolve(value.apply(obj, args));
64+
} else {
65+
resolve(value);
66+
}
67+
});
68+
},
69+
};
5670
const pdfDocument = new PDFDocument(pdfManager, stream);
5771
pdfDocument.xref = xref;
58-
pdfDocument.catalog = { acroForm };
72+
pdfDocument.catalog = catalog;
5973
return pdfDocument;
6074
}
6175

@@ -174,5 +188,56 @@ describe("document", function () {
174188
pdfDocument = getDocument(acroForm);
175189
expect(pdfDocument.calculationOrderIds).toEqual(null);
176190
});
191+
192+
it("should get field objects array or null", async function () {
193+
const acroForm = new Dict();
194+
195+
let pdfDocument = getDocument(acroForm);
196+
let fields = await pdfDocument.fieldObjects;
197+
expect(fields).toEqual(null);
198+
199+
acroForm.set("Fields", []);
200+
pdfDocument = getDocument(acroForm);
201+
fields = await pdfDocument.fieldObjects;
202+
expect(fields).toEqual(null);
203+
204+
const kid1Ref = Ref.get(314, 0);
205+
const kid11Ref = Ref.get(159, 0);
206+
const kid2Ref = Ref.get(265, 0);
207+
const parentRef = Ref.get(358, 0);
208+
209+
const allFields = Object.create(null);
210+
for (const name of ["parent", "kid1", "kid2", "kid11"]) {
211+
const buttonWidgetDict = new Dict();
212+
buttonWidgetDict.set("Type", Name.get("Annot"));
213+
buttonWidgetDict.set("Subtype", Name.get("Widget"));
214+
buttonWidgetDict.set("FT", Name.get("Btn"));
215+
buttonWidgetDict.set("T", name);
216+
allFields[name] = buttonWidgetDict;
217+
}
218+
219+
allFields.kid1.set("Kids", [kid11Ref]);
220+
allFields.parent.set("Kids", [kid1Ref, kid2Ref]);
221+
222+
const xref = new XRefMock([
223+
{ ref: parentRef, data: allFields.parent },
224+
{ ref: kid1Ref, data: allFields.kid1 },
225+
{ ref: kid11Ref, data: allFields.kid11 },
226+
{ ref: kid2Ref, data: allFields.kid2 },
227+
]);
228+
229+
acroForm.set("Fields", [parentRef]);
230+
pdfDocument = getDocument(acroForm, xref);
231+
fields = await pdfDocument.fieldObjects;
232+
233+
for (const [name, objs] of Object.entries(fields)) {
234+
fields[name] = objs.map(obj => obj.id);
235+
}
236+
237+
expect(fields["parent.kid1"]).toEqual(["314R"]);
238+
expect(fields["parent.kid1.kid11"]).toEqual(["159R"]);
239+
expect(fields["parent.kid2"]).toEqual(["265R"]);
240+
expect(fields.parent).toEqual(["358R"]);
241+
});
177242
});
178243
});

0 commit comments

Comments
 (0)