Fix hot reloading components with keys (#2886)

* Fix hot reloading components with keys

* include component formatted segment keys, but not dynamic component value

* Fix component_literal_dyn_idx index

* add a new test for hot reloading components with keys

* Even more tests

* make clippy happy

* fix typo
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-08-28 18:53:11 -05:00
1 parent 0e937daaa1
commit 4676171861
7 files changed
+176 -56

No files matched your search

+103
View File
@@ -1085,6 +1085,109 @@ fn component_with_handlers() {
);
}
#[test]
fn component_remove_key() {
let a = quote! {
Component {
key: "{key}",
class: 123,
id: 456.789,
other: true,
dynamic1,
dynamic2,
blah: "hello {world}",
onclick: |e| { println!("clicked") },
}
};
// changing lit values
let b = quote! {
Component {
class: 456,
id: 789.456,
other: false,
dynamic1,
dynamic2,
blah: "goodbye {world}",
onclick: |e| { println!("clicked") },
}
};
let hot_reload = hot_reload_from_tokens(a, b).unwrap();
let template = hot_reload.get(&0).unwrap();
assert_eq!(
template.component_values,
&[
HotReloadLiteral::Int(456),
HotReloadLiteral::Float(789.456),
HotReloadLiteral::Bool(false),
HotReloadLiteral::Fmted(FmtedSegments::new(vec![
FmtSegment::Literal { value: "goodbye " },
FmtSegment::Dynamic { id: 1 }
]))
]
);
}
#[test]
fn component_modify_key() {
let a = quote! {
Component {
key: "{key}",
class: 123,
id: 456.789,
other: true,
dynamic1,
dynamic2,
blah1: "hello {world123}",
blah2: "hello {world}",
onclick: |e| { println!("clicked") },
}
};
// changing lit values
let b = quote! {
Component {
key: "{key}-{world}",
class: 456,
id: 789.456,
other: false,
dynamic1,
dynamic2,
blah1: "hello {world123}",
blah2: "hello {world}",
onclick: |e| { println!("clicked") },
}
};
let hot_reload = hot_reload_from_tokens(a, b).unwrap();
let template = hot_reload.get(&0).unwrap();
assert_eq!(
template.key,
Some(FmtedSegments::new(vec![
FmtSegment::Dynamic { id: 0 },
FmtSegment::Literal { value: "-" },
FmtSegment::Dynamic { id: 2 },
]))
);
assert_eq!(
template.component_values,
&[
HotReloadLiteral::Int(456),
HotReloadLiteral::Float(789.456),
HotReloadLiteral::Bool(false),
HotReloadLiteral::Fmted(FmtedSegments::new(vec![
FmtSegment::Literal { value: "hello " },
FmtSegment::Dynamic { id: 1 }
])),
HotReloadLiteral::Fmted(FmtedSegments::new(vec![
FmtSegment::Literal { value: "hello " },
FmtSegment::Dynamic { id: 2 }
]))
]
);
}
#[test]
fn duplicating_dynamic_nodes() {
let a = quote! {