-
Notifications
You must be signed in to change notification settings - Fork 84
/
diff_patch.rs
206 lines (179 loc) · 5.26 KB
/
diff_patch.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#![feature(proc_macro_hygiene)]
extern crate wasm_bindgen_test;
extern crate web_sys;
use wasm_bindgen_test::*;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use virtual_dom_rs::prelude::*;
wasm_bindgen_test_configure!(run_in_browser);
struct DiffPatchTest {
desc: &'static str,
old: VirtualNode,
new: VirtualNode,
override_expected: Option<String>,
}
#[wasm_bindgen_test]
fn replace_child() {
DiffPatchTest {
desc: "Replace a root node attribute attribute and a child text node",
old: html! {
<div>
Original element
</div>
},
new: html! { <div> Patched element</div> },
override_expected: None,
}
.test();
}
#[wasm_bindgen_test]
fn truncate_children() {
DiffPatchTest {
desc: "Truncates extra children",
old: html! {
<div>
<div> <div> <b></b> <em></em> </div> </div>
</div>
},
new: html! {
<div>
<div> <div> <b></b> </div> </div>
</div>
},
override_expected: None,
}
.test();
DiffPatchTest {
desc: "https://github.com/chinedufn/percy/issues/48",
old: html! {
<div>
ab <p></p> c
</div>
},
new: html! {
<div>
ab <p></p>
</div>
},
override_expected: None,
}
.test();
}
#[wasm_bindgen_test]
fn remove_attributes() {
DiffPatchTest {
desc: "Removes attributes",
old: html! { <div style=""> </div>
},
new: html! { <div></div> },
override_expected: None,
}
.test();
}
#[wasm_bindgen_test]
fn append_children() {
DiffPatchTest {
desc: "Append a child node",
old: html! { <div> </div>
},
new: html! { <div> <span></span> </div> },
override_expected: None,
}
.test();
}
#[wasm_bindgen_test]
fn text_node_siblings() {
// NOTE: Since there are two text nodes next to eachother we expect a `<!--ptns-->` separator in
// between them.
// @see virtual_node/mod.rs -> create_element() for more information
let override_expected = Some(
r#"<div id="after"><span>The button has been clicked: <!--ptns-->world</span></div>"#
.to_string(),
);
let old1 = VirtualNode::text("The button has been clicked: ");
let old2 = VirtualNode::text("hello");
let new1 = VirtualNode::text("The button has been clicked: ");
let new2 = VirtualNode::text("world");
DiffPatchTest {
desc: "Diff patch on text node siblings",
old: html! {
<div id="before">
<span> { {old1} {old2} } </span>
</div>
},
new: html! {
<div id="after">
<span> { {new1} {new2} } </span>
</div>
},
override_expected,
}
.test();
}
#[wasm_bindgen_test]
fn append_text_node() {
DiffPatchTest {
desc: "Append text node",
old: html! { <div> </div> },
new: html! { <div> Hello </div> },
override_expected: None,
}
.test();
}
#[wasm_bindgen_test]
fn append_sibling_text_nodes() {
let text1 = VirtualNode::text("Hello");
let text2 = VirtualNode::text("World");
DiffPatchTest {
desc: "Append sibling text nodes",
old: html! { <div> </div> },
new: html! { <div> {text1} {text2} </div> },
override_expected: None,
}
.test();
}
#[wasm_bindgen_test]
fn replace_with_children() {
DiffPatchTest {
desc: "Replace node that has children",
old: html! { <table><tr><th>0</th></tr><tr><td>1</td></tr></table> },
new: html! { <table><tr><td>2</td></tr><tr><th>3</th></tr></table> },
override_expected: None,
}
.test();
}
impl DiffPatchTest {
fn test(&mut self) {
let document = web_sys::window().unwrap().document().unwrap();
// If we haven't set an id for our element we hash the description of the test and set
// that as the ID.
// We need an ID in order to find the element within the DOM, otherwise we couldn't run
// our assertions.
if self.old.props.get("id").is_none() {
let mut hashed_desc = DefaultHasher::new();
self.desc.hash(&mut hashed_desc);
self.old
.props
.insert("id".to_string(), hashed_desc.finish().to_string());
}
// Add our old node into the DOM
let root_node = self.old.create_element().element;
document.body().unwrap().append_child(&root_node).unwrap();
let elem_id = self.old.props.get("id").unwrap().clone();
// This is our root node that we're about to patch.
// It isn't actually patched yet.. but by the time we use this it will be.
let patched_element = document.get_element_by_id(&elem_id).unwrap();
let patches = virtual_dom_rs::diff(&self.old, &self.new);
virtual_dom_rs::patch(root_node, &patches);
let expected_new_root_node = match self.override_expected {
Some(ref expected) => expected.clone(),
None => self.new.to_string(),
};
assert_eq!(
patched_element.outer_html(),
expected_new_root_node,
"{}",
self.desc
);
}
}