Skip to content

Commit

Permalink
Added test for FormData path #254
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeTschudi committed Jul 24, 2018
1 parent 5ba71cf commit a79227d
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/arcgis-rest-request/test/utils/encode-form-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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();
});
});

0 comments on commit a79227d

Please sign in to comment.