|
| 1 | +import type {OnyxCollection} from 'react-native-onyx'; |
| 2 | +import Onyx from 'react-native-onyx'; |
| 3 | +import {getBrickRoadForPolicy} from '@libs/WorkspacesSettingsUtils'; |
| 4 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 5 | +import type {Report, ReportActions, TransactionViolations} from '@src/types/onyx'; |
| 6 | +import type {ReportCollectionDataSet} from '@src/types/onyx/Report'; |
| 7 | +import * as TestHelper from '../utils/TestHelper'; |
| 8 | +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; |
| 9 | +import mockData from './WorkspaceSettingsUtilsTest.json'; |
| 10 | + |
| 11 | +describe('WorkspacesSettingsUtils', () => { |
| 12 | + beforeAll(() => { |
| 13 | + Onyx.init({ |
| 14 | + keys: ONYXKEYS, |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + global.fetch = TestHelper.getGlobalFetchMock(); |
| 20 | + return Onyx.clear().then(waitForBatchedUpdates); |
| 21 | + }); |
| 22 | + describe('getBrickRoadForPolicy', () => { |
| 23 | + it('Should return "error"', async () => { |
| 24 | + // Given mock data for reports, transaction violations, sessions, and report actions. |
| 25 | + const report = Object.values(mockData.reports)?.at(0); |
| 26 | + const transactionViolations = mockData.transactionViolations; |
| 27 | + const reports = mockData.reports; |
| 28 | + const session = mockData.session; |
| 29 | + const reportActions = mockData.reportActions; |
| 30 | + |
| 31 | + await Onyx.multiSet({ |
| 32 | + ...(reports as ReportCollectionDataSet), |
| 33 | + ...(reportActions as OnyxCollection<ReportActions>), |
| 34 | + ...(transactionViolations as OnyxCollection<TransactionViolations>), |
| 35 | + session, |
| 36 | + }); |
| 37 | + |
| 38 | + await waitForBatchedUpdates(); |
| 39 | + |
| 40 | + // When calling getBrickRoadForPolicy with a report and report actions |
| 41 | + const result = getBrickRoadForPolicy(report as Report, reportActions as OnyxCollection<ReportActions>); |
| 42 | + |
| 43 | + // The result should be 'error' because there is at least one IOU action associated with a transaction that has a violation. |
| 44 | + expect(result).toBe('error'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('Should return "undefined"', async () => { |
| 48 | + // Given mock data for reports, sessions, and report actions. Note: Transaction data is intentionally excluded. |
| 49 | + const report = Object.values(mockData.reports)?.at(0); |
| 50 | + const reports = mockData.reports; |
| 51 | + const session = mockData.session; |
| 52 | + const reportActions = mockData.reportActions; |
| 53 | + |
| 54 | + await Onyx.multiSet({ |
| 55 | + ...(reports as ReportCollectionDataSet), |
| 56 | + ...(reportActions as OnyxCollection<ReportActions>), |
| 57 | + session, |
| 58 | + }); |
| 59 | + |
| 60 | + await waitForBatchedUpdates(); |
| 61 | + |
| 62 | + // When calling getBrickRoadForPolicy with a report and report actions |
| 63 | + const result = getBrickRoadForPolicy(report as Report, reportActions as OnyxCollection<ReportActions>); |
| 64 | + |
| 65 | + // Then the result should be 'undefined' since no IOU action is linked to a transaction with a violation. |
| 66 | + expect(result).toBe(undefined); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments