Skip to content

Commit

Permalink
add OnyxUpdates test for applying airship updates
Browse files Browse the repository at this point in the history
  • Loading branch information
arosiclair committed Jun 5, 2024
1 parent 340038d commit 45243f8
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions tests/unit/OnyxUpdatesTest.ts
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),
});
});
}

0 comments on commit 45243f8

Please sign in to comment.