-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[No QA] Remove Onyx.connect() for the key: ONYXKEYS.COLLECTION.REPORT in src/libs/ReportUtils.ts (part 1) #86953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+294
−95
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2b255b9
fix: init derived key
truph01 a9bc22a
fix: add deprecated prefix
truph01 5360d65
fix: add test
truph01 eeb4067
fix: conflicts
truph01 8f1b22f
fix: conflicts
truph01 4cf80d0
fix: conflicts
truph01 c488baf
fix: conflicts
truph01 a452d31
fix: export createReport util in test
truph01 6c011af
fix: rename key
truph01 044f4e2
fix: lint
truph01 1cf117c
fix: tests
truph01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
38 changes: 38 additions & 0 deletions
38
src/libs/actions/OnyxDerived/configs/openAndSubmittedReportsByPolicyID.ts
This file contains hidden or 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,38 @@ | ||
| import createOnyxDerivedValueConfig from '@userActions/OnyxDerived/createOnyxDerivedValueConfig'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {OpenAndSubmittedReportsByPolicyIDDerivedValue} from '@src/types/onyx'; | ||
|
|
||
| export default createOnyxDerivedValueConfig({ | ||
| key: ONYXKEYS.DERIVED.OPEN_AND_SUBMITTED_REPORTS_BY_POLICY_ID, | ||
| dependencies: [ONYXKEYS.COLLECTION.REPORT, ONYXKEYS.SESSION], | ||
| compute: ([reports, session]) => { | ||
| if (!reports) { | ||
| return {}; | ||
| } | ||
|
|
||
| const currentUserAccountID = session?.accountID; | ||
|
|
||
| return Object.entries(reports).reduce<OpenAndSubmittedReportsByPolicyIDDerivedValue>((acc, [reportID, report]) => { | ||
| if (!report) { | ||
| return acc; | ||
| } | ||
|
|
||
| // Get all reports, which are the ones that are: | ||
| // - Owned by the current user | ||
| // - Are either open or submitted | ||
| // - Belong to a workspace | ||
| if (report.policyID && report.ownerAccountID === currentUserAccountID && (report.stateNum ?? 0) <= 1) { | ||
| if (!acc[report.policyID]) { | ||
| acc[report.policyID] = {}; | ||
| } | ||
|
|
||
| acc[report.policyID] = { | ||
| ...acc[report.policyID], | ||
| [reportID]: report, | ||
| }; | ||
| } | ||
|
|
||
| return acc; | ||
| }, {}); | ||
| }, | ||
| }); | ||
This file contains hidden or 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 hidden or 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 hidden or 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
156 changes: 156 additions & 0 deletions
156
tests/unit/OnyxDerived/openAndSubmittedReportsByPolicyIDTest.ts
This file contains hidden or 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,156 @@ | ||
| import type {OnyxCollection} from 'react-native-onyx'; | ||
| import openAndSubmittedReportsByPolicyIDConfig from '@libs/actions/OnyxDerived/configs/openAndSubmittedReportsByPolicyID'; | ||
| import type {DerivedValueContext} from '@libs/actions/OnyxDerived/types'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Report, Session} from '@src/types/onyx'; | ||
| import {createMockReport} from '../../utils/ReportTestUtils'; | ||
|
|
||
| const CURRENT_USER_ACCOUNT_ID = 1; | ||
| const OTHER_USER_ACCOUNT_ID = 2; | ||
| const POLICY_ID_1 = 'policy1'; | ||
| const POLICY_ID_2 = 'policy2'; | ||
|
|
||
| function createReport(reportID: string, overrides: Partial<Report> = {}): Report { | ||
| return createMockReport({ | ||
| reportID, | ||
| type: CONST.REPORT.TYPE.EXPENSE, | ||
| ownerAccountID: CURRENT_USER_ACCOUNT_ID, | ||
| policyID: POLICY_ID_1, | ||
| stateNum: CONST.REPORT.STATE_NUM.OPEN, | ||
| statusNum: CONST.REPORT.STATUS_NUM.OPEN, | ||
| ...overrides, | ||
| }); | ||
| } | ||
|
|
||
| function buildReports(...reports: Report[]): OnyxCollection<Report> { | ||
| const result: OnyxCollection<Report> = {}; | ||
| for (const report of reports) { | ||
| result[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = report; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function createSession(accountID: number = CURRENT_USER_ACCOUNT_ID): Session { | ||
| return {accountID} as Session; | ||
| } | ||
|
|
||
| const {compute} = openAndSubmittedReportsByPolicyIDConfig; | ||
| const emptyContext = {} as DerivedValueContext<typeof ONYXKEYS.DERIVED.OPEN_AND_SUBMITTED_REPORTS_BY_POLICY_ID, [typeof ONYXKEYS.COLLECTION.REPORT, typeof ONYXKEYS.SESSION]>; | ||
|
|
||
| describe('openAndSubmittedReportsByPolicyID derived value', () => { | ||
| it('returns empty object when reports is null/undefined', () => { | ||
| const result = compute([undefined, createSession()], emptyContext); | ||
| expect(result).toEqual({}); | ||
| }); | ||
|
|
||
| it('returns empty object when reports is empty', () => { | ||
| const result = compute([{}, createSession()], emptyContext); | ||
| expect(result).toEqual({}); | ||
| }); | ||
|
|
||
| it('groups open reports owned by current user by policyID', () => { | ||
| const report1 = createReport('1', {policyID: POLICY_ID_1}); | ||
| const report2 = createReport('2', {policyID: POLICY_ID_2}); | ||
| const reports = buildReports(report1, report2); | ||
|
|
||
| const result = compute([reports, createSession()], emptyContext); | ||
|
|
||
| expect(Object.keys(result)).toHaveLength(2); | ||
| expect(result[POLICY_ID_1]).toEqual({[`${ONYXKEYS.COLLECTION.REPORT}1`]: report1}); | ||
| expect(result[POLICY_ID_2]).toEqual({[`${ONYXKEYS.COLLECTION.REPORT}2`]: report2}); | ||
| }); | ||
|
|
||
| it('groups multiple reports under the same policyID', () => { | ||
| const report1 = createReport('1', {policyID: POLICY_ID_1}); | ||
| const report2 = createReport('2', {policyID: POLICY_ID_1}); | ||
| const reports = buildReports(report1, report2); | ||
|
|
||
| const result = compute([reports, createSession()], emptyContext); | ||
|
|
||
| expect(Object.keys(result)).toHaveLength(1); | ||
| expect(result[POLICY_ID_1]).toEqual({ | ||
| [`${ONYXKEYS.COLLECTION.REPORT}1`]: report1, | ||
| [`${ONYXKEYS.COLLECTION.REPORT}2`]: report2, | ||
| }); | ||
| }); | ||
|
|
||
| it('excludes reports not owned by current user', () => { | ||
| const ownReport = createReport('1', {ownerAccountID: CURRENT_USER_ACCOUNT_ID}); | ||
| const otherReport = createReport('2', {ownerAccountID: OTHER_USER_ACCOUNT_ID}); | ||
| const reports = buildReports(ownReport, otherReport); | ||
|
|
||
| const result = compute([reports, createSession()], emptyContext); | ||
|
|
||
| expect(Object.keys(result)).toHaveLength(1); | ||
| expect(result[POLICY_ID_1]).toEqual({[`${ONYXKEYS.COLLECTION.REPORT}1`]: ownReport}); | ||
| }); | ||
|
|
||
| it('excludes reports without a policyID', () => { | ||
| const reportWithPolicy = createReport('1', {policyID: POLICY_ID_1}); | ||
| const reportWithoutPolicy = createReport('2', {policyID: undefined}); | ||
| const reports = buildReports(reportWithPolicy, reportWithoutPolicy); | ||
|
|
||
| const result = compute([reports, createSession()], emptyContext); | ||
|
|
||
| expect(Object.keys(result)).toHaveLength(1); | ||
| expect(result[POLICY_ID_1]).toEqual({[`${ONYXKEYS.COLLECTION.REPORT}1`]: reportWithPolicy}); | ||
| }); | ||
|
|
||
| it('includes open reports (stateNum = 0)', () => { | ||
| const report = createReport('1', {stateNum: CONST.REPORT.STATE_NUM.OPEN}); | ||
| const reports = buildReports(report); | ||
|
|
||
| const result = compute([reports, createSession()], emptyContext); | ||
|
|
||
| expect(result[POLICY_ID_1]).toEqual({[`${ONYXKEYS.COLLECTION.REPORT}1`]: report}); | ||
| }); | ||
|
|
||
| it('includes submitted reports (stateNum = 1)', () => { | ||
| const report = createReport('1', {stateNum: CONST.REPORT.STATE_NUM.SUBMITTED}); | ||
| const reports = buildReports(report); | ||
|
|
||
| const result = compute([reports, createSession()], emptyContext); | ||
|
|
||
| expect(result[POLICY_ID_1]).toEqual({[`${ONYXKEYS.COLLECTION.REPORT}1`]: report}); | ||
| }); | ||
|
|
||
| it('excludes approved reports (stateNum = 2)', () => { | ||
| const report = createReport('1', {stateNum: CONST.REPORT.STATE_NUM.APPROVED}); | ||
| const reports = buildReports(report); | ||
|
|
||
| const result = compute([reports, createSession()], emptyContext); | ||
|
|
||
| expect(result).toEqual({}); | ||
| }); | ||
|
|
||
| it('treats undefined stateNum as open (defaults to 0)', () => { | ||
| const report = createReport('1', {stateNum: undefined}); | ||
| const reports = buildReports(report); | ||
|
|
||
| const result = compute([reports, createSession()], emptyContext); | ||
|
|
||
| expect(result[POLICY_ID_1]).toEqual({[`${ONYXKEYS.COLLECTION.REPORT}1`]: report}); | ||
| }); | ||
|
|
||
| it('skips null report entries in collection', () => { | ||
| const report = createReport('1'); | ||
| const reports = buildReports(report); | ||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
| reports![`${ONYXKEYS.COLLECTION.REPORT}2`] = undefined; | ||
|
|
||
| const result = compute([reports, createSession()], emptyContext); | ||
|
|
||
| expect(Object.keys(result)).toHaveLength(1); | ||
| expect(result[POLICY_ID_1]).toEqual({[`${ONYXKEYS.COLLECTION.REPORT}1`]: report}); | ||
| }); | ||
|
|
||
| it('returns empty object when session has no accountID', () => { | ||
| const report = createReport('1'); | ||
| const reports = buildReports(report); | ||
|
|
||
| const result = compute([reports, {} as Session], emptyContext); | ||
|
|
||
| expect(result).toEqual({}); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.