diff --git a/src/ui/effect.rs b/src/ui/effect.rs index 2e6213c..8b3d201 100644 --- a/src/ui/effect.rs +++ b/src/ui/effect.rs @@ -82,6 +82,11 @@ impl Effect for AddFormula { fn apply(&self, app: &mut App) { match crate::formula::parse_formula(&self.raw, &self.target_category) { Ok(formula) => { + // Ensure the formula target exists as an item in the target category + // so the grid layout includes cells for it. + if let Some(cat) = app.model.category_mut(&formula.target_category) { + cat.add_item(&formula.target); + } app.model.add_formula(formula); } Err(e) => { @@ -1021,6 +1026,38 @@ mod tests { assert!(!app.model.formulas().is_empty()); } + /// Regression: AddFormula must add the target item to the target category + /// so the grid layout includes cells for it. Without this, a formula like + /// `Margin = Profit / Revenue` would be registered but invisible in the grid. + #[test] + fn add_formula_adds_target_item_to_category() { + let mut app = test_app(); + // "Margin" does not exist as an item in "Type" before adding the formula + assert!(!app + .model + .category("Type") + .unwrap() + .ordered_item_names() + .contains(&"Margin")); + AddFormula { + raw: "Margin = Food * 2".to_string(), + target_category: "Type".to_string(), + } + .apply(&mut app); + let items: Vec<&str> = app + .model + .category("Type") + .unwrap() + .ordered_item_names() + .into_iter() + .collect(); + assert!( + items.contains(&"Margin"), + "formula target 'Margin' should be added as item to 'Type' category, got: {:?}", + items + ); + } + #[test] fn add_formula_invalid_sets_error_status() { let mut app = test_app();