-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from bugsnag/bengourley/plugin-refactor
refactor: Rename electron plugins and add unit tests
- Loading branch information
Showing
48 changed files
with
770 additions
and
459 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# @bugsnag/plugin-client-state-manager | ||
|
||
This plugin provides a wrapper around the parts of state that need to be synchronised, providing a way for listeners to be notified of changes. | ||
|
||
The plugin runs in the main Electron process, and patches each of the client mutators whose state we need to synchronise: | ||
|
||
- `setUser()` | ||
- `setContext()` | ||
- `addMetadata()` | ||
- `clearMetadata()` | ||
|
||
Any call to these methods (which will be from a developer or a plugin calling `Bugsnag.<method>()` in the main process) will emit an event signifying the change and updated value. | ||
|
||
Separately, we expose a `bulkUpdate` method for a new renderer to deliver a full state update in one pass. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/plugin-electron-client-state-manager/test/client-state-manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import stateManager from '../client-state-manager' | ||
import Client from '@bugsnag/core/client' | ||
|
||
describe('@bugsnag/plugin-electron-client-state-manager', () => { | ||
it('should emit events when user changes', done => { | ||
const client = new Client({}, {}, [stateManager], {}) | ||
const { emitter } = client.getPlugin('clientStateManager') | ||
emitter.on('UserUpdate', user => { | ||
expect(user).toEqual({ id: '123', email: '[email protected]', name: 'Jim' }) | ||
done() | ||
}) | ||
client.setUser('123', '[email protected]', 'Jim') | ||
}) | ||
|
||
it('should emit events when context changes', done => { | ||
const client = new Client({}, {}, [stateManager], {}) | ||
const { emitter } = client.getPlugin('clientStateManager') | ||
emitter.on('ContextUpdate', (context) => { | ||
expect(context).toBe('ctx') | ||
done() | ||
}) | ||
client.setContext('ctx') | ||
}) | ||
|
||
it('should emit events when metadata is added', done => { | ||
const client = new Client({}, {}, [stateManager], {}) | ||
const { emitter } = client.getPlugin('clientStateManager') | ||
emitter.on('MetadataUpdate', (payload) => { | ||
expect(payload.section).toBe('section') | ||
expect(payload.values).toEqual({ key: 'value' }) | ||
done() | ||
}) | ||
client.addMetadata('section', 'key', 'value') | ||
}) | ||
|
||
it('should emit events when metadata is cleared', done => { | ||
const client = new Client({}, {}, [stateManager], {}) | ||
const { emitter } = client.getPlugin('clientStateManager') | ||
emitter.on('MetadataUpdate', (payload) => { | ||
expect(payload.section).toBe('section') | ||
expect(payload.values).toBe(undefined) | ||
done() | ||
}) | ||
client.clearMetadata('section', 'key') | ||
}) | ||
|
||
it('should support bulk updates', () => { | ||
const client = new Client({}, {}, [stateManager], {}) | ||
const { emitter, bulkUpdate } = client.getPlugin('clientStateManager') | ||
|
||
const metadataCb = jest.fn() | ||
const contextCb = jest.fn() | ||
const userCb = jest.fn() | ||
|
||
emitter.on('MetadataReplace', metadataCb) | ||
emitter.on('ContextUpdate', contextCb) | ||
emitter.on('UserUpdate', userCb) | ||
|
||
// update everything | ||
bulkUpdate({ | ||
user: { | ||
id: '123', name: 'Jim', email: '[email protected]' | ||
}, | ||
context: 'ctx', | ||
metadata: { | ||
section: { key: 'value' } | ||
} | ||
}) | ||
|
||
expect(metadataCb).toHaveBeenCalledWith({ section: { key: 'value' } }) | ||
expect(contextCb).toHaveBeenCalledWith('ctx') | ||
expect(userCb).toHaveBeenCalledWith({ id: '123', name: 'Jim', email: '[email protected]' }) | ||
|
||
jest.resetAllMocks() | ||
|
||
// update just context | ||
bulkUpdate({ | ||
context: 'ctx' | ||
}) | ||
|
||
expect(metadataCb).not.toHaveBeenCalled() | ||
expect(contextCb).toHaveBeenCalledWith('ctx') | ||
expect(userCb).not.toHaveBeenCalled() | ||
|
||
jest.resetAllMocks() | ||
|
||
// update just user | ||
bulkUpdate({ | ||
user: { id: '123', name: 'Jim', email: '[email protected]' } | ||
}) | ||
|
||
expect(metadataCb).not.toHaveBeenCalled() | ||
expect(contextCb).not.toHaveBeenCalledWith() | ||
expect(userCb).toHaveBeenCalledWith({ id: '123', name: 'Jim', email: '[email protected]' }) | ||
|
||
jest.resetAllMocks() | ||
|
||
// update just metadata | ||
bulkUpdate({ | ||
metadata: { | ||
section: { key: 'value' } | ||
} | ||
}) | ||
|
||
expect(metadataCb).toHaveBeenCalledWith({ section: { key: 'value' } }) | ||
expect(contextCb).not.toHaveBeenCalled() | ||
expect(userCb).not.toHaveBeenCalled() | ||
}) | ||
}) |
4 changes: 2 additions & 2 deletions
4
...n-electron-native-client-sync/binding.gyp → ...tron-client-state-persistence/binding.gyp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.