Skip to content

Commit

Permalink
test: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yousif-bugsnag committed Dec 18, 2024
1 parent 7e17c9a commit 5d4888c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import createClock from '../../lib/clock'
import { AppStartPlugin } from '../../lib/auto-instrumentation/app-start-plugin'
import type { ReactNativeConfiguration } from '../../lib/config'
import type { AppRegistry } from 'react-native'
import type { ReactNativeSpanFactory } from '../../lib/span-factory'

describe('app start plugin', () => {
let spanFactory: MockSpanFactory<ReactNativeConfiguration>
Expand All @@ -20,7 +21,7 @@ describe('app start plugin', () => {

it('starts an app start span when autoInstrumentAppStarts is true', () => {
const appStartTime = 1234
const plugin = new AppStartPlugin(appStartTime, spanFactory, clock, appRegistry)
const plugin = new AppStartPlugin(appStartTime, spanFactory as unknown as ReactNativeSpanFactory, clock, appRegistry)

plugin.configure(createConfiguration<ReactNativeConfiguration>({ autoInstrumentAppStarts: true }))

Expand All @@ -34,7 +35,7 @@ describe('app start plugin', () => {
})

it('does not start an app start span when autoInstrumentAppStarts is false', () => {
const plugin = new AppStartPlugin(1234, spanFactory, clock, appRegistry)
const plugin = new AppStartPlugin(1234, spanFactory as unknown as ReactNativeSpanFactory, clock, appRegistry)

plugin.configure(createConfiguration<ReactNativeConfiguration>({ autoInstrumentAppStarts: false }))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import { MockSpanFactory } from '@bugsnag/js-performance-test-utilities'
import { createNavigationSpan } from '../lib/create-navigation-span'
import type { ReactNativeSpanFactory } from '../lib/span-factory'

describe('createNavigationSpan', () => {
it('sets the span name to the route prefixed with [Navigation]', () => {
const spanFactory = new MockSpanFactory()
const span = createNavigationSpan(spanFactory, 'testRoute', { isFirstClass: false })
const span = createNavigationSpan(spanFactory as unknown as ReactNativeSpanFactory, 'testRoute', { isFirstClass: false })
const endedSpan = span.end(12345, spanFactory.sampler.spanProbability)

expect(endedSpan.name).toBe('[Navigation]testRoute')
})

it('always sets the span as first class', () => {
const spanFactory = new MockSpanFactory()
const span = createNavigationSpan(spanFactory, 'testRoute', { isFirstClass: false })
const span = createNavigationSpan(spanFactory as unknown as ReactNativeSpanFactory, 'testRoute', { isFirstClass: false })
const endedSpan = span.end(12345, spanFactory.sampler.spanProbability)

expect(endedSpan.attributes.toJson()).toContainEqual({ key: 'bugsnag.span.first_class', value: { boolValue: true } })
})

it('includes navigation category attribute', () => {
const spanFactory = new MockSpanFactory()
const span = createNavigationSpan(spanFactory, 'testRoute', { isFirstClass: true })
const span = createNavigationSpan(spanFactory as unknown as ReactNativeSpanFactory, 'testRoute', { isFirstClass: true })
const endedSpan = span.end(12345, spanFactory.sampler.spanProbability)

expect(endedSpan.attributes.toJson()).toContainEqual({ key: 'bugsnag.span.category', value: { stringValue: 'navigation' } })
})

it('includes the route attribute', () => {
const spanFactory = new MockSpanFactory()
const span = createNavigationSpan(spanFactory, 'testRoute', { isFirstClass: true })
const span = createNavigationSpan(spanFactory as unknown as ReactNativeSpanFactory, 'testRoute', { isFirstClass: true })
const endedSpan = span.end(12345, spanFactory.sampler.spanProbability)

expect(endedSpan.attributes.toJson()).toContainEqual({ key: 'bugsnag.navigation.route', value: { stringValue: 'testRoute' } })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ReactNativeSpanFactory } from '../lib'
import { platformExtensions } from '../lib/platform-extensions'
import { createTestClient, IncrementingClock, InMemoryDelivery, VALID_API_KEY } from '@bugsnag/js-performance-test-utilities'

Expand All @@ -7,7 +8,7 @@ describe('startNavigationSpan', () => {
it('creates a navigation span', async () => {
const delivery = new InMemoryDelivery()
const clock = new IncrementingClock()
const testClient = createTestClient({ deliveryFactory: () => delivery, platformExtensions: (spanFactory, spanContextStorage) => platformExtensions(0, clock, spanFactory, spanContextStorage) })
const testClient = createTestClient({ deliveryFactory: () => delivery, platformExtensions: (spanFactory, spanContextStorage) => platformExtensions(0, clock, spanFactory as unknown as ReactNativeSpanFactory, spanContextStorage) })
testClient.start({ apiKey: VALID_API_KEY })
await jest.runOnlyPendingTimersAsync()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MockSpanFactory } from '@bugsnag/js-performance-test-utilities'
import type { ReactNativeConfiguration } from '@bugsnag/react-native-performance'
import type { ReactNativeConfiguration, ReactNativeSpanFactory } from '@bugsnag/react-native-performance'
import { NavigationContainer, createNavigationContainerRef } from '@react-navigation/native'
import type { ParamListBase } from '@react-navigation/native'
import { createNativeStackNavigator } from '@react-navigation/native-stack'
Expand All @@ -20,7 +20,7 @@ afterEach(() => {
describe('createNavigationContainer', () => {
it('creates a navigation span when the route changes', () => {
const spanFactory = new MockSpanFactory<ReactNativeConfiguration>()
const BugsnagNavigationContainer = createNavigationContainer(NavigationContainer, spanFactory)
const BugsnagNavigationContainer = createNavigationContainer(NavigationContainer, spanFactory as unknown as ReactNativeSpanFactory)

render(
<BugsnagNavigationContainer>
Expand All @@ -44,7 +44,7 @@ describe('createNavigationContainer', () => {
it('forwards the provided ref to the NavigationContainer', () => {
const navigationRef = createNavigationContainerRef()
const spanFactory = new MockSpanFactory<ReactNativeConfiguration>()
const BugsnagNavigationContainer = createNavigationContainer(NavigationContainer, spanFactory)
const BugsnagNavigationContainer = createNavigationContainer(NavigationContainer, spanFactory as unknown as ReactNativeSpanFactory)

render(
<BugsnagNavigationContainer ref={navigationRef}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SpanFactory } from '@bugsnag/core-performance'
import { MockSpanFactory } from '@bugsnag/js-performance-test-utilities'
import type { ReactNativeConfiguration } from '@bugsnag/react-native-performance'
import type { ReactNativeConfiguration, ReactNativeSpanFactory } from '@bugsnag/react-native-performance'
import { fireEvent, render, screen } from '@testing-library/react-native'
import React, { useContext } from 'react'
import { Button, View } from 'react-native'
Expand Down Expand Up @@ -189,7 +189,7 @@ const App = ({ spanFactory }: AppProps) => {
const [currentRoute, setCurrentRoute] = React.useState('initial-route')

return (
<NavigationContextProvider spanFactory={spanFactory} currentRoute={currentRoute} >
<NavigationContextProvider spanFactory={spanFactory as unknown as ReactNativeSpanFactory} currentRoute={currentRoute} >
<Route />
<Button title='Change to route 1' onPress={() => { setCurrentRoute('route-1') }} />
<Button title='Change to route 2' onPress={() => { setCurrentRoute('route-2') }} />
Expand Down
2 changes: 0 additions & 2 deletions packages/test-utilities/lib/mock-span-factory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {
DefaultSpanContextStorage,
Sampler,

SpanFactory

} from '@bugsnag/core-performance'
import type { SpanAttribute, Configuration, SpanEnded, SpanInternal, SpanOptions } from '@bugsnag/core-performance'
import ControllableBackgroundingListener from './controllable-backgrounding-listener'
Expand Down

0 comments on commit 5d4888c

Please sign in to comment.