-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add migration to fix NotificationServicesController bug (#12219)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> The purpose of this PR is to fix an error apparently caused by a missing `NotificationServicesController` property on the `state.engine.backgroundState` Fixes: #12207 Although we didn't pinpoint what was causing the `NotificationServicesController` to be missing, this code fills in the data if it is missing. n/a n/a n/a - [ ] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --------- Co-authored-by: sethkfman <[email protected]>
- Loading branch information
Showing
6 changed files
with
159 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
import { captureException } from '@sentry/react-native'; | ||
import { isObject } from '@metamask/utils'; | ||
import { ensureValidState } from './util'; | ||
import { hasProperty, isObject } from '@metamask/utils'; | ||
|
||
export const DEFAULT_NOTIFICATION_SERVICES_CONTROLLER = { | ||
isCheckingAccountsPresence: false, | ||
isFeatureAnnouncementsEnabled: false, | ||
isFetchingMetamaskNotifications: false, | ||
isMetamaskNotificationsFeatureSeen: false, | ||
isNotificationServicesEnabled: false, | ||
isUpdatingMetamaskNotifications: false, | ||
isUpdatingMetamaskNotificationsAccount: [], | ||
metamaskNotificationsList: [], | ||
metamaskNotificationsReadList: [], | ||
subscriptionAccountsSeen: [], | ||
}; | ||
|
||
export default function migrate(state: unknown) { | ||
if (!ensureValidState(state, 58)) { | ||
// Increment the migration number as appropriate | ||
return state; | ||
} | ||
if (!ensureValidState(state, 58)) { | ||
return state; | ||
} | ||
|
||
if (!isObject(state.settings)) { | ||
captureException( | ||
new Error( | ||
`FATAL ERROR: Migration 58: Invalid Settings state error: '${typeof state.settings}'`, | ||
), | ||
); | ||
if ( | ||
!hasProperty(state.engine.backgroundState, 'NotificationServicesController') || | ||
!isObject(state.engine.backgroundState.NotificationServicesController) | ||
) { | ||
state.engine.backgroundState.NotificationServicesController = DEFAULT_NOTIFICATION_SERVICES_CONTROLLER; | ||
} | ||
return state; | ||
} | ||
|
||
state.settings.searchEngine = 'Google'; | ||
|
||
// Return the modified state | ||
return state; | ||
} |
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,93 @@ | ||
import migrate from './059'; | ||
import { merge } from 'lodash'; | ||
import { captureException } from '@sentry/react-native'; | ||
import initialRootState from '../../util/test/initial-root-state'; | ||
import mockedEngine from '../../core/__mocks__/MockedEngine'; | ||
|
||
jest.mock('@sentry/react-native', () => ({ | ||
captureException: jest.fn(), | ||
})); | ||
const mockedCaptureException = jest.mocked(captureException); | ||
|
||
jest.mock('../../core/Engine', () => ({ | ||
init: () => mockedEngine.init(), | ||
})); | ||
|
||
describe('Migration #58 - Update default search engine from DDG to Google', () => { | ||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const invalidStates = [ | ||
{ | ||
state: null, | ||
errorMessage: "FATAL ERROR: Migration 58: Invalid state error: 'object'", | ||
scenario: 'state is invalid', | ||
}, | ||
{ | ||
state: merge({}, initialRootState, { | ||
engine: null, | ||
}), | ||
errorMessage: | ||
"FATAL ERROR: Migration 58: Invalid engine state error: 'object'", | ||
scenario: 'engine state is invalid', | ||
}, | ||
{ | ||
state: merge({}, initialRootState, { | ||
engine: { | ||
backgroundState: null, | ||
}, | ||
}), | ||
errorMessage: | ||
"FATAL ERROR: Migration 58: Invalid engine backgroundState error: 'object'", | ||
scenario: 'backgroundState is invalid', | ||
}, | ||
{ | ||
state: merge({}, initialRootState, { | ||
engine: { | ||
backgroundState: {}, | ||
}, | ||
settings: null, | ||
}), | ||
errorMessage: | ||
"FATAL ERROR: Migration 58: Invalid Settings state error: 'object'", | ||
scenario: 'Settings object is invalid', | ||
}, | ||
]; | ||
|
||
for (const { errorMessage, scenario, state } of invalidStates) { | ||
it(`should capture exception if ${scenario}`, async () => { | ||
const newState = await migrate(state); | ||
|
||
expect(newState).toStrictEqual(state); | ||
expect(mockedCaptureException).toHaveBeenCalledWith(expect.any(Error)); | ||
expect(mockedCaptureException.mock.calls[0][0].message).toBe( | ||
errorMessage, | ||
); | ||
}); | ||
} | ||
|
||
it('should update the search engine to Google', async () => { | ||
const oldState = { | ||
engine: { | ||
backgroundState: {}, | ||
}, | ||
settings: { | ||
searchEngine: 'DuckDuckGo', | ||
} | ||
}; | ||
|
||
const expectedState = { | ||
engine: { | ||
backgroundState: {}, | ||
}, | ||
settings: { | ||
searchEngine: 'Google', | ||
} | ||
}; | ||
|
||
const migratedState = await migrate(oldState); | ||
expect(migratedState).toStrictEqual(expectedState); | ||
}); | ||
}); |
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,24 @@ | ||
import { captureException } from '@sentry/react-native'; | ||
import { isObject } from '@metamask/utils'; | ||
import { ensureValidState } from './util'; | ||
|
||
export default function migrate(state: unknown) { | ||
if (!ensureValidState(state, 59)) { | ||
// Increment the migration number as appropriate | ||
return state; | ||
} | ||
|
||
if (!isObject(state.settings)) { | ||
captureException( | ||
new Error( | ||
`FATAL ERROR: Migration 59: Invalid Settings state error: '${typeof state.settings}'`, | ||
), | ||
); | ||
return state; | ||
} | ||
|
||
state.settings.searchEngine = 'Google'; | ||
|
||
// Return the modified state | ||
return state; | ||
} |
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