From 8941c30ef92f1cd87164d8836d1656596dddcb48 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Thu, 14 May 2020 11:32:08 +0800 Subject: [PATCH] Add test foundation for component scope (#1224) --- yew/src/html/scope.rs | 80 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/yew/src/html/scope.rs b/yew/src/html/scope.rs index 8ef5e97c260..5f37b0decc3 100644 --- a/yew/src/html/scope.rs +++ b/yew/src/html/scope.rs @@ -336,3 +336,83 @@ where } } } + +#[cfg(test)] +mod tests { + use crate::html::*; + use crate::Properties; + use std::ops::Deref; + #[cfg(feature = "wasm_test")] + use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; + + #[cfg(feature = "wasm_test")] + wasm_bindgen_test_configure!(run_in_browser); + + #[derive(Clone, Properties)] + struct Props { + lifecycle: Rc>>, + } + struct Comp { + props: Props, + } + + impl Component for Comp { + type Message = (); + type Properties = Props; + + fn create(props: Self::Properties, _: ComponentLink) -> Self { + props.lifecycle.borrow_mut().push("create".into()); + Comp { props } + } + + fn rendered(&mut self, first_render: bool) { + self.props + .lifecycle + .borrow_mut() + .push(format!("rendered({})", first_render)); + } + + fn update(&mut self, _: Self::Message) -> ShouldRender { + self.props.lifecycle.borrow_mut().push("update".into()); + false + } + + fn change(&mut self, _: Self::Properties) -> ShouldRender { + self.props.lifecycle.borrow_mut().push("change".into()); + false + } + + fn view(&self) -> Html { + self.props.lifecycle.borrow_mut().push("view".into()); + html! {} + } + } + + impl Drop for Comp { + fn drop(&mut self) { + self.props.lifecycle.borrow_mut().push("drop".into()); + } + } + + #[test] + fn text_mount_in_place() { + let document = crate::utils::document(); + let lifecycle: Rc>> = Rc::default(); + let props = Props { + lifecycle: lifecycle.clone(), + }; + + let scope = Scope::::new(None); + let el = document.create_element("div").unwrap(); + scope.mount_in_place(el, None, NodeRef::default(), props); + + assert_eq!( + lifecycle.borrow_mut().deref(), + &vec![ + "create".to_string(), + "view".to_string(), + "rendered(true)".to_string() + ] + ); + } +}