use std::rc::Rc; use gpui::{AppContext as _, TestAppContext}; use gpui_native_core::alert::{Alert, AlertSeverity, Alerts}; use gpui_native_theme::Theme; fn alerts_stack() -> (TestAppContext, gpui::Entity) { let cx = TestAppContext::single(); cx.update(Theme::init); let alerts = cx.update(|cx| cx.new(|_| Alerts::default())); (cx, alerts) } #[test] fn pushed_alerts_stack_up_and_dismiss_by_id() { let (cx, alerts) = alerts_stack(); cx.update(|cx| { alerts.update(cx, |alerts, cx| { alerts.push( Alert::new("a").title("First").severity(AlertSeverity::Info), cx, ); alerts.push( Alert::new("b") .title("Second") .severity(AlertSeverity::Warning), cx, ); }); }); assert_eq!(cx.update(|cx| alerts.read(cx).count()), 2); cx.update(|cx| { alerts.update(cx, |alerts, cx| alerts.dismiss(0, cx)); }); assert_eq!(cx.update(|cx| alerts.read(cx).count()), 1); // Duplicate ids are fine: the stack re-keys each entry to its own id. cx.update(|cx| { alerts.update(cx, |alerts, cx| { alerts.push(Alert::new("b").title("Third"), cx); }); }); assert_eq!(cx.update(|cx| alerts.read(cx).count()), 2); } #[test] fn the_close_button_routes_through_the_store_and_runs_the_handler() { let (cx, alerts) = alerts_stack(); let dismissed = Rc::new(std::cell::RefCell::new(false)); cx.update(|cx| { let handler = dismissed.clone(); alerts.update(cx, |alerts, cx| { alerts.push( Alert::new("a").title("Sync failed").on_dismiss(move |_| { *handler.borrow_mut() = true; }), cx, ); }); }); // The pushed alert's close button calls the store's dismissal, which // then runs the handler. cx.update(|cx| { alerts.update(cx, |alerts, cx| alerts.dismiss(0, cx)); }); assert!(*dismissed.borrow()); assert_eq!(cx.update(|cx| alerts.read(cx).count()), 0); }