diff --git a/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts b/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts index 841e358c36..622d10c1f2 100644 --- a/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts +++ b/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts @@ -3,6 +3,7 @@ import { requiresFormData, processParams } from "../../src/utils/process-params"; +import { attachmentFile } from "../../../arcgis-rest-feature-service/test/attachments.test"; describe("encodeFormData", () => { it("should encode in form data for multipart requests", () => { @@ -63,4 +64,49 @@ describe("encodeFormData", () => { "myNumber=380" ); }); + + it("should switch to form data if any item is not a basic type", () => { + const dateValue = 1471417200000; + const file = attachmentFile(); + if (!file.name) { + // The file's name is used for adding files to a form, so supply a name when we're in a testing + // environment that doesn't support File (attachmentFile creates a File with the name "foo.txt" + // if File is supported and a file stream otherwise) + file.name = "foo.txt"; + } + + // null, undefined, function are excluded. If you want to send an empty key you need to send an empty string "". + // See https://github.com/Esri/arcgis-rest-js/issues/18 + const params = { + myArray1: new Array(8), + myArray2: [1, 2, 4, 16], + myArray3: [{ a: 1, b: 2 }, { c: "abc" }], + myDate: new Date(dateValue), + myFunction: () => { + return 3.1415; + }, + myBoolean: true, + myString: "Hello, world!", + myEmptyString: "", + myNumber: 380, + file + }; + + expect(requiresFormData(params)).toBeTruthy(); + + const formData = processParams(params); + expect(typeof formData).toBe("object"); + expect(formData.myArray1).toBe(",,,,,,,"); + expect(formData.myArray2).toBe("1,2,4,16"); + expect(formData.myArray3).toBe('[{"a":1,"b":2},{"c":"abc"}]'); + expect(formData.myDate).toBe(dateValue); + expect(formData.myBoolean).toBeTruthy(); + expect(formData.myString).toBe("Hello, world!"); + expect(formData.myEmptyString).toBe(""); + expect(formData.myNumber).toBe(380); + expect(typeof formData.file).toBe("object"); + + const encodedFormData = encodeFormData(params); + expect(encodedFormData instanceof FormData).toBeTruthy(); + }); });