@@ -726,7 +726,8 @@ pub mod tests {
726726 use super :: * ;
727727
728728 use crate as seed; // required for macros to work.
729- use crate :: { div, li, prelude:: * } ;
729+ use crate :: { class, div, li, prelude:: * , span} ;
730+ use wasm_bindgen:: JsCast ;
730731
731732 #[ derive( Clone , Debug ) ]
732733 enum Msg { }
@@ -737,6 +738,18 @@ pub mod tests {
737738 vdom
738739 }
739740
741+ fn setup_and_patch (
742+ doc : & Document ,
743+ parent : & Element ,
744+ mailbox : & Mailbox < Msg > ,
745+ old_vdom : El < Msg > ,
746+ new_vdom : El < Msg > ,
747+ ) -> El < Msg > {
748+ let mut new_vdom = make_vdom ( & doc, new_vdom) ;
749+ patch ( & doc, old_vdom, & mut new_vdom, parent, mailbox) ;
750+ new_vdom
751+ }
752+
740753 #[ wasm_bindgen_test]
741754 fn el_added ( ) {
742755 let mailbox = Mailbox :: new ( |_msg : Msg | { } ) ;
@@ -752,34 +765,28 @@ pub mod tests {
752765 assert_eq ! ( parent. children( ) . length( ) , 1 ) ;
753766 assert_eq ! ( old_ws. child_nodes( ) . length( ) , 0 ) ;
754767
755- vdom = {
756- let mut new_vdom = make_vdom ( & doc, div ! [ "text" ] ) ;
757- patch ( & doc, vdom, & mut new_vdom, & parent, & mailbox) ;
758-
759- assert_eq ! ( parent. children( ) . length( ) , 1 ) ;
760- assert ! ( old_ws. is_same_node( parent. first_child( ) . as_ref( ) ) ) ;
761- assert_eq ! ( old_ws. child_nodes( ) . length( ) , 1 ) ;
762- assert_eq ! ( old_ws. first_child( ) . unwrap( ) . text_content( ) . unwrap( ) , "text" ) ;
763-
764- new_vdom
765- } ;
768+ vdom = setup_and_patch ( & doc, & parent, & mailbox, vdom, div ! [ "text" ] ) ;
769+ assert_eq ! ( parent. children( ) . length( ) , 1 ) ;
770+ assert ! ( old_ws. is_same_node( parent. first_child( ) . as_ref( ) ) ) ;
771+ assert_eq ! ( old_ws. child_nodes( ) . length( ) , 1 ) ;
772+ assert_eq ! ( old_ws. first_child( ) . unwrap( ) . text_content( ) . unwrap( ) , "text" ) ;
773+
774+ setup_and_patch (
775+ & doc,
776+ & parent,
777+ & mailbox,
778+ vdom,
779+ div ! [ "text" , "more text" , vec![ li![ "even more text" ] ] ] ,
780+ ) ;
766781
767- {
768- let mut new_vdom = make_vdom (
769- & doc,
770- div ! [ "text" , "more text" , vec![ li![ "even more text" ] ] ] ,
771- ) ;
772- patch ( & doc, vdom, & mut new_vdom, & parent, & mailbox) ;
773-
774- assert_eq ! ( parent. children( ) . length( ) , 1 ) ;
775- assert ! ( old_ws. is_same_node( parent. first_child( ) . as_ref( ) ) ) ;
776- assert_eq ! ( old_ws. child_nodes( ) . length( ) , 3 ) ;
777- assert_eq ! ( old_ws. child_nodes( ) . item( 0 ) . unwrap( ) . text_content( ) . unwrap( ) , "text" ) ;
778- assert_eq ! ( old_ws. child_nodes( ) . item( 1 ) . unwrap( ) . text_content( ) . unwrap( ) , "more text" ) ;
779- let child3 = old_ws. child_nodes ( ) . item ( 2 ) . unwrap ( ) ;
780- assert_eq ! ( child3. node_name( ) , "LI" ) ;
781- assert_eq ! ( child3. text_content( ) . unwrap( ) , "even more text" ) ;
782- }
782+ assert_eq ! ( parent. children( ) . length( ) , 1 ) ;
783+ assert ! ( old_ws. is_same_node( parent. first_child( ) . as_ref( ) ) ) ;
784+ assert_eq ! ( old_ws. child_nodes( ) . length( ) , 3 ) ;
785+ assert_eq ! ( old_ws. child_nodes( ) . item( 0 ) . unwrap( ) . text_content( ) . unwrap( ) , "text" ) ;
786+ assert_eq ! ( old_ws. child_nodes( ) . item( 1 ) . unwrap( ) . text_content( ) . unwrap( ) , "more text" ) ;
787+ let child3 = old_ws. child_nodes ( ) . item ( 2 ) . unwrap ( ) ;
788+ assert_eq ! ( child3. node_name( ) , "LI" ) ;
789+ assert_eq ! ( child3. text_content( ) . unwrap( ) , "even more text" ) ;
783790 }
784791
785792 #[ wasm_bindgen_test]
@@ -795,35 +802,73 @@ pub mod tests {
795802 parent. append_child ( & old_ws) . unwrap ( ) ;
796803
797804 // First add some child nodes using the vdom
798- vdom = {
799- let mut new_vdom = make_vdom (
800- & doc,
801- div ! [ "text" , "more text" , vec![ li![ "even more text" ] ] ] ,
802- ) ;
803- patch ( & doc, vdom, & mut new_vdom, & parent, & mailbox) ;
804-
805- assert_eq ! ( parent. children( ) . length( ) , 1 ) ;
806- new_vdom
807- } ;
805+ vdom = setup_and_patch (
806+ & doc,
807+ & parent,
808+ & mailbox,
809+ vdom,
810+ div ! [ "text" , "more text" , vec![ li![ "even more text" ] ] ] ,
811+ ) ;
808812
809813 assert_eq ! ( parent. children( ) . length( ) , 1 ) ;
810814 assert_eq ! ( old_ws. child_nodes( ) . length( ) , 3 ) ;
811815 let old_child1 = old_ws. child_nodes ( ) . item ( 0 ) . unwrap ( ) ;
812816
813817 // Now test that patch function removes the last 2 nodes
814- {
815- let mut new_vdom = make_vdom ( & doc, div ! [ "text" ] ) ;
816- patch ( & doc, vdom, & mut new_vdom, & parent, & mailbox) ;
817-
818- assert_eq ! ( parent. children( ) . length( ) , 1 ) ;
819- assert ! ( old_ws. is_same_node( parent. first_child( ) . as_ref( ) ) ) ;
820- assert_eq ! ( old_ws. child_nodes( ) . length( ) , 1 ) ;
821- assert ! ( old_child1. is_same_node( old_ws. child_nodes( ) . item( 0 ) . as_ref( ) ) ) ;
822- }
818+ setup_and_patch (
819+ & doc,
820+ & parent,
821+ & mailbox,
822+ vdom,
823+ div ! [ "text" ] ,
824+ ) ;
825+
826+ assert_eq ! ( parent. children( ) . length( ) , 1 ) ;
827+ assert ! ( old_ws. is_same_node( parent. first_child( ) . as_ref( ) ) ) ;
828+ assert_eq ! ( old_ws. child_nodes( ) . length( ) , 1 ) ;
829+ assert ! ( old_child1. is_same_node( old_ws. child_nodes( ) . item( 0 ) . as_ref( ) ) ) ;
823830 }
824831
825- // #[wasm_bindgen_test]
826- fn _el_changed ( ) {
827- unimplemented ! ( )
832+ #[ wasm_bindgen_test]
833+ fn el_changed ( ) {
834+ let mailbox = Mailbox :: new ( |_msg : Msg | { } ) ;
835+
836+ let doc = util:: document ( ) ;
837+ let parent = doc. create_element ( "div" ) . unwrap ( ) ;
838+
839+ let mut vdom = make_vdom ( & doc, El :: empty ( seed:: dom_types:: Tag :: Div ) ) ;
840+ // clone so we can keep using it after vdom is modified
841+ let old_ws = vdom. el_ws . as_ref ( ) . unwrap ( ) . clone ( ) ;
842+ parent. append_child ( & old_ws) . unwrap ( ) ;
843+
844+ // First add some child nodes using the vdom
845+ vdom = setup_and_patch (
846+ & doc,
847+ & parent,
848+ & mailbox,
849+ vdom,
850+ div ! [ span![ "hello" ] , ", " , span![ "world" ] ] ,
851+ ) ;
852+
853+ assert_eq ! ( parent. child_nodes( ) . length( ) , 1 ) ;
854+ assert_eq ! ( old_ws. child_nodes( ) . length( ) , 3 ) ;
855+
856+ // Now add some attributes
857+ setup_and_patch (
858+ & doc,
859+ & parent,
860+ & mailbox,
861+ vdom,
862+ div ! [
863+ span![ class![ "first" ] , "hello" ] ,
864+ ", " ,
865+ span![ class![ "second" ] , "world" ] ,
866+ ] ,
867+ ) ;
868+
869+ let child1 = old_ws. child_nodes ( ) . item ( 0 ) . unwrap ( ) . dyn_into :: < Element > ( ) . unwrap ( ) ;
870+ assert_eq ! ( child1. get_attribute( "class" ) , Some ( "first" . to_string( ) ) ) ;
871+ let child3 = old_ws. child_nodes ( ) . item ( 2 ) . unwrap ( ) . dyn_into :: < Element > ( ) . unwrap ( ) ;
872+ assert_eq ! ( child3. get_attribute( "class" ) , Some ( "second" . to_string( ) ) ) ;
828873 }
829874}
0 commit comments