11import { expect } from 'chai' ;
22import sinon from 'sinon' ;
33
4+ import Api from '../../src/api.js' ;
45import {
56 ContextManager ,
67 createContextKey ,
@@ -28,7 +29,7 @@ const tracingOptions = function (options = {}) {
2829describe ( 'Tracing()' , function ( ) {
2930 it ( 'should initialize' , function ( done ) {
3031 const options = tracingOptions ( ) ;
31- const t = new Tracing ( window , options ) ;
32+ const t = new Tracing ( window , null , options ) ;
3233 t . initSession ( ) ;
3334
3435 expect ( t ) . to . have . property ( 'initSession' ) . and . to . be . a ( 'function' ) ;
@@ -53,7 +54,7 @@ describe('Tracing()', function () {
5354
5455 it ( 'should configure' , function ( ) {
5556 const options = tracingOptions ( ) ;
56- const t = new Tracing ( window , options ) ;
57+ const t = new Tracing ( window , null , options ) ;
5758 t . initSession ( ) ;
5859
5960 expect ( t . options . resource ) . to . deep . equal ( options . resource ) ;
@@ -74,7 +75,7 @@ describe('Tracing()', function () {
7475
7576 it ( 'should create and export spans' , function ( done ) {
7677 const options = tracingOptions ( ) ;
77- const t = new Tracing ( window , options ) ;
78+ const t = new Tracing ( window , null , options ) ;
7879 t . initSession ( ) ;
7980
8081 expect ( t . sessionId ) . to . match ( / ^ [ a - f 0 - 9 ] { 32 } $ / ) ;
@@ -120,7 +121,7 @@ describe('Tracing()', function () {
120121
121122 it ( 'should create spans with span option overrides' , function ( ) {
122123 const options = tracingOptions ( ) ;
123- const t = new Tracing ( window , options ) ;
124+ const t = new Tracing ( window , null , options ) ;
124125 t . initSession ( ) ;
125126
126127 expect ( t . sessionId ) . to . match ( / ^ [ a - f 0 - 9 ] { 32 } $ / ) ;
@@ -143,7 +144,7 @@ describe('Tracing()', function () {
143144
144145 it ( 'should get and set session attributes' , function ( ) {
145146 const options = tracingOptions ( ) ;
146- const t = new Tracing ( window , options ) ;
147+ const t = new Tracing ( window , null , options ) ;
147148 t . initSession ( ) ;
148149
149150 t . session . setAttributes ( { codeVersion : 'abc123' } ) ;
@@ -162,7 +163,7 @@ describe('Tracing()', function () {
162163 it ( 'should not init session when sessionStorage is not detected' , function ( ) {
163164 const stub = sinon . stub ( window , 'sessionStorage' ) . value ( undefined ) ;
164165 const options = tracingOptions ( ) ;
165- const t = new Tracing ( window , options ) ;
166+ const t = new Tracing ( window , null , options ) ;
166167
167168 // calling initSession should be a no-op
168169 t . initSession ( ) ;
@@ -173,12 +174,70 @@ describe('Tracing()', function () {
173174
174175 it ( 'should generate ids' , function ( done ) {
175176 const options = tracingOptions ( ) ;
176- const t = new Tracing ( window , options ) ;
177+ const t = new Tracing ( window , null , options ) ;
177178
178179 const replayId = t . idGen ( 8 ) ;
179180
180181 expect ( replayId ) . to . match ( / ^ [ a - f 0 - 9 ] { 16 } $ / ) ;
181182
182183 done ( ) ;
183184 } ) ;
185+
186+ describe ( 'post' , function ( ) {
187+ let api ;
188+ let transport ;
189+ let tracing ;
190+
191+ beforeEach ( function ( ) {
192+ transport = {
193+ post : sinon
194+ . stub ( )
195+ . callsFake ( ( { accessToken, options, payload, callback } ) => {
196+ setTimeout ( ( ) => {
197+ callback (
198+ null ,
199+ { err : 0 , result : { id : '12345' } } ,
200+ ) ;
201+ } , 1 ) ;
202+ } ) ,
203+ postJsonPayload : sinon . stub ( ) ,
204+ } ;
205+ const urlMock = { parse : sinon . stub ( ) . returns ( { } ) } ;
206+ const truncationMock = {
207+ truncate : sinon . stub ( ) . returns ( { error : null , value : '{}' } ) ,
208+ } ;
209+
210+ api = new Api (
211+ { accessToken : 'test-token-12345' } ,
212+ transport ,
213+ urlMock ,
214+ truncationMock ,
215+ ) ;
216+ tracing = new Tracing ( window , api , { } ) ;
217+ tracing . initSession ( ) ;
218+ } ) ;
219+
220+ afterEach ( function ( ) {
221+ sinon . restore ( ) ;
222+ } ) ;
223+
224+ it ( 'should post a trace payload' , function ( done ) {
225+ const span = tracing . startSpan ( 'test.span' ) ;
226+ span . end ( ) ;
227+ tracing . exporter . post (
228+ tracing . exporter . toPayload ( ) ,
229+ { 'X-Rollbar-Session-Id' : tracing . sessionId }
230+ ) ;
231+
232+ setTimeout ( ( ) => {
233+ expect ( transport . post . callCount ) . to . equal ( 1 ) ;
234+ const call = transport . post . getCall ( 0 ) ;
235+ const { payload, headers } = call . args [ 0 ] ;
236+ expect ( payload ) . to . have . property ( 'resourceSpans' ) ;
237+ expect ( headers ) . to . have . property ( 'X-Rollbar-Session-Id' , tracing . sessionId ) ;
238+
239+ done ( ) ;
240+ } , 10 ) ;
241+ } ) ;
242+ } ) ;
184243} ) ;
0 commit comments