From f17ecc8d89ff28e51cd24ef334ae99140c3b5ae4 Mon Sep 17 00:00:00 2001 From: Tim Blackbird Date: Thu, 17 Jul 2025 16:51:34 +0200 Subject: [PATCH] Add `observer_no_target` test back --- crates/bevy_ecs/src/observer/mod.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/bevy_ecs/src/observer/mod.rs b/crates/bevy_ecs/src/observer/mod.rs index 70e405882e..ae8ab9b09a 100644 --- a/crates/bevy_ecs/src/observer/mod.rs +++ b/crates/bevy_ecs/src/observer/mod.rs @@ -527,6 +527,11 @@ mod tests { #[component(storage = "SparseSet")] struct S; + #[derive(EntityEvent)] + struct EventA; + + impl BroadcastEvent for EventA {} + #[derive(EntityEvent)] struct EntityEventA; @@ -839,6 +844,28 @@ mod tests { assert_eq!(vec!["add_ab"], world.resource::().0); } + #[test] + fn observer_no_target() { + let mut world = World::new(); + world.init_resource::(); + + let system: fn(On) = |_| { + panic!("Trigger routed to non-targeted entity."); + }; + world.spawn_empty().observe(system); + world.add_observer(move |obs: On, mut res: ResMut| { + assert_eq!(obs.target(), Entity::PLACEHOLDER); + res.observed("event_a"); + }); + + // TODO: ideally this flush is not necessary, but right now observe() returns WorldEntityMut + // and therefore does not automatically flush. + world.flush(); + world.trigger(EventA); + world.flush(); + assert_eq!(vec!["event_a"], world.resource::().0); + } + #[test] fn observer_entity_routing() { let mut world = World::new();