Skip to content

Commit

Permalink
Merge pull request #90 from bugsnag/fix-renderer-user
Browse files Browse the repository at this point in the history
Fix 'user' not being set in renderer Events
  • Loading branch information
imjoehaines authored Apr 20, 2021
2 parents 4bd2518 + 7eb112d commit 9b2afaf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/plugin-electron-renderer-event-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
},
"devDependencies": {
"@bugsnag/electron-test-helpers": "^1.0.0",
"@bugsnag/core": "^7.9.2-alpha.0"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ module.exports = (BugsnagIpcRenderer = window.__bugsnag_ipc__) => ({
event.breadcrumbs = breadcrumbs
event.app = { ...event.app, ...app }
event.device = { ...event.device, ...device }
if (!event.user || Object.keys(event.user).length === 0) {
event.user = user

if (!event._user || Object.keys(event._user).length === 0) {
event._user = user
}

for (const section in metadata) {
if (event._metadata[section]) {
event._metadata[section] = { ...metadata[section], ...event._metadata[section] }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { makeClientForPlugin } from '@bugsnag/electron-test-helpers'
import Breadcrumb from '@bugsnag/core/breadcrumb'
import plugin from '../'

describe('plugin: electron renderer event data', () => {
it('cancels if main process callbacks return false', async () => {
const { client, sendEvent } = makeClient({ shouldSend: false })

const onError = jest.fn()
client.addOnError(onError)

await sendEvent()
expect(onError).not.toHaveBeenCalled()
})

it('sets context, breadcrumbs, app, device, user and metadata from the main process', async () => {
const context = 'abc context xyz'
const breadcrumbs = [new Breadcrumb('message', { metadata: true }, 'manual')]
const app = { abc: 123 }
const device = { xyz: 456 }
const user = { id: '10293847' }
const metadata = { meta: { data: true } }

const { sendEvent } = makeClient({ context, breadcrumbs, app, device, user, metadata })

const event = await sendEvent()

expect(event.context).toBe(context)
expect(event.breadcrumbs).toStrictEqual(breadcrumbs)
expect(event.app).toStrictEqual({ ...app, releaseStage: 'production', type: undefined, version: undefined })
expect(event.device).toStrictEqual(device)
expect(event.getUser()).toStrictEqual(user)
expect(event.getMetadata('meta')).toStrictEqual(metadata.meta)
})

it('prefers pre-existing context from the event', async () => {
const { client, sendEvent } = makeClient({ context: 'goodbye' })

client.addOnError(event => { event.context = 'hello' }, true)

const event = await sendEvent()

expect(event.context).toBe('hello')
})

it('prefers pre-existing user from the event', async () => {
const { client, sendEvent } = makeClient({ user: { id: 123 } })

client.addOnError(event => { event.setUser(456, '[email protected]', 'abc') }, true)

const event = await sendEvent()

expect(event.getUser()).toStrictEqual({ id: 456, email: '[email protected]', name: 'abc' })
})
})

const makeClient = payloadInfo => makeClientForPlugin({ plugin: plugin(makeIpcRenderer(payloadInfo)) })
const makeIpcRenderer = payloadInfo => ({ getPayloadInfo: async () => payloadInfo })

0 comments on commit 9b2afaf

Please sign in to comment.