use pkecs_core::{component::Component, entity::Entity, world::World};

/// Asserts that event handlers are called when commands are flushed in [`World`].
#[test]
fn flushing_triggers_handlers() {
    // Arrange
    let mut world = World::default();
    world
        .entities_mut()
        .spawn(Called(false));
    world.register(|_: &TestEvent, w: &mut World| {
        let called = w
            .entities_mut()
            .query_mut(Called::query())
            .unwrap()
            .first_mut::<Called>()
            .unwrap();

        called.0 = true;
    });

    // Act
    world.trigger(&TestEvent);

    let called = world
        .entities_ref()
        .query(Called::query())
        .unwrap()
        .first::<Called>()
        .unwrap();

    // Assert
    assert!(called.0);
}

struct Called(bool);
impl Component for Called {}

struct TestEvent;
