|
7 | 7 | createWorld, |
8 | 8 | getStore, |
9 | 9 | Not, |
| 10 | + Or, |
10 | 11 | trait, |
11 | 12 | } from '../../dist'; |
12 | 13 |
|
@@ -575,6 +576,98 @@ describe('Query modifiers', () => { |
575 | 576 | expect(entity.get(Name)!.name).toBe('modified'); |
576 | 577 | }); |
577 | 578 |
|
| 579 | + it('should combine Or with Changed modifiers to match ANY changed trait', () => { |
| 580 | + const Changed = createChanged(); |
| 581 | + |
| 582 | + const entityA = world.spawn(Position, Foo); |
| 583 | + const entityB = world.spawn(Position, Foo); |
| 584 | + const entityC = world.spawn(Position, Foo); |
| 585 | + |
| 586 | + // No changes yet |
| 587 | + let entities = world.query(Or(Changed(Position), Changed(Foo))); |
| 588 | + expect(entities.length).toBe(0); |
| 589 | + |
| 590 | + // Change only Position on entityA |
| 591 | + entityA.changed(Position); |
| 592 | + entities = world.query(Or(Changed(Position), Changed(Foo))); |
| 593 | + expect(entities).toContain(entityA); |
| 594 | + expect(entities.length).toBe(1); |
| 595 | + |
| 596 | + // Change only Foo on entityB |
| 597 | + entityB.changed(Foo); |
| 598 | + entities = world.query(Or(Changed(Position), Changed(Foo))); |
| 599 | + expect(entities).toContain(entityB); |
| 600 | + expect(entities.length).toBe(1); |
| 601 | + |
| 602 | + // Change both on entityC - should still match |
| 603 | + entityC.changed(Position); |
| 604 | + entityC.changed(Foo); |
| 605 | + entities = world.query(Or(Changed(Position), Changed(Foo))); |
| 606 | + expect(entities).toContain(entityC); |
| 607 | + expect(entities.length).toBe(1); |
| 608 | + }); |
| 609 | + |
| 610 | + it('should combine Or with Added modifiers to match ANY added trait', () => { |
| 611 | + const Added = createAdded(); |
| 612 | + |
| 613 | + const entityA = world.spawn(); |
| 614 | + const entityB = world.spawn(); |
| 615 | + const entityC = world.spawn(); |
| 616 | + |
| 617 | + // No additions yet |
| 618 | + let entities = world.query(Or(Added(Position), Added(Foo))); |
| 619 | + expect(entities.length).toBe(0); |
| 620 | + |
| 621 | + // Add only Position to entityA |
| 622 | + entityA.add(Position); |
| 623 | + entities = world.query(Or(Added(Position), Added(Foo))); |
| 624 | + expect(entities).toContain(entityA); |
| 625 | + expect(entities.length).toBe(1); |
| 626 | + |
| 627 | + // Add only Foo to entityB |
| 628 | + entityB.add(Foo); |
| 629 | + entities = world.query(Or(Added(Position), Added(Foo))); |
| 630 | + expect(entities).toContain(entityB); |
| 631 | + expect(entities.length).toBe(1); |
| 632 | + |
| 633 | + // Add both to entityC - should still match |
| 634 | + entityC.add(Position, Foo); |
| 635 | + entities = world.query(Or(Added(Position), Added(Foo))); |
| 636 | + expect(entities).toContain(entityC); |
| 637 | + expect(entities.length).toBe(1); |
| 638 | + }); |
| 639 | + |
| 640 | + it('should combine Or with Removed modifiers to match ANY removed trait', () => { |
| 641 | + const Removed = createRemoved(); |
| 642 | + |
| 643 | + const entityA = world.spawn(Position, Foo); |
| 644 | + const entityB = world.spawn(Position, Foo); |
| 645 | + const entityC = world.spawn(Position, Foo); |
| 646 | + |
| 647 | + // No removals yet |
| 648 | + let entities = world.query(Or(Removed(Position), Removed(Foo))); |
| 649 | + expect(entities.length).toBe(0); |
| 650 | + |
| 651 | + // Remove only Position from entityA |
| 652 | + entityA.remove(Position); |
| 653 | + entities = world.query(Or(Removed(Position), Removed(Foo))); |
| 654 | + expect(entities).toContain(entityA); |
| 655 | + expect(entities.length).toBe(1); |
| 656 | + |
| 657 | + // Remove only Foo from entityB |
| 658 | + entityB.remove(Foo); |
| 659 | + entities = world.query(Or(Removed(Position), Removed(Foo))); |
| 660 | + expect(entities).toContain(entityB); |
| 661 | + expect(entities.length).toBe(1); |
| 662 | + |
| 663 | + // Remove both from entityC - should still match |
| 664 | + entityC.remove(Position); |
| 665 | + entityC.remove(Foo); |
| 666 | + entities = world.query(Or(Removed(Position), Removed(Foo))); |
| 667 | + expect(entities).toContain(entityC); |
| 668 | + expect(entities.length).toBe(1); |
| 669 | + }); |
| 670 | + |
578 | 671 | // @internal Tests internal implementation edge case with generation overflow |
579 | 672 | it('[internal] should handle Changed modifier when trait registration causes generation overflow', () => { |
580 | 673 | // Create a fresh world to control trait registration count |
|
0 commit comments