use stylist::{css, StyleSource};
use wasm_bindgen_test::*;
use web_sys::window;
use yew::prelude::*;
use yew::{utils, App};
pub enum Msg {
Clicked(MouseEvent),
}
pub struct NavbarItem {
link: ComponentLink<Self>,
props: Props,
}
#[derive(Clone, PartialEq, Properties)]
pub struct Props {
#[prop_or_default]
pub code_ref: NodeRef,
#[prop_or_default]
pub key: String,
#[prop_or_default]
pub class_name: String,
#[prop_or_default]
pub id: String,
#[prop_or(true)]
pub interaction_effect: bool,
#[prop_or(false)]
pub active: bool,
#[prop_or(Callback::noop())]
pub onclick_signal: Callback<MouseEvent>,
#[prop_or(css!(""))]
pub styles: StyleSource<'static>,
pub children: Children,
}
impl Component for NavbarItem {
type Message = Msg;
type Properties = Props;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
NavbarItem { link, props }
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Clicked(mouse_event) => {
self.props.onclick_signal.emit(mouse_event);
}
}
true
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
if self.props != props {
self.props = props;
true
} else {
false
}
}
fn view(&self) -> Html {
html! {
<div
class=classes!("navbar-item", if self.props.active {
"active"
} else {
""
},
if self.props.interaction_effect {
"interaction"
} else {
""
},
self.props.class_name.clone(),
self.props.styles.clone()
)
id=self.props.id.clone()
key=self.props.key.clone()
ref=self.props.code_ref.clone()
onclick=self.link.callback(Msg::Clicked)
>
{self.props.children.clone()}
</div>
}
}
}
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn should_create_navbar_item() {
let navbar_item_props = Props {
key: "".to_string(),
code_ref: NodeRef::default(),
class_name: "navbar-item-test".to_string(),
id: "navbar-item-id-test".to_string(),
onclick_signal: Callback::noop(),
active: false,
interaction_effect: true,
styles: css!("background-color: #918d94;"),
children: Children::new(vec![html! {
<div id="item">{"Item"}</div>
}]),
};
let navbar_item: App<NavbarItem> = App::new();
navbar_item.mount_with_props(
utils::document().get_element_by_id("output").unwrap(),
navbar_item_props,
);
let item_element = utils::document().get_element_by_id("item").unwrap();
assert_eq!(item_element.text_content().unwrap(), "Item".to_string());
}
#[wasm_bindgen_test]
fn should_create_clickable_navbar_item() {
let on_add_item_div = Callback::from(|_| {
let body = window().unwrap().document().unwrap().body().unwrap();
let child_element = window()
.unwrap()
.document()
.unwrap()
.create_element("div")
.unwrap();
child_element.set_text_content(Some("item2"));
child_element.set_id("item2");
body.append_child(&child_element).unwrap();
});
let navbar_item_props = Props {
key: "".to_string(),
code_ref: NodeRef::default(),
class_name: "navbar-item-test".to_string(),
id: "navbar-item-id-test".to_string(),
active: false,
interaction_effect: true,
onclick_signal: on_add_item_div,
styles: css!("background-color: #918d94;"),
children: Children::new(vec![html! {
<div id="item">{"Item"}</div>
}]),
};
let mouse_event = MouseEvent::new("click").unwrap();
navbar_item_props.onclick_signal.emit(mouse_event);
let updated_content = window()
.unwrap()
.document()
.unwrap()
.get_element_by_id("item2")
.unwrap()
.text_content()
.unwrap();
assert_eq!(updated_content, "item2".to_string());
}