Skip to content

Commit

Permalink
make layout testing code public (#2310)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile authored Dec 28, 2021
1 parent 0268c7d commit c91364b
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 151 deletions.
2 changes: 2 additions & 0 deletions packages/yew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ pub mod context;
pub mod functional;
pub mod html;
pub mod scheduler;
#[cfg(test)]
pub mod tests;
pub mod utils;
pub mod virtual_dom;

Expand Down
142 changes: 142 additions & 0 deletions packages/yew/src/tests/layout_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use crate::html::{AnyScope, Scope};
use crate::virtual_dom::{VDiff, VNode, VText};
use crate::{Component, Context, Html};
use gloo::console::log;
use web_sys::Node;
use yew::NodeRef;

struct Comp;
impl Component for Comp {
type Message = ();
type Properties = ();

fn create(_: &Context<Self>) -> Self {
unimplemented!()
}

fn update(&mut self, _ctx: &Context<Self>, _: Self::Message) -> bool {
unimplemented!();
}

fn changed(&mut self, _ctx: &Context<Self>) -> bool {
unimplemented!()
}

fn view(&self, _ctx: &Context<Self>) -> Html {
unimplemented!()
}
}

#[derive(Debug)]
pub struct TestLayout<'a> {
pub name: &'a str,
pub node: VNode,
pub expected: &'a str,
}

pub fn diff_layouts(layouts: Vec<TestLayout<'_>>) {
let document = gloo_utils::document();
let parent_scope: AnyScope = Scope::<Comp>::new(None).into();
let parent_element = document.create_element("div").unwrap();
let parent_node: Node = parent_element.clone().into();
let end_node = document.create_text_node("END");
parent_node.append_child(&end_node).unwrap();
let mut empty_node: VNode = VText::new("").into();

// Tests each layout independently
let next_sibling = NodeRef::new(end_node.into());
for layout in layouts.iter() {
// Apply the layout
let mut node = layout.node.clone();
log!("Independently apply layout '{}'", layout.name);

node.apply(&parent_scope, &parent_element, next_sibling.clone(), None);
assert_eq!(
parent_element.inner_html(),
format!("{}END", layout.expected),
"Independent apply failed for layout '{}'",
layout.name,
);

// Diff with no changes
let mut node_clone = layout.node.clone();

log!("Independently reapply layout '{}'", layout.name);

node_clone.apply(
&parent_scope,
&parent_element,
next_sibling.clone(),
Some(node),
);
assert_eq!(
parent_element.inner_html(),
format!("{}END", layout.expected),
"Independent reapply failed for layout '{}'",
layout.name,
);

// Detach
empty_node.clone().apply(
&parent_scope,
&parent_element,
next_sibling.clone(),
Some(node_clone),
);
assert_eq!(
parent_element.inner_html(),
"END",
"Independent detach failed for layout '{}'",
layout.name,
);
}

// Sequentially apply each layout
let mut ancestor: Option<VNode> = None;
for layout in layouts.iter() {
let mut next_node = layout.node.clone();

log!("Sequentially apply layout '{}'", layout.name);
next_node.apply(
&parent_scope,
&parent_element,
next_sibling.clone(),
ancestor,
);
assert_eq!(
parent_element.inner_html(),
format!("{}END", layout.expected),
"Sequential apply failed for layout '{}'",
layout.name,
);
ancestor = Some(next_node);
}

// Sequentially detach each layout
for layout in layouts.into_iter().rev() {
let mut next_node = layout.node.clone();

log!("Sequentially detach layout '{}'", layout.name);
next_node.apply(
&parent_scope,
&parent_element,
next_sibling.clone(),
ancestor,
);
assert_eq!(
parent_element.inner_html(),
format!("{}END", layout.expected),
"Sequential detach failed for layout '{}'",
layout.name,
);
ancestor = Some(next_node);
}

// Detach last layout
empty_node.apply(&parent_scope, &parent_element, next_sibling, ancestor);
assert_eq!(
parent_element.inner_html(),
"END",
"Failed to detach last layout"
);
}
1 change: 1 addition & 0 deletions packages/yew/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod layout_tests;
147 changes: 3 additions & 144 deletions packages/yew/src/virtual_dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ mod tests_attr_value {
assert_eq!(b, c);
assert_eq!(a, c);

assert!(a == b);
assert!(b == c);
assert!(a == c);
assert_eq!(a, b);
assert_eq!(b, c);
assert_eq!(a, c);
}
}

Expand Down Expand Up @@ -538,147 +538,6 @@ pub(crate) fn insert_node(node: &Node, parent: &Element, next_sibling: Option<&N
};
}

#[cfg(test)]
mod layout_tests {
use super::*;
use crate::html::{AnyScope, Scope};
use crate::{Component, Context, Html};

struct Comp;
impl Component for Comp {
type Message = ();
type Properties = ();

fn create(_: &Context<Self>) -> Self {
unimplemented!()
}

fn update(&mut self, _ctx: &Context<Self>, _: Self::Message) -> bool {
unimplemented!();
}

fn changed(&mut self, _ctx: &Context<Self>) -> bool {
unimplemented!()
}

fn view(&self, _ctx: &Context<Self>) -> Html {
unimplemented!()
}
}

pub(crate) struct TestLayout<'a> {
pub(crate) name: &'a str,
pub(crate) node: VNode,
pub(crate) expected: &'a str,
}

