@@ -41,6 +41,89 @@ describe("World - Component Management", () => {
4141 expect ( world . get ( entity , positionComponent ) ) . toEqual ( position2 ) ;
4242 } ) ;
4343
44+ it ( "should keep last value for repeated set in one sync when merge is not configured" , ( ) => {
45+ const world = new World ( ) ;
46+ const entity = world . new ( ) ;
47+ const position1 : Position = { x : 10 , y : 20 } ;
48+ const position2 : Position = { x : 30 , y : 40 } ;
49+
50+ world . set ( entity , positionComponent , position1 ) ;
51+ world . set ( entity , positionComponent , position2 ) ;
52+ world . sync ( ) ;
53+
54+ expect ( world . get ( entity , positionComponent ) ) . toEqual ( position2 ) ;
55+ } ) ;
56+
57+ it ( "should merge repeated sets in one sync for merge-enabled components" , ( ) => {
58+ const world = new World ( ) ;
59+ const entity = world . new ( ) ;
60+ const Mailbox = component < string [ ] > ( {
61+ merge : ( prev , next ) => [ ...prev , ...next ] ,
62+ } ) ;
63+
64+ world . set ( entity , Mailbox , [ "A" ] ) ;
65+ world . set ( entity , Mailbox , [ "B" , "C" ] ) ;
66+ world . sync ( ) ;
67+
68+ expect ( world . get ( entity , Mailbox ) ) . toEqual ( [ "A" , "B" , "C" ] ) ;
69+ } ) ;
70+
71+ it ( "should reset merge accumulation after remove in one sync" , ( ) => {
72+ const world = new World ( ) ;
73+ const entity = world . new ( ) ;
74+ const Mailbox = component < string [ ] > ( {
75+ merge : ( prev , next ) => [ ...prev , ...next ] ,
76+ } ) ;
77+
78+ world . set ( entity , Mailbox , [ "A1" ] ) ;
79+ world . set ( entity , Mailbox , [ "A2" ] ) ;
80+ world . remove ( entity , Mailbox ) ;
81+ world . set ( entity , Mailbox , [ "B1" ] ) ;
82+ world . set ( entity , Mailbox , [ "B2" ] ) ;
83+ world . sync ( ) ;
84+
85+ expect ( world . get ( entity , Mailbox ) ) . toEqual ( [ "B1" , "B2" ] ) ;
86+ } ) ;
87+
88+ it ( "should merge relation sets by exact component type only" , ( ) => {
89+ const world = new World ( ) ;
90+ const entity = world . new ( ) ;
91+ const target1 = world . new ( ) ;
92+ const target2 = world . new ( ) ;
93+ const MailRel = component < string [ ] > ( {
94+ merge : ( prev , next ) => [ ...prev , ...next ] ,
95+ } ) ;
96+ const rel1 = relation ( MailRel , target1 ) ;
97+ const rel2 = relation ( MailRel , target2 ) ;
98+
99+ world . set ( entity , rel1 , [ "T1-A" ] ) ;
100+ world . set ( entity , rel2 , [ "T2-A" ] ) ;
101+ world . set ( entity , rel1 , [ "T1-B" ] ) ;
102+ world . set ( entity , rel2 , [ "T2-B" ] ) ;
103+ world . sync ( ) ;
104+
105+ expect ( world . get ( entity , rel1 ) ) . toEqual ( [ "T1-A" , "T1-B" ] ) ;
106+ expect ( world . get ( entity , rel2 ) ) . toEqual ( [ "T2-A" , "T2-B" ] ) ;
107+ } ) ;
108+
109+ it ( "should apply merge for singleton(component entity) sets" , ( ) => {
110+ const world = new World ( ) ;
111+ const Inbox = component < string [ ] > ( {
112+ merge : ( prev , next ) => [ ...prev , ...next ] ,
113+ } ) ;
114+
115+ world . set ( Inbox , [ "A" ] ) ;
116+ world . set ( Inbox , [ "B" ] ) ;
117+ world . sync ( ) ;
118+ expect ( world . get ( Inbox ) ) . toEqual ( [ "A" , "B" ] ) ;
119+
120+ world . remove ( Inbox ) ;
121+ world . set ( Inbox , [ "C" ] ) ;
122+ world . set ( Inbox , [ "D" ] ) ;
123+ world . sync ( ) ;
124+ expect ( world . get ( Inbox ) ) . toEqual ( [ "C" , "D" ] ) ;
125+ } ) ;
126+
44127 it ( "should remove components from entities" , ( ) => {
45128 const world = new World ( ) ;
46129 const entity = world . new ( ) ;
0 commit comments