mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-27 03:31:35 +00:00
Support asynchronous tests (#600)
* Tweak the implementation of heap closures This commit updates the implementation of the `Closure` type to internally store an `Rc` and be suitable for dropping a `Closure` during the execution of the closure. This is currently needed for promises but may be generally useful as well! * Support asynchronous tests This commit adds support for executing tests asynchronously. This is modeled by tests returning a `Future` instead of simply executing inline, and is signified with `#[wasm_bindgen_test(async)]`. Support for this is added through a new `wasm-bindgen-futures` crate which is a binding between the `futures` crate and JS `Promise` objects. Lots more details can be found in the details of the commit, but one of the end results is that the `web-sys` tests are now entirely contained in the same test suite and don't need `npm install` to be run to execute them! * Review tweaks * Add some bindings for `Function.call` to `js_sys` Name them `call0`, `call1`, `call2`, ... for the number of arguments being passed. * Use oneshots channels with `JsFuture` It did indeed clean up the implementation!
This commit is contained in:
@ -1,52 +0,0 @@
|
||||
use super::websys_project;
|
||||
|
||||
#[test]
|
||||
fn event() {
|
||||
websys_project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(use_extern_macros)]
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
extern crate web_sys;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn test_event(event: &web_sys::Event) {
|
||||
// These should match `new Event`.
|
||||
assert!(event.bubbles());
|
||||
assert!(event.cancelable());
|
||||
assert!(event.composed());
|
||||
|
||||
// The default behavior not initially prevented, but after
|
||||
// we call `prevent_default` it better be.
|
||||
assert!(!event.default_prevented());
|
||||
event.prevent_default();
|
||||
assert!(event.default_prevented());
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export async function test() {
|
||||
await new Promise(resolve => {
|
||||
window.addEventListener("test-event", e => {
|
||||
wasm.test_event(e);
|
||||
resolve();
|
||||
});
|
||||
|
||||
window.dispatchEvent(new Event("test-event", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
composed: true,
|
||||
}));
|
||||
});
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
#![cfg(not(target_arch = "wasm32"))]
|
||||
|
||||
extern crate wasm_bindgen_test_project_builder as project_builder;
|
||||
use project_builder::{project, Project};
|
||||
|
||||
mod event;
|
||||
|
||||
fn websys_project() -> Project {
|
||||
project()
|
||||
.add_local_dependency("web-sys", env!("CARGO_MANIFEST_DIR"))
|
||||
.headless(true)
|
||||
.clone()
|
||||
}
|
10
crates/web-sys/tests/wasm/event.js
Normal file
10
crates/web-sys/tests/wasm/event.js
Normal file
@ -0,0 +1,10 @@
|
||||
export function new_event() {
|
||||
return new Promise(resolve => {
|
||||
window.addEventListener("test-event", resolve);
|
||||
window.dispatchEvent(new Event("test-event", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
composed: true,
|
||||
}));
|
||||
});
|
||||
}
|
29
crates/web-sys/tests/wasm/event.rs
Normal file
29
crates/web-sys/tests/wasm/event.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use futures::future::Future;
|
||||
use js_sys::Promise;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen_futures::JsFuture;
|
||||
use wasm_bindgen_test::*;
|
||||
use web_sys::Event;
|
||||
|
||||
#[wasm_bindgen(module = "./tests/wasm/event.js")]
|
||||
extern {
|
||||
fn new_event() -> Promise;
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test(async)]
|
||||
fn event() -> impl Future<Item = (), Error = JsValue> {
|
||||
JsFuture::from(new_event())
|
||||
.map(Event::from)
|
||||
.map(|event| {
|
||||
// These should match `new Event`.
|
||||
assert!(event.bubbles());
|
||||
assert!(event.cancelable());
|
||||
assert!(event.composed());
|
||||
|
||||
// The default behavior not initially prevented, but after
|
||||
// we call `prevent_default` it better be.
|
||||
assert!(!event.default_prevented());
|
||||
event.prevent_default();
|
||||
assert!(event.default_prevented());
|
||||
})
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
#![feature(use_extern_macros)]
|
||||
#![cfg(target_arch = "wasm32")]
|
||||
|
||||
extern crate futures;
|
||||
extern crate js_sys;
|
||||
extern crate wasm_bindgen;
|
||||
extern crate wasm_bindgen_futures;
|
||||
extern crate wasm_bindgen_test;
|
||||
extern crate web_sys;
|
||||
|
||||
@ -14,6 +16,7 @@ pub mod br_element;
|
||||
pub mod button_element;
|
||||
pub mod div_element;
|
||||
pub mod element;
|
||||
pub mod event;
|
||||
pub mod head_element;
|
||||
pub mod heading_element;
|
||||
pub mod headers;
|
||||
|
Reference in New Issue
Block a user