@@ -287,4 +287,86 @@ describe('Span', () => {
287287 } ) ;
288288 } ) ;
289289 } ) ;
290+
291+ describe ( 'toContext and updateWithContext' , ( ) => {
292+ test ( 'toContext should return correct context' , ( ) => {
293+ const originalContext = { traceId : 'a' , spanId : 'b' , sampled : false , description : 'test' , op : 'op' } ;
294+ const span = new Span ( originalContext ) ;
295+
296+ const newContext = span . toContext ( ) ;
297+
298+ expect ( newContext ) . toStrictEqual ( {
299+ ...originalContext ,
300+ data : { } ,
301+ spanId : expect . any ( String ) ,
302+ startTimestamp : expect . any ( Number ) ,
303+ tags : { } ,
304+ traceId : expect . any ( String ) ,
305+ } ) ;
306+ } ) ;
307+
308+ test ( 'updateWithContext should completely change span properties' , ( ) => {
309+ const originalContext = {
310+ traceId : 'a' ,
311+ spanId : 'b' ,
312+ sampled : false ,
313+ description : 'test' ,
314+ op : 'op' ,
315+ tags : {
316+ tag0 : 'hello' ,
317+ } ,
318+ } ;
319+ const span = new Span ( originalContext ) ;
320+
321+ span . updateWithContext ( {
322+ traceId : 'c' ,
323+ spanId : 'd' ,
324+ sampled : true ,
325+ } ) ;
326+
327+ expect ( span . traceId ) . toBe ( 'c' ) ;
328+ expect ( span . spanId ) . toBe ( 'd' ) ;
329+ expect ( span . sampled ) . toBe ( true ) ;
330+ expect ( span . description ) . toBe ( undefined ) ;
331+ expect ( span . op ) . toBe ( undefined ) ;
332+ expect ( span . tags ) . toStrictEqual ( { } ) ;
333+ } ) ;
334+
335+ test ( 'using toContext and updateWithContext together should update only changed properties' , ( ) => {
336+ const originalContext = {
337+ traceId : 'a' ,
338+ spanId : 'b' ,
339+ sampled : false ,
340+ description : 'test' ,
341+ op : 'op' ,
342+ tags : { tag0 : 'hello' } ,
343+ data : { data0 : 'foo' } ,
344+ } ;
345+ const span = new Span ( originalContext ) ;
346+
347+ const newContext = {
348+ ...span . toContext ( ) ,
349+ description : 'new' ,
350+ endTimestamp : 1 ,
351+ op : 'new-op' ,
352+ sampled : true ,
353+ tags : {
354+ tag1 : 'bye' ,
355+ } ,
356+ } ;
357+
358+ if ( newContext . data ) newContext . data . data1 = 'bar' ;
359+
360+ span . updateWithContext ( newContext ) ;
361+
362+ expect ( span . traceId ) . toBe ( 'a' ) ;
363+ expect ( span . spanId ) . toBe ( 'b' ) ;
364+ expect ( span . description ) . toBe ( 'new' ) ;
365+ expect ( span . endTimestamp ) . toBe ( 1 ) ;
366+ expect ( span . op ) . toBe ( 'new-op' ) ;
367+ expect ( span . sampled ) . toBe ( true ) ;
368+ expect ( span . tags ) . toStrictEqual ( { tag1 : 'bye' } ) ;
369+ expect ( span . data ) . toStrictEqual ( { data0 : 'foo' , data1 : 'bar' } ) ;
370+ } ) ;
371+ } ) ;
290372} ) ;
0 commit comments