|
| 1 | +import { BrowserClient, Hub } from '@sentry/browser'; |
| 2 | + |
| 3 | +import { addExtensionMethods } from '../src'; |
1 | 4 | import { Transaction } from '../src/transaction'; |
| 5 | +import { getDefaultBrowserClientOptions } from './testutils'; |
2 | 6 |
|
3 | 7 | describe('`Transaction` class', () => { |
| 8 | + beforeAll(() => { |
| 9 | + addExtensionMethods(); |
| 10 | + }); |
| 11 | + |
4 | 12 | describe('transaction name source', () => { |
5 | 13 | it('sets source in constructor if provided', () => { |
6 | 14 | const transaction = new Transaction({ name: 'dogpark', metadata: { source: 'route' } }); |
@@ -192,4 +200,127 @@ describe('`Transaction` class', () => { |
192 | 200 | }); |
193 | 201 | }); |
194 | 202 | }); |
| 203 | + |
| 204 | + describe('setContext', () => { |
| 205 | + it('sets context', () => { |
| 206 | + const transaction = new Transaction({ name: 'dogpark' }); |
| 207 | + transaction.setContext('foo', { |
| 208 | + key: 'val', |
| 209 | + key2: 'val2', |
| 210 | + }); |
| 211 | + |
| 212 | + // @ts-ignore accessing private property |
| 213 | + expect(transaction._contexts).toEqual({ |
| 214 | + foo: { |
| 215 | + key: 'val', |
| 216 | + key2: 'val2', |
| 217 | + }, |
| 218 | + }); |
| 219 | + }); |
| 220 | + |
| 221 | + it('overwrites context', () => { |
| 222 | + const transaction = new Transaction({ name: 'dogpark' }); |
| 223 | + transaction.setContext('foo', { |
| 224 | + key: 'val', |
| 225 | + key2: 'val2', |
| 226 | + }); |
| 227 | + transaction.setContext('foo', { |
| 228 | + key3: 'val3', |
| 229 | + }); |
| 230 | + |
| 231 | + // @ts-ignore accessing private property |
| 232 | + expect(transaction._contexts).toEqual({ |
| 233 | + foo: { |
| 234 | + key3: 'val3', |
| 235 | + }, |
| 236 | + }); |
| 237 | + }); |
| 238 | + |
| 239 | + it('merges context', () => { |
| 240 | + const transaction = new Transaction({ name: 'dogpark' }); |
| 241 | + transaction.setContext('foo', { |
| 242 | + key: 'val', |
| 243 | + key2: 'val2', |
| 244 | + }); |
| 245 | + transaction.setContext('bar', { |
| 246 | + anotherKey: 'anotherVal', |
| 247 | + }); |
| 248 | + |
| 249 | + // @ts-ignore accessing private property |
| 250 | + expect(transaction._contexts).toEqual({ |
| 251 | + foo: { |
| 252 | + key: 'val', |
| 253 | + key2: 'val2', |
| 254 | + }, |
| 255 | + bar: { |
| 256 | + anotherKey: 'anotherVal', |
| 257 | + }, |
| 258 | + }); |
| 259 | + }); |
| 260 | + |
| 261 | + it('deletes context', () => { |
| 262 | + const transaction = new Transaction({ name: 'dogpark' }); |
| 263 | + transaction.setContext('foo', { |
| 264 | + key: 'val', |
| 265 | + key2: 'val2', |
| 266 | + }); |
| 267 | + transaction.setContext('foo', null); |
| 268 | + |
| 269 | + // @ts-ignore accessing private property |
| 270 | + expect(transaction._contexts).toEqual({}); |
| 271 | + }); |
| 272 | + |
| 273 | + it('sets contexts on the event', () => { |
| 274 | + const options = getDefaultBrowserClientOptions({ tracesSampleRate: 1 }); |
| 275 | + const client = new BrowserClient(options); |
| 276 | + const hub = new Hub(client); |
| 277 | + |
| 278 | + jest.spyOn(hub, 'captureEvent'); |
| 279 | + |
| 280 | + const transaction = hub.startTransaction({ name: 'dogpark' }); |
| 281 | + transaction.setContext('foo', { key: 'val' }); |
| 282 | + transaction.finish(); |
| 283 | + |
| 284 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 285 | + expect(hub.captureEvent).toHaveBeenCalledTimes(1); |
| 286 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 287 | + expect(hub.captureEvent).toHaveBeenLastCalledWith( |
| 288 | + expect.objectContaining({ |
| 289 | + contexts: { |
| 290 | + foo: { key: 'val' }, |
| 291 | + trace: { |
| 292 | + span_id: transaction.spanId, |
| 293 | + trace_id: transaction.traceId, |
| 294 | + }, |
| 295 | + }, |
| 296 | + }), |
| 297 | + ); |
| 298 | + }); |
| 299 | + |
| 300 | + it('does not override trace context', () => { |
| 301 | + const options = getDefaultBrowserClientOptions({ tracesSampleRate: 1 }); |
| 302 | + const client = new BrowserClient(options); |
| 303 | + const hub = new Hub(client); |
| 304 | + |
| 305 | + jest.spyOn(hub, 'captureEvent'); |
| 306 | + |
| 307 | + const transaction = hub.startTransaction({ name: 'dogpark' }); |
| 308 | + transaction.setContext('trace', { key: 'val' }); |
| 309 | + transaction.finish(); |
| 310 | + |
| 311 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 312 | + expect(hub.captureEvent).toHaveBeenCalledTimes(1); |
| 313 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 314 | + expect(hub.captureEvent).toHaveBeenLastCalledWith( |
| 315 | + expect.objectContaining({ |
| 316 | + contexts: { |
| 317 | + trace: { |
| 318 | + span_id: transaction.spanId, |
| 319 | + trace_id: transaction.traceId, |
| 320 | + }, |
| 321 | + }, |
| 322 | + }), |
| 323 | + ); |
| 324 | + }); |
| 325 | + }); |
195 | 326 | }); |
0 commit comments