-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 #43056 from Expensify/arosiclair-push-notif-onyx-u…
…pdates Fix applying onyx updates from push notifications
- Loading branch information
Showing
4 changed files
with
101 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type {KeyValueMapping, OnyxEntry, OnyxKey} from 'react-native-onyx'; | ||
import Onyx from 'react-native-onyx'; | ||
import CONST from '@src/CONST'; | ||
import * as OnyxUpdates from '@src/libs/actions/OnyxUpdates'; | ||
import DateUtils from '@src/libs/DateUtils'; | ||
import * as NumberUtils from '@src/libs/NumberUtils'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type {OnyxUpdatesFromServer} from '@src/types/onyx'; | ||
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; | ||
|
||
describe('OnyxUpdatesTest', () => { | ||
beforeAll(() => { | ||
Onyx.init({ | ||
keys: ONYXKEYS, | ||
}); | ||
}); | ||
|
||
beforeEach(() => Onyx.clear().then(waitForBatchedUpdates)); | ||
|
||
it('applies Airship Onyx updates correctly', () => { | ||
const reportID = NumberUtils.rand64(); | ||
const reportActionID = NumberUtils.rand64(); | ||
const created = DateUtils.getDBTime(); | ||
|
||
const reportValue = {reportID}; | ||
const reportActionValue = { | ||
[reportActionID]: { | ||
reportActionID, | ||
created, | ||
}, | ||
}; | ||
|
||
// Given an onyx update from an Airship push notification | ||
const airshipUpdates: OnyxUpdatesFromServer = { | ||
type: CONST.ONYX_UPDATE_TYPES.AIRSHIP, | ||
previousUpdateID: 0, | ||
lastUpdateID: 1, | ||
updates: [ | ||
{ | ||
eventType: '', | ||
data: [ | ||
{ | ||
onyxMethod: 'merge', | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, | ||
value: reportValue, | ||
}, | ||
{ | ||
onyxMethod: 'merge', | ||
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, | ||
value: reportActionValue, | ||
shouldShowPushNotification: true, | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
// When we apply the updates, then their values are updated correctly | ||
return OnyxUpdates.apply(airshipUpdates) | ||
.then(() => getOnyxValues(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`)) | ||
.then(([report, reportAction]) => { | ||
expect(report).toStrictEqual(reportValue); | ||
expect(reportAction).toStrictEqual(reportActionValue); | ||
}); | ||
}); | ||
}); | ||
|
||
function getOnyxValues<TKey extends OnyxKey>(...keys: TKey[]) { | ||
return Promise.all(keys.map((key) => getOnyxValue(key))); | ||
} | ||
|
||
function getOnyxValue<TKey extends OnyxKey>(key: TKey): Promise<OnyxEntry<KeyValueMapping[TKey]>> { | ||
return new Promise((resolve) => { | ||
Onyx.connect({ | ||
key, | ||
callback: (value) => resolve(value), | ||
}); | ||
}); | ||
} |