Skip to content

Commit

Permalink
Add tests for File and Blob serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
octet-stream committed Mar 12, 2024
1 parent 720dba4 commit bc906fe
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/objectToFormData.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import test from "ava"

import {FormData as FormDataNode} from "formdata-node"
import {stub} from "sinon"
import {
FormData as FormDataNode,
File as FormDataFile,
Blob as FormDataBlob
} from "formdata-node"
import {createSandbox} from "sinon"

import {objectToFormData} from "./objectToFormData.js"

Expand All @@ -24,6 +28,14 @@ test.before(() => {
if (typeof globalThis.FormData === "undefined") {
globalThis.FormData = FormDataNode
}

if (typeof globalThis.File === "undefined") {
globalThis.File = FormDataFile
}

if (typeof globalThis.Blob === "undefined") {
globalThis.Blob = FormDataBlob
}
})

test("Returns FormData", t => {
Expand All @@ -41,17 +53,41 @@ test("Accepts custom FormData", t => {
})

test("Calls .append() method if FormData does not implement .set()", t => {
const appendStub = stub(FormDataNode.prototype, "append")
const setStub = stub(FormDataNode.prototype, "set").get(() => undefined)
const sandbox = createSandbox()

const appendStub = sandbox.stub(FormDataNode.prototype, "append")

sandbox.stub(FormDataNode.prototype, "set").get(() => undefined)

objectToFormData({key: "value"}, {
FormData: FormDataNode
})

t.true(appendStub.called)

setStub.reset()
appendStub.reset()
sandbox.restore()
})

test("Returns File as is", async t => {
const file = new File(["Test file content"], "test.txt", {type: "text/plain"})

const form = objectToFormData({file})

const actual = form.get("file") as File

t.true(actual instanceof File)
t.is(await actual.text(), await file.text())
})

test("Returns Blob as is", async t => {
const blob = new Blob(["Test file content"], {type: "text/plain"})

const form = objectToFormData({blob})

const actual = form.get("blob") as File

t.true(actual instanceof File)
t.is(await actual.text(), await blob.text())
})

test("Returns empty FormData for empty object", withSetializerTest, {
Expand Down

0 comments on commit bc906fe

Please sign in to comment.