Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix internal type definition for session delegates #1480

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/core/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export default class ClientWithInternals<T extends Config = Config> extends Clie
_pausedSession: Session | null

_sessionDelegate: {
startSession: (client: ClientWithInternals, session: Session) => any
pauseSession: () => void
resumeSession: () => void
startSession: (client: ClientWithInternals, session: Session) => ClientWithInternals
pauseSession: (client: ClientWithInternals) => void
resumeSession: (client: ClientWithInternals) => ClientWithInternals
}

_addOnSessionPayload: (cb: (sessionPayload: Session) => void) => void
Expand Down
41 changes: 21 additions & 20 deletions packages/core/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import Session from '../session'
import breadcrumbTypes from '../lib/breadcrumb-types'
import { BreadcrumbType } from '../types/common'

const noop = () => {}
const id = <T>(a: T) => a

describe('@bugsnag/core/client', () => {
describe('constructor', () => {
it('can handle bad input', () => {
Expand Down Expand Up @@ -669,17 +672,15 @@ describe('@bugsnag/core/client', () => {
describe('startSession()', () => {
it('calls the provided session delegate and return delegate’s return value', () => {
const client = new Client({ apiKey: 'API_KEY' })
let ret
client._sessionDelegate = {
startSession: c => {
expect(c).toBe(client)
ret = {}
return ret
return c
},
pauseSession: () => {},
resumeSession: () => {}
pauseSession: noop,
resumeSession: id
}
expect(client.startSession()).toBe(ret)
expect(client.startSession()).toBe(client)
})

it('tracks error counts using the session delegate and sends them in error payloads', (done) => {
Expand All @@ -690,8 +691,8 @@ describe('@bugsnag/core/client', () => {
client._session = new Session()
return client
},
pauseSession: () => {},
resumeSession: () => {}
pauseSession: noop,
resumeSession: id
}
client._setDelivery(client => ({
sendSession: () => {},
Expand Down Expand Up @@ -720,9 +721,9 @@ describe('@bugsnag/core/client', () => {
it('does not start the session if onSession returns false', () => {
const client = new Client({ apiKey: 'API_KEY', onSession: () => false })
const sessionDelegate = {
startSession: () => {},
pauseSession: () => {},
resumeSession: () => {}
startSession: id,
pauseSession: noop,
resumeSession: id
}
client._sessionDelegate = sessionDelegate

Expand All @@ -740,9 +741,9 @@ describe('@bugsnag/core/client', () => {
}
})
const sessionDelegate = {
startSession: () => {},
pauseSession: () => {},
resumeSession: () => {}
startSession: id,
pauseSession: noop,
resumeSession: id
}
client._sessionDelegate = sessionDelegate

Expand All @@ -759,9 +760,9 @@ describe('@bugsnag/core/client', () => {
c._setDelivery(client => ({ sendEvent: (p, cb) => cb(null), sendSession: (s: any, cb: any) => cb(null) }))
c._logger = console
const sessionDelegate = {
startSession: () => {},
pauseSession: () => {},
resumeSession: () => {}
startSession: id,
pauseSession: noop,
resumeSession: id
}
c._sessionDelegate = sessionDelegate
const eSpy = jest.fn()
Expand Down Expand Up @@ -839,9 +840,9 @@ describe('@bugsnag/core/client', () => {
it('forwards on calls to the session delegate', () => {
const client = new Client({ apiKey: 'API_KEY' })
const sessionDelegate = {
startSession: () => {},
pauseSession: () => {},
resumeSession: () => {}
startSession: id,
pauseSession: noop,
resumeSession: id
}
client._sessionDelegate = sessionDelegate

Expand Down
24 changes: 18 additions & 6 deletions packages/in-flight/test/in-flight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import BugsnagInFlightJustForTypescript from '../types/bugsnag-in-flight'

let bugsnagInFlight: BugsnagInFlightJustForTypescript
jest.isolateModules(() => { bugsnagInFlight = require('../src/in-flight') })
const noop = () => {}
const id = <T>(a: T) => a

describe('@bugsnag/in-flight', () => {
it('tracks in-flight events', () => {
Expand Down Expand Up @@ -50,6 +52,8 @@ describe('@bugsnag/in-flight', () => {
client._sessionDelegate = {
startSession: jest.fn(function (client, session) {
client._delivery.sendSession(session, callback)

return client
}),
pauseSession: jest.fn(),
resumeSession: jest.fn()
Expand Down Expand Up @@ -89,6 +93,8 @@ describe('@bugsnag/in-flight', () => {
client._sessionDelegate = {
startSession: jest.fn(function (client, session) {
client._delivery.sendSession(session, sessionCallback)

return client
}),
pauseSession: jest.fn(),
resumeSession: jest.fn()
Expand Down Expand Up @@ -135,9 +141,11 @@ describe('@bugsnag/in-flight', () => {
client._sessionDelegate = {
startSession (client, session) {
client._delivery.sendSession(session, () => {})

return client
},
pauseSession: () => {},
resumeSession: () => {}
pauseSession: noop,
resumeSession: id
}

client._setDelivery(() => ({
Expand Down Expand Up @@ -177,9 +185,11 @@ describe('@bugsnag/in-flight', () => {
client._sessionDelegate = {
startSession: (client, session) => {
client._delivery.sendSession(session, () => {})

return client
},
pauseSession: () => {},
resumeSession: () => {}
pauseSession: noop,
resumeSession: id
}

client._setDelivery(() => ({
Expand Down Expand Up @@ -225,9 +235,11 @@ describe('@bugsnag/in-flight', () => {
client._sessionDelegate = {
startSession (client, session) {
client._delivery.sendSession(session, () => {})

return client
},
pauseSession: () => {},
resumeSession: () => {}
pauseSession: noop,
resumeSession: id
}

client._setDelivery(() => ({
Expand Down
20 changes: 14 additions & 6 deletions packages/plugin-browser-device/test/device.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ declare class SessionWithDevice extends Session { public device: Device }

const navigator = { language: 'en-GB', userAgent: 'testing browser 1.2.3' } as unknown as Navigator
const screen = { orientation: { type: 'landscape-primary' } } as unknown as Screen
const noop = () => {}
const id = <T>(a: T) => a

describe('plugin: device', () => {
it('should add an onError callback which captures device information', () => {
Expand Down Expand Up @@ -56,9 +58,11 @@ describe('plugin: device', () => {
client._sessionDelegate = {
startSession: (client, session) => {
client._delivery.sendSession(session, () => {})

return client
},
pauseSession: () => {},
resumeSession: () => {}
pauseSession: noop,
resumeSession: id
}

expect(client._cbs.s).toHaveLength(1)
Expand All @@ -80,9 +84,11 @@ describe('plugin: device', () => {
client._sessionDelegate = {
startSession: (client, session) => {
client._delivery.sendSession(session, () => {})

return client
},
pauseSession: () => {},
resumeSession: () => {}
pauseSession: noop,
resumeSession: id
}

expect(client._cbs.s).toHaveLength(1)
Expand Down Expand Up @@ -119,9 +125,11 @@ describe('plugin: device', () => {
{ sessions: [session] },
(err) => { if (err) throw err }
)

return client
},
pauseSession: () => {},
resumeSession: () => {}
pauseSession: noop,
resumeSession: id
}

client._setDelivery((client) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import plugin from '../navigation-breadcrumbs'

import Client from '@bugsnag/core/client'

const noop = () => {}
const id = <T>(a: T) => a

describe('plugin: navigation breadcrumbs', () => {
it('should drop breadcrumb for navigational activity', done => {
const { winHandlers, docHandlers, window } = getMockWindow()
const c = new Client({ apiKey: 'aaaa-aaaa-aaaa-aaaa', plugins: [plugin(window)] })
c._sessionDelegate = {
startSession: () => {},
pauseSession: () => {},
resumeSession: () => {}
startSession: id,
pauseSession: noop,
resumeSession: id
}

winHandlers.load.forEach((h) => h.call(window))
Expand Down Expand Up @@ -37,9 +40,9 @@ describe('plugin: navigation breadcrumbs', () => {
const { winHandlers, docHandlers, window } = getMockWindow()
const c = new Client({ apiKey: 'aaaa-aaaa-aaaa-aaaa', enabledBreadcrumbTypes: [], plugins: [plugin(window)] })
c._sessionDelegate = {
startSession: () => {},
pauseSession: () => {},
resumeSession: () => {}
startSession: id,
pauseSession: noop,
resumeSession: id
}
winHandlers.load.forEach((h) => h.call(window))
docHandlers.DOMContentLoaded.forEach((h) => h.call(window.document))
Expand All @@ -53,8 +56,8 @@ describe('plugin: navigation breadcrumbs', () => {
const c = new Client({ apiKey: 'aaaa-aaaa-aaaa-aaaa', plugins: [plugin(window)] })
c._sessionDelegate = {
startSession: jest.fn(),
pauseSession: () => {},
resumeSession: () => {}
pauseSession: noop,
resumeSession: id
}
winHandlers.load.forEach((h) => h.call(window))
docHandlers.DOMContentLoaded.forEach((h) => h.call(window.document))
Expand All @@ -70,8 +73,8 @@ describe('plugin: navigation breadcrumbs', () => {
const c = new Client({ apiKey: 'aaaa-aaaa-aaaa-aaaa', autoTrackSessions: false, plugins: [plugin(window)] })
c._sessionDelegate = {
startSession: jest.fn(),
pauseSession: () => {},
resumeSession: () => {}
pauseSession: noop,
resumeSession: id
}
winHandlers.load.forEach((h) => h.call(window))
docHandlers.DOMContentLoaded.forEach((h) => h.call(window.document))
Expand All @@ -83,9 +86,9 @@ describe('plugin: navigation breadcrumbs', () => {
const { winHandlers, docHandlers, window } = getMockWindow()
const c = new Client({ apiKey: 'aaaa-aaaa-aaaa-aaaa', enabledBreadcrumbTypes: ['navigation'], plugins: [plugin(window)] })
c._sessionDelegate = {
startSession: () => {},
pauseSession: () => {},
resumeSession: () => {}
startSession: id,
pauseSession: noop,
resumeSession: id
}
winHandlers.load.forEach((h) => h.call(window))
docHandlers.DOMContentLoaded.forEach((h) => h.call(window.document))
Expand All @@ -98,9 +101,9 @@ describe('plugin: navigation breadcrumbs', () => {
const { winHandlers, docHandlers, window } = getMockWindow()
const c = new Client({ apiKey: 'aaaa-aaaa-aaaa-aaaa', enabledBreadcrumbTypes: null, plugins: [plugin(window)] })
c._sessionDelegate = {
startSession: () => {},
pauseSession: () => {},
resumeSession: () => {}
startSession: id,
pauseSession: noop,
resumeSession: id
}
winHandlers.load.forEach((h) => h.call(window))
docHandlers.DOMContentLoaded.forEach((h) => h.call(window.document))
Expand Down