-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dispatch_custom_event to enable dispatching CustomEvent's
- Loading branch information
1 parent
e78db23
commit f85010e
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function new_custom_event() { | ||
return new Promise(resolve => { | ||
window.addEventListener("test-custom-event", resolve); | ||
window.dispatchEvent(new CustomEvent("test-custom-event", { | ||
detail: "detail", | ||
bubbles: true, | ||
cancelable: true, | ||
composed: true, | ||
})); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use js_sys::{Object, Promise}; | ||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen_futures::JsFuture; | ||
use wasm_bindgen_test::*; | ||
use web_sys::CustomEvent; | ||
|
||
#[wasm_bindgen(module = "/tests/wasm/custom_event.js")] | ||
extern "C" { | ||
fn new_custom_event() -> Promise; | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
async fn custom_event() { | ||
let result = JsFuture::from(new_custom_event()).await.unwrap(); | ||
let event = CustomEvent::from(result); | ||
// All DOM interfaces should inherit from `Object`. | ||
assert!(event.is_instance_of::<Object>()); | ||
let _: &Object = event.as_ref(); | ||
|
||
// These should match `new Event`. | ||
assert!(event.bubbles()); | ||
assert!(event.cancelable()); | ||
assert!(event.composed()); | ||
assert_eq!(event.detail().as_string().unwrap(), "detail"); | ||
|
||
// 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()); | ||
} |