|
| 1 | +import test from "ava" |
| 2 | + |
| 3 | +import {File, Blob} from "formdata-node" |
| 4 | + |
| 5 | +import {FileLike} from "../FileLike" |
| 6 | + |
| 7 | +import isFile from "./isFile" |
| 8 | + |
| 9 | +test("Returns true for a File", t => { |
| 10 | + const file = new File(["Content"], "name.txt") |
| 11 | + |
| 12 | + t.true(isFile(file)) |
| 13 | +}) |
| 14 | + |
| 15 | +test("Returns true for a class that implements File", t => { |
| 16 | + class MyFile implements FileLike { |
| 17 | + name = "" |
| 18 | + |
| 19 | + type = "" |
| 20 | + |
| 21 | + size = 0 |
| 22 | + |
| 23 | + lastModified = Date.now() |
| 24 | + |
| 25 | + async* stream(): AsyncGenerator<Uint8Array> { |
| 26 | + yield new Uint8Array(0) |
| 27 | + } |
| 28 | + |
| 29 | + get [Symbol.toStringTag](): string { |
| 30 | + return "File" |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + t.true(isFile(new MyFile())) |
| 35 | +}) |
| 36 | + |
| 37 | +test("Returns true for a file-shaped object", t => { |
| 38 | + const object = { |
| 39 | + name: "", |
| 40 | + |
| 41 | + type: "", |
| 42 | + |
| 43 | + size: 0, |
| 44 | + |
| 45 | + lastModified: Date.now(), |
| 46 | + |
| 47 | + async* stream(): AsyncGenerator<Uint8Array> { |
| 48 | + yield new Uint8Array(0) |
| 49 | + }, |
| 50 | + |
| 51 | + get [Symbol.toStringTag](): string { |
| 52 | + return "File" |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + t.true(isFile(object)) |
| 57 | +}) |
| 58 | + |
| 59 | +test("Returns false for null", t => { |
| 60 | + t.false(isFile(null)) |
| 61 | +}) |
| 62 | + |
| 63 | +test("Returns false for undefined", t => { |
| 64 | + t.false(isFile(undefined)) |
| 65 | +}) |
| 66 | + |
| 67 | +test("Returns false for non-File object", t => { |
| 68 | + t.false(isFile(new Map())) |
| 69 | +}) |
| 70 | + |
| 71 | +test("Returns false for Blob", t => { |
| 72 | + const blob = new Blob(["Content"], {type: "text/plain"}) |
| 73 | + |
| 74 | + t.false(isFile(blob)) |
| 75 | +}) |
0 commit comments