This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
forked from segmentio/analytics-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: deepmerge device context on update (segmentio#543)
- Loading branch information
Showing
9 changed files
with
147 additions
and
43 deletions.
There are no files selected for viewing
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
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,23 @@ | ||
export const createCallbackManager = <V, R = void>() => { | ||
type Callback = (value: V) => R; | ||
const callbacks: Callback[] = []; | ||
|
||
const deregister = (callback: Callback) => { | ||
callbacks.splice(callbacks.indexOf(callback), 1); | ||
}; | ||
|
||
const register = (callback: Callback) => { | ||
callbacks.push(callback); | ||
return () => { | ||
deregister(callback); | ||
}; | ||
}; | ||
|
||
const run = (value: V) => { | ||
for (const callback of [...callbacks]) { | ||
callback(value); | ||
} | ||
}; | ||
|
||
return { register, deregister, run }; | ||
}; |
102 changes: 102 additions & 0 deletions
102
packages/core/src/storage/__tests__/sovranStorage.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,102 @@ | ||
import deepmerge from 'deepmerge'; | ||
import { createCallbackManager } from '../../__tests__/__helpers__/utils'; | ||
import { SovranStorage } from '../sovranStorage'; | ||
|
||
jest.mock('@segment/sovran-react-native', () => ({ | ||
registerBridgeStore: jest.fn(), | ||
createStore: <T extends {}>(initialState: T) => { | ||
const callbackManager = createCallbackManager<T>(); | ||
|
||
let store = { | ||
...initialState, | ||
}; | ||
|
||
return { | ||
subscribe: jest | ||
.fn() | ||
.mockImplementation((callback: (state: T) => void) => { | ||
callbackManager.register(callback); | ||
return () => callbackManager.deregister(callback); | ||
}), | ||
dispatch: jest | ||
.fn() | ||
.mockImplementation( | ||
async (action: (state: T) => T | Promise<T>): Promise<T> => { | ||
store = await action(store); | ||
callbackManager.run(store); | ||
return store; | ||
} | ||
), | ||
getState: jest.fn().mockImplementation(() => ({ ...store })), | ||
}; | ||
}, | ||
})); | ||
|
||
describe('sovranStorage', () => { | ||
it('works', async () => { | ||
// First test that the constructor works correctly | ||
const sovran = new SovranStorage('test'); | ||
expect(sovran.isReady.get()).toBe(false); | ||
|
||
// Setup a listener for context changes | ||
const contextListener = jest.fn(); | ||
sovran.context.onChange(contextListener); | ||
|
||
// A basic test that sets up the context data in the store and checks that the listener is called | ||
const appContext = { | ||
app: { | ||
name: 'test', | ||
namespace: 'com.segment', | ||
version: '1.0.0', | ||
}, | ||
device: { | ||
manufacturer: 'Apple', | ||
model: 'iPhone X', | ||
name: 'iPhone', | ||
type: 'mobile', | ||
}, | ||
}; | ||
|
||
const newContext = await sovran.context.set(appContext); | ||
expect(newContext).toEqual(appContext); | ||
expect(sovran.context.get()).toEqual(appContext); | ||
expect(contextListener).toHaveBeenCalledWith(appContext); | ||
|
||
// Context should be deeply merged to preserve values set by other plugins | ||
const deviceToken = { | ||
device: { | ||
token: '123', | ||
}, | ||
}; | ||
|
||
const expected = deepmerge(appContext, deviceToken); | ||
const updated = await sovran.context.set(deviceToken); | ||
expect(updated).toEqual(expected); | ||
expect(sovran.context.get()).toEqual(expected); | ||
expect(contextListener).toHaveBeenCalledWith(expected); | ||
|
||
// Now lets test the settings, settings are not deeply merged, only merged at the top level | ||
const settings = { | ||
segment: { | ||
apiKey: '123', | ||
}, | ||
}; | ||
|
||
const newSettings = await sovran.settings.set(settings); | ||
expect(newSettings).toEqual(settings); | ||
expect(sovran.settings.get()).toEqual(settings); | ||
|
||
const settingsUpdate = { | ||
segment: { | ||
key: '123', | ||
}, | ||
braze: { | ||
key: '123', | ||
}, | ||
}; | ||
|
||
const updatedSettings = await sovran.settings.set(settingsUpdate); | ||
expect(updatedSettings).toEqual(settingsUpdate); | ||
expect(sovran.settings.get()).toEqual(settingsUpdate); | ||
}); | ||
}); |
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