pub(crate) fn diff_layouts(layouts: Vec<TestLayout<'_>>) {
let document = gloo_utils::document();
let parent_scope: AnyScope = Scope::<Comp>::new(None).into();
let parent_element = document.create_element("div").unwrap();
let parent_node: Node = parent_element.clone().into();
let end_node = document.create_text_node("END");
parent_node.append_child(&end_node).unwrap();
let mut empty_node: VNode = VText::new("").into();

// Tests each layout independently
let next_sibling = NodeRef::new(end_node.into());
for layout in layouts.iter() {
// Apply the layout
let mut node = layout.node.clone();
#[cfg(feature = "wasm_test")]
wasm_bindgen_test::console_log!("Independently apply layout '{}'", layout.name);
node.apply(&parent_scope, &parent_element, next_sibling.clone(), None);
assert_eq!(
parent_element.inner_html(),
format!("{}END", layout.expected),
"Independent apply failed for layout '{}'",
layout.name,
);

// Diff with no changes
let mut node_clone = layout.node.clone();
#[cfg(feature = "wasm_test")]
wasm_bindgen_test::console_log!("Independently reapply layout '{}'", layout.name);
node_clone.apply(
&parent_scope,
&parent_element,
next_sibling.clone(),
Some(node),
);
assert_eq!(
parent_element.inner_html(),
format!("{}END", layout.expected),
"Independent reapply failed for layout '{}'",
layout.name,
);

// Detach
empty_node.clone().apply(
&parent_scope,
&parent_element,
next_sibling.clone(),
Some(node_clone),
);
assert_eq!(
parent_element.inner_html(),
"END",
"Independent detach failed for layout '{}'",
layout.name,
);
}

// Sequentially apply each layout
let mut ancestor: Option<VNode> = None;
for layout in layouts.iter() {
let mut next_node = layout.node.clone();
#[cfg(feature = "wasm_test")]
wasm_bindgen_test::console_log!("Sequentially apply layout '{}'", layout.name);
next_node.apply(
&parent_scope,
&parent_element,
next_sibling.clone(),
ancestor,
);
assert_eq!(
parent_element.inner_html(),
format!("{}END", layout.expected),
"Sequential apply failed for layout '{}'",
layout.name,
);
ancestor = Some(next_node);
}

// Sequentially detach each layout
for layout in layouts.into_iter().rev() {
let mut next_node = layout.node.clone();
#[cfg(feature = "wasm_test")]
wasm_bindgen_test::console_log!("Sequentially detach layout '{}'", layout.name);
next_node.apply(
&parent_scope,
&parent_element,
next_sibling.clone(),
ancestor,
);
assert_eq!(
parent_element.inner_html(),
format!("{}END", layout.expected),
"Sequential detach failed for layout '{}'",
layout.name,
);
ancestor = Some(next_node);
}

// Detach last layout
empty_node.apply(&parent_scope, &parent_element, next_sibling, ancestor);
assert_eq!(
parent_element.inner_html(),
"END",
"Failed to detach last layout"
);
}
}

#[cfg(all(test, feature = "wasm_bench"))]
mod benchmarks {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/virtual_dom/vcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ mod layout_tests {
extern crate self as yew;

use crate::html;
use crate::virtual_dom::layout_tests::{diff_layouts, TestLayout};
use crate::tests::layout_tests::{diff_layouts, TestLayout};
use crate::{Children, Component, Context, Html, Properties};
use std::marker::PhantomData;

Expand Down
4 changes: 2 additions & 2 deletions packages/yew/src/virtual_dom/vlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ mod layout_tests {
extern crate self as yew;

use crate::html;
use crate::virtual_dom::layout_tests::{diff_layouts, TestLayout};
use crate::tests::layout_tests::{diff_layouts, TestLayout};

#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
Expand Down Expand Up @@ -426,7 +426,7 @@ mod layout_tests_keys {
extern crate self as yew;

use crate::html;
use crate::virtual_dom::layout_tests::{diff_layouts, TestLayout};
use crate::tests::layout_tests::{diff_layouts, TestLayout};
use crate::virtual_dom::VNode;
use crate::{Children, Component, Context, Html, Properties};
use web_sys::Node;
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/virtual_dom/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl PartialEq for VNode {
#[cfg(test)]
mod layout_tests {
use super::*;
use crate::virtual_dom::layout_tests::{diff_layouts, TestLayout};
use crate::tests::layout_tests::{diff_layouts, TestLayout};

#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/virtual_dom/vportal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mod layout_tests {
extern crate self as yew;

use crate::html;
use crate::virtual_dom::layout_tests::{diff_layouts, TestLayout};
use crate::tests::layout_tests::{diff_layouts, TestLayout};
use crate::virtual_dom::VNode;
use yew::virtual_dom::VPortal;

Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ mod layout_tests {
extern crate self as yew;

use crate::html;
use crate::virtual_dom::layout_tests::{diff_layouts, TestLayout};
use crate::tests::layout_tests::{diff_layouts, TestLayout};

#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/virtual_dom/vtext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ mod layout_tests {
extern crate self as yew;

use crate::html;
use crate::virtual_dom::layout_tests::{diff_layouts, TestLayout};
use crate::tests::layout_tests::{diff_layouts, TestLayout};

#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
Expand Down

0 comments on commit c91364b

Please sign in to comment.