Skip to content

Commit 687241e

Browse files
authored
Merge pull request #7 from Ivy-Interactive/tendril/00239-MigrateTestsFromDeprecatedBuildCtxToAssignIds
[00239] Migrate tests from deprecated .build(ctx) to assign_ids pattern
2 parents 909dfdf + c015a40 commit 687241e

3 files changed

Lines changed: 68 additions & 34 deletions

File tree

rusty/src/core/runtime.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,13 @@ mod tests {
311311
}
312312

313313
impl View for ClickView {
314-
fn build(&self, ctx: &mut BuildContext) -> Element {
314+
fn build(&self, _ctx: &mut BuildContext) -> Element {
315315
let clicked = self.clicked.clone();
316-
let btn = Button::new("Click me")
316+
Button::new("Click me")
317317
.on_click(move || {
318318
clicked.store(true, Ordering::SeqCst);
319319
})
320-
.build(ctx);
321-
Element::Widget(Box::new(btn))
320+
.into()
322321
}
323322
}
324323

@@ -355,15 +354,14 @@ mod tests {
355354
}
356355

357356
impl View for InputView {
358-
fn build(&self, ctx: &mut BuildContext) -> Element {
357+
fn build(&self, _ctx: &mut BuildContext) -> Element {
359358
let received = self.received.clone();
360-
let input = crate::widgets::input::TextInput::new()
359+
crate::widgets::input::TextInput::new()
361360
.on_change(move |val| {
362361
let mut r = received.lock().unwrap();
363362
*r = val;
364363
})
365-
.build(ctx);
366-
Element::Widget(Box::new(input))
364+
.into()
367365
}
368366
}
369367

rusty/src/widgets/button.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,30 @@ mod tests {
193193
fn test_button_json_includes_id() {
194194
let mut store = HookStore::default();
195195
let mut ctx = BuildContext::new(&mut store, None);
196-
let btn = Button::new("Test").build(&mut ctx);
197-
let json = btn.to_json();
198-
assert_eq!(json["id"], "w-0");
199-
assert_eq!(json["type"], "button");
196+
let mut element: Element = Button::new("Test").into();
197+
element.assign_ids(&mut ctx);
198+
if let Element::Widget(ref w) = element {
199+
let json = w.to_json();
200+
assert_eq!(json["id"], "w-0");
201+
assert_eq!(json["type"], "button");
202+
} else {
203+
panic!("Expected Element::Widget");
204+
}
200205
}
201206

202207
#[test]
203208
fn test_button_build_registers_handler() {
204209
let mut store = HookStore::default();
205210
let mut ctx = BuildContext::new(&mut store, None);
206-
let btn = Button::new("Click").on_click(|| {}).build(&mut ctx);
207-
assert_eq!(btn.id, Some("w-0".to_string()));
208-
assert!(json!({"hasOnClick": true})["hasOnClick"].as_bool().unwrap());
209-
let json = btn.to_json();
210-
assert_eq!(json["hasOnClick"], true);
211+
let mut element: Element = Button::new("Click").on_click(|| {}).into();
212+
element.assign_ids(&mut ctx);
213+
if let Element::Widget(ref w) = element {
214+
assert_eq!(w.get_id(), Some("w-0"));
215+
assert!(json!({"hasOnClick": true})["hasOnClick"].as_bool().unwrap());
216+
let json = w.to_json();
217+
assert_eq!(json["hasOnClick"], true);
218+
} else {
219+
panic!("Expected Element::Widget");
220+
}
211221
}
212222
}

rusty/src/widgets/input.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -621,46 +621,72 @@ mod tests {
621621
fn test_text_input_json_includes_id() {
622622
let mut store = HookStore::default();
623623
let mut ctx = BuildContext::new(&mut store, None);
624-
let input = TextInput::new().build(&mut ctx);
625-
let json = input.to_json();
626-
assert_eq!(json["id"], "w-0");
627-
assert_eq!(json["type"], "text_input");
624+
let mut element: Element = TextInput::new().into();
625+
element.assign_ids(&mut ctx);
626+
if let Element::Widget(ref w) = element {
627+
let json = w.to_json();
628+
assert_eq!(json["id"], "w-0");
629+
assert_eq!(json["type"], "text_input");
630+
} else {
631+
panic!("Expected Element::Widget");
632+
}
628633
}
629634

630635
#[test]
631636
fn test_number_input_json_includes_id() {
632637
let mut store = HookStore::default();
633638
let mut ctx = BuildContext::new(&mut store, None);
634-
let input = NumberInput::new().build(&mut ctx);
635-
let json = input.to_json();
636-
assert_eq!(json["id"], "w-0");
639+
let mut element: Element = NumberInput::new().into();
640+
element.assign_ids(&mut ctx);
641+
if let Element::Widget(ref w) = element {
642+
let json = w.to_json();
643+
assert_eq!(json["id"], "w-0");
644+
} else {
645+
panic!("Expected Element::Widget");
646+
}
637647
}
638648

639649
#[test]
640650
fn test_select_json_includes_id() {
641651
let mut store = HookStore::default();
642652
let mut ctx = BuildContext::new(&mut store, None);
643-
let select = Select::new(vec![]).build(&mut ctx);
644-
let json = select.to_json();
645-
assert_eq!(json["id"], "w-0");
653+
let mut element: Element = Select::new(vec![]).into();
654+
element.assign_ids(&mut ctx);
655+
if let Element::Widget(ref w) = element {
656+
let json = w.to_json();
657+
assert_eq!(json["id"], "w-0");
658+
} else {
659+
panic!("Expected Element::Widget");
660+
}
646661
}
647662

648663
#[test]
649664
fn test_checkbox_json_includes_id() {
650665
let mut store = HookStore::default();
651666
let mut ctx = BuildContext::new(&mut store, None);
652-
let cb = Checkbox::new(false).build(&mut ctx);
653-
let json = cb.to_json();
654-
assert_eq!(json["id"], "w-0");
667+
let mut element: Element = Checkbox::new(false).into();
668+
element.assign_ids(&mut ctx);
669+
if let Element::Widget(ref w) = element {
670+
let json = w.to_json();
671+
assert_eq!(json["id"], "w-0");
672+
} else {
673+
panic!("Expected Element::Widget");
674+
}
655675
}
656676

657677
#[test]
658678
fn test_widget_ids_are_sequential() {
659679
let mut store = HookStore::default();
660680
let mut ctx = BuildContext::new(&mut store, None);
661-
let input1 = TextInput::new().build(&mut ctx);
662-
let input2 = TextInput::new().build(&mut ctx);
663-
assert_eq!(input1.id, Some("w-0".to_string()));
664-
assert_eq!(input2.id, Some("w-1".to_string()));
681+
let mut el1: Element = TextInput::new().into();
682+
let mut el2: Element = TextInput::new().into();
683+
el1.assign_ids(&mut ctx);
684+
el2.assign_ids(&mut ctx);
685+
if let (Element::Widget(ref w1), Element::Widget(ref w2)) = (&el1, &el2) {
686+
assert_eq!(w1.get_id(), Some("w-0"));
687+
assert_eq!(w2.get_id(), Some("w-1"));
688+
} else {
689+
panic!("Expected Element::Widget");
690+
}
665691
}
666692
}

0 commit comments

Comments
 (0)