-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make layout testing code pub
#2310
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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" | ||
); | ||
} |
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 @@ | ||
pub mod layout_tests; |
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does layout mean in this context?
From the code it does not seem like it renders anything 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm i looked at the tests bellow, this does seem to be comparing html