Skip to content

Commit fa47a8e

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

File tree

1 file changed

+76
-6
lines changed

1 file changed

+76
-6
lines changed

test/unit/document_spec.js

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

0 commit comments

Comments
 (0)