Skip to content

Commit

Permalink
test: add test coverage for more FormData functions (#1283)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored Jan 7, 2025
1 parent 39ce1b9 commit cc06d8d
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 14 deletions.
2 changes: 1 addition & 1 deletion jscomp/runtime/js_blob.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ external make : string Js.iterator -> ?options:options -> unit -> t = "Blob"

external size : t -> float = "size"
[@@mel.get]
(** [size t] returns the size of the Blob or File in bytes *)
(** [size t] returns the size of the Blob in bytes *)

external type_ : t -> string = "type"
[@@mel.get]
Expand Down
12 changes: 6 additions & 6 deletions jscomp/runtime/js_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type options = {
endings : [ `transparent | `native ] option; [@mel.optional]
(** How to interpret newline characters (\n) within the contents, if the
data is text. The default value, transparent, copies newline
characters into the blob without changing them. To convert newlines
characters into the file without changing them. To convert newlines
to the host system's native convention, specify the value native. *)
lastModified : float option; [@mel.optional]
(** A number representing the number of milliseconds between the Unix
Expand Down Expand Up @@ -71,7 +71,7 @@ external name : t -> string = "name"
(* Since File is a subclass of Blob, it has all the properties and methods of Blob. *)
external size : t -> float = "size"
[@@mel.get]
(** [size t] returns the size of the Blob or File in bytes *)
(** [size t] returns the size of the File in bytes *)

external type_ : t -> string = "type"
[@@mel.get]
Expand All @@ -85,18 +85,18 @@ external arrayBuffer : t -> Js.arrayBuffer Js.promise = "arrayBuffer"
external bytes : t -> Js.uint8Array Js.promise = "bytes"
[@@mel.send]
(** [bytes t] returns a Promise that resolves with a [Js.uint8Array] containing
the contents of the blob as an array of bytes. *)
the contents of the file as an array of bytes. *)

external slice : ?start:int -> ?end_:int -> ?contentType:string -> t -> t
= "slice"
[@@mel.send]
(** [slice ?start ?end_ ?contentType t] creates and returns a new Blob object
which contains data from a subset of the blob on which it's called. *)
(** [slice ?start ?end_ ?contentType t] creates and returns a new File object
which contains data from a subset of the file on which it's called. *)

external text : t -> string Js.promise = "text"
[@@mel.send]
(** [text t] returns a Promise that resolves with a string containing the
contents of the blob, interpreted as UTF-8. *)
contents of the file, interpreted as UTF-8. *)

(* XXX: stream can't be bound until we have some bindings to ReadableStream *)
(* https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream *)
Expand Down
2 changes: 1 addition & 1 deletion jscomp/test/arity_deopt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let f2 = fun [@u] x y -> let a = x in fun z -> a + y + z
let f3 = fun x -> let a = x in fun [@u] y z -> a + y + z
(* be careful! When you start optimize functions of [@u], its call site invariant
(Ml_app) will not hold any more.
So the best is never shrink functons which could change arity
So the best is never shrink functions which could change arity
*)
let () =
begin
Expand Down
25 changes: 25 additions & 0 deletions jscomp/test/dist/jscomp/test/js_blob_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 33 additions & 1 deletion jscomp/test/dist/jscomp/test/js_file_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 31 additions & 2 deletions jscomp/test/js_blob_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,37 @@ let make_with_options () =
Mt.Eq (Js.Blob.type_ blob, "application/json")
;;

;; Mt.from_pair_suites __MODULE__ [
let blob_bytes =
let module TextDecoder = struct
type t
external make : string -> t = "TextDecoder" [@@mel.new]
external decode : t -> Js.uint8Array -> string = "decode" [@@mel.send]
let make_utf8 () = make "utf-8"
end
in
let decodeUint8Array: Js.uint8Array -> string = fun b ->
let decoder = TextDecoder.make_utf8 () in
TextDecoder.decode decoder b
in
fun () ->
let file =
Js.File.make
(Js.Array.values [|"hello"|])
~filename:"foo.txt"
()
in
Js.File.bytes file
|> Js.Promise.then_ (fun b ->
Js.Promise.resolve (Mt.Eq (decodeUint8Array b, "hello")))

;;

Mt.from_pair_suites __MODULE__ [
"make with options", make_with_options;
]
] ;;

Mt.from_promise_suites __MODULE__
[
("blob bytes", blob_bytes ());
]

23 changes: 22 additions & 1 deletion jscomp/test/js_file_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,29 @@ let make_with_options () =
Mt.Eq (Js.File.lastModified file, 0.)
;;

let file_size () =
let file =
Js.File.make
(Js.Array.values [|"hello"|])
~filename:"foo.txt"
()
in
Mt.Eq (Js.File.size file, 5.)
;;

let file_type () =
let file =
Js.File.make
(Js.Array.values [|"hello"|])
~filename:"foo.txt"
()
in
Mt.Eq (Js.File.type_ file, "")
;;

;; Mt.from_pair_suites __MODULE__ [
"make with options", make_with_options;
"file type", file_size;
"file size", file_type;
]


2 changes: 0 additions & 2 deletions jscomp/test/js_formdata_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,3 @@ Mt.from_promise_suites __MODULE__
("append file", form_data_append_file ());
]



0 comments on commit cc06d8d

Please sign in to comment.