Skip to content
Merged
80 changes: 40 additions & 40 deletions tests/unit/OnyxDerivedTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('OnyxDerived', () => {
initOnyxDerivedValues();
});

beforeEach(() => {
Onyx.clear();
beforeEach(async () => {
await Onyx.clear();
});

describe('conciergeChatReportID', () => {
Expand All @@ -33,55 +33,55 @@ describe('OnyxDerived', () => {

describe('reportAttributes', () => {
const mockReport = {
reportID: '123',
reportID: `test_1`,
reportName: 'Test Report',
type: 'chat',
participants: ['email1@test.com', 'email2@test.com'],
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ROOM,
lastVisibleActionCreated: '2023-01-01T00:00:00.000Z',
lastMessageText: 'Test message',
lastActorAccountID: 1,
lastMessageHtml: '<p>Test message</p>',
policyID: '123',
ownerAccountID: 1,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
};

const mockReports = {
[mockReport.reportID]: mockReport,
};

it('returns empty reports when dependencies are not set', () => {
waitForBatchedUpdates().then(async () => {
const derivedReportAttributes = await OnyxUtils.get(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES);
expect(derivedReportAttributes).toEqual({
reports: {},
locale: null,
});
it('returns empty reports when dependencies are not set', async () => {
await waitForBatchedUpdates();
const derivedReportAttributes = await OnyxUtils.get(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES);
expect(derivedReportAttributes).toMatchObject({
reports: {},
});
});

it('computes report attributes when reports are set', () => {
waitForBatchedUpdates().then(async () => {
await Onyx.set(ONYXKEYS.COLLECTION.REPORT, mockReports);
await Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, 'en');
const derivedReportAttributes = await OnyxUtils.get(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES);
expect(derivedReportAttributes).toEqual({
reports: {
[mockReport.reportID]: {
reportName: expect(String),
},
it('computes report attributes when reports are set', async () => {
await waitForBatchedUpdates();

await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${mockReport.reportID}`, mockReport);
await Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, 'en');

const derivedReportAttributes = await OnyxUtils.get(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES);

expect(derivedReportAttributes).toMatchObject({
reports: {
[mockReport.reportID]: {
reportName: mockReport.reportName,
},
locale: 'en',
});
},
});
});

it('updates when locale changes', () => {
waitForBatchedUpdates().then(async () => {
await Onyx.set(ONYXKEYS.COLLECTION.REPORT, mockReports);
await Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, 'es');
const derivedReportAttributes = await OnyxUtils.get(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES);
expect(derivedReportAttributes).toEqual({
reports: {
[mockReport.reportID]: {
reportName: expect(String),
},
},
locale: 'es',
});
it('updates when locale changes', async () => {
await waitForBatchedUpdates();

await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${mockReport.reportID}`, mockReport);
await Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, 'es');

const derivedReportAttributes = await OnyxUtils.get(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES);

expect(derivedReportAttributes).toMatchObject({
locale: 'es',
});
});
});
Expand Down