Skip to content

Commit

Permalink
Add test stub
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma-andex committed Jan 4, 2024
1 parent f154fdd commit 896ad6d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ tokenizers = { version = "0.15.0", features = ["unstable_wasm"], default-feature
serde = {version = "1.0.193", features = ["derive"]}
serde_json = "1.0.108"
js-sys = "0.3.64"
wasm-bindgen-futures = "0.4.39"
anyhow = "1.0"

[dependencies.web-sys]
features = [
'console'
'console',
'Headers',
'Request',
'RequestInit',
'RequestMode',
'Response',
'Window',
]
version = "0.3.64"

Expand Down
34 changes: 33 additions & 1 deletion tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,43 @@
#![cfg(target_arch = "wasm32")]

extern crate wasm_bindgen_test;
use std::println;

use wasm_bindgen::{prelude::*, JsValue};
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::*;
use web_sys::console;
use web_sys::{Request, RequestInit, RequestMode, Response};

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn pass() {
async fn pass() -> Result<(), JsValue> {
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);

let url = "https://api.github.com/repos/text-yoga/transformers-wasm/branches/main";

let request = Request::new_with_str_and_init(&url, &opts)?;

request
.headers()
.set("Accept", "application/vnd.github.v3+json")?;

let window = web_sys::window().unwrap();
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?;

// `resp_value` is a `Response` object.
assert!(resp_value.is_instance_of::<Response>());
let resp: Response = resp_value.dyn_into().unwrap();

// Convert this other `Promise` into a rust `Future`.
let json = JsFuture::from(resp.json()?).await?;

let json_str = js_sys::JSON::stringify(&json)?;

console::log_2(&"Logging arbitrary values looks like".into(), &json_str);
assert_eq!(1 + 1, 2);
Ok(())
}

0 comments on commit 896ad6d

Please sign in to comment.