Hello,
What is an appropriate way to unit test nested element macros?
e.g. if I have a function like so and I want to write a unit test for checking it returns some or all of the expected content:
fn draw_title(){
div![h1!["some string"]]
}
I don't have a good answer at the moment. Two approaches that come to mind depending on your intent are to compare the macro's output to the 1) Vdom code (ie Node, El etc), or 2) To web_sys code (ie web_sys::Node, web_sys::Element etc). From a preliminary look, I don't think we expose the required functions to do this. Ie seed::make_websys_el could be used for case 2, but is private.
What are you specifically trying to test? Perhaps we need to expose seed::make_websys_el.
In this FAQ page/file for instance, I would like to test the returned content contains a well formatted link to github: https://github.com/craigmayhew/bigprimes.net/blob/master/src/pages/faq.rs
This would allow me more confidence in my tests when upgrading seed-rs as a dependency.
I also intend to test this in functional testing with a web browser, but it's far quicker to get feedback from unit tests for an initial pass.
In this particular case, this pseudo-code explains what I might want to do:
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn render_test() {
assert_eq!(render().to_string().contains("<a class=\"link\" href=\"https://github.com/craigmayhew/bigprimes.net/\">github</a>"),true);
}
}
I'm working on adding a helper function that will return a Node's rendered HTML.
@David-OConnor Is it still in your todo list?
Hi @MartinKavik, this page from the tutorial links to this issue. Since it's closed, shouldn't the link be removed/updated?
Hi @aklajnert and thanks for the feedback!
You are right, feel free to update the text how you think it would make sense. Perhaps the link above with a test would be useful.
I will be glad. Thank you.
Most helpful comment
I'm working on adding a helper function that will return a
Node's rendered HTML.