From 3ca037af78a3234b820411ec070a3b94e69f1163 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Tue, 18 Aug 2020 22:55:59 +0200 Subject: [PATCH] Combine the choice widget field value unit tests into one parametrized unit test This commit follows the same pattern as another unit test in this file and both reduces existing and future code duplication (since the next commit will extend this test with an additional input). --- test/unit/annotation_spec.js | 62 +++++++++++++----------------------- 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/test/unit/annotation_spec.js b/test/unit/annotation_spec.js index 134b946c0..aaba14975 100644 --- a/test/unit/annotation_spec.js +++ b/test/unit/annotation_spec.js @@ -2478,48 +2478,32 @@ describe("annotation", function () { }, done.fail); }); - it("should handle array field values", function (done) { - const fieldValue = ["Foo", "Bar"]; + it("should convert the field value to an array", function (done) { + const inputs = ["Foo", ["Foo", "Bar"]]; + const outputs = [["Foo"], ["Foo", "Bar"]]; - choiceWidgetDict.set("V", fieldValue); + let promise = Promise.resolve(); + for (let i = 0, ii = inputs.length; i < ii; i++) { + promise = promise.then(() => { + choiceWidgetDict.set("V", inputs[i]); - const choiceWidgetRef = Ref.get(968, 0); - const xref = new XRefMock([ - { ref: choiceWidgetRef, data: choiceWidgetDict }, - ]); + const choiceWidgetRef = Ref.get(968, 0); + const xref = new XRefMock([ + { ref: choiceWidgetRef, data: choiceWidgetDict }, + ]); - AnnotationFactory.create( - xref, - choiceWidgetRef, - pdfManagerMock, - idFactoryMock - ).then(({ data }) => { - expect(data.annotationType).toEqual(AnnotationType.WIDGET); - expect(data.fieldValue).toEqual(fieldValue); - done(); - }, done.fail); - }); - - it("should handle string field values", function (done) { - const fieldValue = "Foo"; - - choiceWidgetDict.set("V", fieldValue); - - const choiceWidgetRef = Ref.get(978, 0); - const xref = new XRefMock([ - { ref: choiceWidgetRef, data: choiceWidgetDict }, - ]); - - AnnotationFactory.create( - xref, - choiceWidgetRef, - pdfManagerMock, - idFactoryMock - ).then(({ data }) => { - expect(data.annotationType).toEqual(AnnotationType.WIDGET); - expect(data.fieldValue).toEqual([fieldValue]); - done(); - }, done.fail); + return AnnotationFactory.create( + xref, + choiceWidgetRef, + pdfManagerMock, + idFactoryMock + ).then(({ data }) => { + expect(data.annotationType).toEqual(AnnotationType.WIDGET); + expect(data.fieldValue).toEqual(outputs[i]); + }); + }); + } + promise.then(done, done.fail); }); it("should handle unknown flags", function (done) {