1 use js_sys::{Array, ArrayBuffer};
2 use wasm_bindgen::prelude::*;
3 use wasm_bindgen::JsCast;
4 use wasm_bindgen_futures::JsFuture;
5 use wasm_bindgen_test::*;
6 use web_sys::Blob;
7 
8 #[wasm_bindgen(module = "/tests/wasm/blob.js")]
9 extern "C" {
new_blob() -> Blob10     fn new_blob() -> Blob;
11 }
12 
13 #[wasm_bindgen_test]
test_blob_from_js()14 fn test_blob_from_js() {
15     let blob = new_blob();
16     assert!(blob.is_instance_of::<Blob>());
17     assert_eq!(blob.size(), 3.0);
18 }
19 
20 #[wasm_bindgen_test]
test_blob_from_bytes()21 fn test_blob_from_bytes() {
22     let bytes = Array::new();
23     bytes.push(&1.into());
24     bytes.push(&2.into());
25     bytes.push(&3.into());
26 
27     let blob = Blob::new_with_u8_array_sequence(&bytes.into()).unwrap();
28     assert!(blob.is_instance_of::<Blob>());
29     assert_eq!(blob.size(), 3.0);
30 }
31 
32 #[wasm_bindgen_test]
test_blob_empty()33 fn test_blob_empty() {
34     let blob = Blob::new().unwrap();
35     assert!(blob.is_instance_of::<Blob>());
36     assert_eq!(blob.size(), 0.0);
37 }
38 
39 #[wasm_bindgen_test]
test_blob_array_buffer()40 async fn test_blob_array_buffer() {
41     let bytes = Array::new();
42     bytes.push(&1.into());
43     bytes.push(&2.into());
44     bytes.push(&3.into());
45 
46     let blob = Blob::new_with_u8_array_sequence(&bytes.into()).unwrap();
47 
48     let buffer: ArrayBuffer = JsFuture::from(blob.array_buffer()).await.unwrap().into();
49 
50     assert!(blob.is_instance_of::<Blob>());
51     assert!(buffer.is_instance_of::<ArrayBuffer>());
52     assert_eq!(blob.size(), buffer.byte_length() as f64);
53 }
54 
55 #[wasm_bindgen_test]
test_blob_text()56 async fn test_blob_text() {
57     let strings = Array::new();
58     strings.push(&"hello".into());
59 
60     let blob = Blob::new_with_str_sequence(&strings.into()).unwrap();
61     let string = JsFuture::from(blob.text()).await.unwrap();
62 
63     assert!(blob.is_instance_of::<Blob>());
64     assert_eq!(string, "hello")
65 }
66