Skip to content

Commit

Permalink
test: ✅ use SetAppState definition from core
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbenw committed Jan 20, 2025
1 parent ddbe636 commit 2cbfdd9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/core/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import type { AppState, SetAppState } from './core'
import type { SpanFactory } from './span-factory'

export interface Plugin<C extends Configuration> {
configure: (configuration: InternalConfiguration<C>, spanFactory: SpanFactory<C>, setAppState: SetAppState, appState: AppState) => void
configure: (configuration: InternalConfiguration<C>, spanFactory: SpanFactory<C>, setAppState: SetAppState, appState?: AppState) => void
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import type { AppState, Clock, SetAppState } from '@bugsnag/core-performance'
import { MockSpanFactory, createConfiguration } from '@bugsnag/js-performance-test-utilities'
import type { Clock, AppState } from '@bugsnag/core-performance'
import createClock from '../../lib/clock'
import type { AppRegistry } from 'react-native'
import { AppStartPlugin } from '../../lib/auto-instrumentation/app-start-plugin'
import createClock from '../../lib/clock'
import type { ReactNativeConfiguration } from '../../lib/config'
import type { AppRegistry } from 'react-native'

describe('app start plugin', () => {
let spanFactory: MockSpanFactory<ReactNativeConfiguration>
let clock: Clock
let appRegistry: typeof AppRegistry
let setAppState: (appState: AppState) => void
let appState: AppState
const setAppState: SetAppState = jest.fn()
const appState: AppState = 'starting'

beforeEach(() => {
spanFactory = new MockSpanFactory()
clock = createClock(performance)
appRegistry = {
setWrapperComponentProvider: jest.fn()
} as unknown as typeof AppRegistry
appState = 'starting'
})

it('starts an app start span when autoInstrumentAppStarts is true', () => {
Expand All @@ -33,8 +32,6 @@ describe('app start plugin', () => {
parentContext: null
}))

expect(appState).toBe('starting')

expect(appRegistry.setWrapperComponentProvider).toHaveBeenCalledWith(expect.any(Function))
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { VALID_API_KEY } from '@bugsnag/js-performance-test-utilities'
import BugsnagPerformance from '@bugsnag/react-native-performance'
import type { AppState } from '@bugsnag/core-performance'
import { fireEvent, render, screen } from '@testing-library/react-native'
import React, { useState } from 'react'
import { useState } from 'react'
import { Button, View } from 'react-native'
import { Navigation } from 'react-native-navigation'
import { CompleteNavigation } from '../lib/CompleteNavigation'
Expand All @@ -28,19 +27,19 @@ function createMockClient (plugin: ReactNativeNavigationPlugin) {
apiKey: VALID_API_KEY,
plugins: [plugin]
})
}

const setAppState: (appState: AppState) => void = jest.fn()
return BugsnagPerformance
}

describe('CompleteNavigation', () => {
it('calls the appropriate methods on mount', () => {
const plugin = new ReactNativeNavigationPlugin(Navigation, setAppState)
const plugin = new ReactNativeNavigationPlugin(Navigation)
createMockClient(plugin)

render(
<View>
<CompleteNavigation on='mount' />
</View>
<View>
<CompleteNavigation on='mount' />
</View>
)

// Wait for component to mount
Expand All @@ -51,17 +50,17 @@ describe('CompleteNavigation', () => {
})

it('calls the appropriate methods on unmount', () => {
const plugin = new ReactNativeNavigationPlugin(Navigation, setAppState)
const plugin = new ReactNativeNavigationPlugin(Navigation)
createMockClient(plugin)

function TestApp () {
const [showComponent, setShowComponent] = useState(true)

return (
<View>
{showComponent && <CompleteNavigation on='unmount' />}
<Button title='Unmount component' onPress={() => { setShowComponent(false) }} />
</View>
<View>
{showComponent && <CompleteNavigation on='unmount' />}
<Button title='Unmount component' onPress={() => { setShowComponent(false) }} />
</View>
)
}

Expand All @@ -78,17 +77,17 @@ describe('CompleteNavigation', () => {
})

it('calls the appropriate method when the "on" condition changes to true', () => {
const plugin = new ReactNativeNavigationPlugin(Navigation, setAppState)
const plugin = new ReactNativeNavigationPlugin(Navigation)
createMockClient(plugin)

function TestApp () {
const [loaded, setLoaded] = useState(false)

return (
<View>
<CompleteNavigation on={loaded} />
<Button title='Finish loading' onPress={() => { setLoaded(true) }} />
</View>
<View>
<CompleteNavigation on={loaded} />
<Button title='Finish loading' onPress={() => { setLoaded(true) }} />
</View>
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AppState, SpanFactory } from '@bugsnag/core-performance'
import type { AppState, SetAppState, SpanFactory } from '@bugsnag/core-performance'
import { MockSpanFactory } from '@bugsnag/js-performance-test-utilities'
import type { ReactNativeConfiguration } from '@bugsnag/react-native-performance'
import { fireEvent, render, screen } from '@testing-library/react-native'
Expand Down Expand Up @@ -223,7 +223,7 @@ const Route = () => {

interface AppProps {
spanFactory: SpanFactory<ReactNativeConfiguration>
setAppState: (appState: AppState) => void
setAppState: SetAppState
}

const App = ({ spanFactory, setAppState }: AppProps) => {
Expand Down

0 comments on commit 2cbfdd9

Please sign in to comment.