use dioxus_native_core::prelude::TextNode;
use dioxus_native_core::NodeId;
use dioxus_native_core::{node::NodeType, real_dom::NodeImmutable};
use freya_core::node::NodeState;
use freya_node_state::CustomAttributeValues;
use torin::geometry::Area;
use crate::test_utils::TestUtils;
#[derive(Clone)]
pub struct TestNode {
pub(crate) node_id: NodeId,
pub(crate) utils: TestUtils,
pub(crate) height: u16,
pub(crate) children_ids: Vec<NodeId>,
pub(crate) state: NodeState,
pub(crate) node_type: NodeType<CustomAttributeValues>,
}
impl TestNode {
pub fn get(&self, child_index: usize) -> Self {
self.child(child_index).unwrap_or_else(|| {
panic!("Child by index {} not found", child_index);
})
}
pub fn child(&self, child_index: usize) -> Option<Self> {
let child_id = self.children_ids.get(child_index)?;
let child: TestNode = self.utils.get_node_by_id(*child_id);
Some(child)
}
pub fn text(&self) -> Option<&str> {
if let NodeType::Text(TextNode { text, .. }) = &self.node_type {
Some(text)
} else {
None
}
}
pub fn state(&self) -> &NodeState {
&self.state
}
pub fn layout(&self) -> Option<Area> {
self.utils()
.sdom()
.get()
.layout()
.get(self.node_id)
.map(|l| l.area)
}
pub fn utils(&self) -> &TestUtils {
&self.utils
}
pub fn parent_id(&self) -> Option<NodeId> {
let sdom = self.utils().sdom();
let fdom = sdom.get();
let dom = fdom.rdom();
let node = dom.get(self.node_id).unwrap();
node.parent_id()
}
pub fn dom_height(&self) -> u16 {
self.height
}
}