diff --git a/src/libs/actions/Policy/Tag.ts b/src/libs/actions/Policy/Tag.ts index e332f3b472c7..22e42f90d244 100644 --- a/src/libs/actions/Policy/Tag.ts +++ b/src/libs/actions/Policy/Tag.ts @@ -737,7 +737,6 @@ function renamePolicyTagList(policyID: string, policyTagListName: {oldName: stri function setPolicyRequiresTag(policyID: string, requiresTag: boolean) { const policyTags = allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {}; - const isMultiLevelTags = PolicyUtils.isMultiLevelTags(policyTags); const onyxData: OnyxData = { optimisticData: [ @@ -782,25 +781,23 @@ function setPolicyRequiresTag(policyID: string, requiresTag: boolean) { ], }; - if (isMultiLevelTags) { - const getUpdatedTagsData = (required: boolean): OnyxUpdate => ({ - key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, - onyxMethod: Onyx.METHOD.MERGE, - value: { - ...Object.keys(policyTags).reduce((acc, key) => { - acc[key] = { - ...acc[key], - required, - }; - return acc; - }, {}), - }, - }); + const getUpdatedTagsData = (required: boolean): OnyxUpdate => ({ + key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, + onyxMethod: Onyx.METHOD.MERGE, + value: { + ...Object.keys(policyTags).reduce((acc, key) => { + acc[key] = { + ...acc[key], + required, + }; + return acc; + }, {}), + }, + }); - onyxData.optimisticData?.push(getUpdatedTagsData(requiresTag)); - onyxData.failureData?.push(getUpdatedTagsData(!requiresTag)); - onyxData.successData?.push(getUpdatedTagsData(requiresTag)); - } + onyxData.optimisticData?.push(getUpdatedTagsData(requiresTag)); + onyxData.failureData?.push(getUpdatedTagsData(!requiresTag)); + onyxData.successData?.push(getUpdatedTagsData(requiresTag)); const parameters = { policyID, diff --git a/tests/actions/PolicyTagTest.ts b/tests/actions/PolicyTagTest.ts index 7c01a05df5ac..f590f3be27b8 100644 --- a/tests/actions/PolicyTagTest.ts +++ b/tests/actions/PolicyTagTest.ts @@ -1,9 +1,9 @@ import Onyx from 'react-native-onyx'; import OnyxUpdateManager from '@libs/actions/OnyxUpdateManager'; -import * as Tag from '@userActions/Policy/Tag'; +import {createPolicyTag, deletePolicyTags, renamePolicyTag, renamePolicyTagList, setPolicyRequiresTag, setWorkspaceTagEnabled} from '@libs/actions/Policy/Tag'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {PolicyTags} from '@src/types/onyx'; +import type {PolicyTagLists, PolicyTags} from '@src/types/onyx'; import createRandomPolicy from '../utils/collections/policies'; import createRandomPolicyTags from '../utils/collections/policyTags'; import * as TestHelper from '../utils/TestHelper'; @@ -34,7 +34,7 @@ describe('actions/Policy', () => { return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { - Tag.setPolicyRequiresTag(fakePolicy.id, true); + setPolicyRequiresTag(fakePolicy.id, true); return waitForBatchedUpdates(); }) .then( @@ -81,7 +81,7 @@ describe('actions/Policy', () => { return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { - Tag.setPolicyRequiresTag(fakePolicy.id, false); + setPolicyRequiresTag(fakePolicy.id, false); return waitForBatchedUpdates(); }) .then( @@ -129,7 +129,7 @@ describe('actions/Policy', () => { return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { mockFetch?.fail?.(); - Tag.setPolicyRequiresTag(fakePolicy.id, false); + setPolicyRequiresTag(fakePolicy.id, false); return waitForBatchedUpdates(); }) @@ -152,6 +152,28 @@ describe('actions/Policy', () => { }), ); }); + + it('should update required field in policy tag list', async () => { + const fakePolicy = createRandomPolicy(0); + const tagListName = 'Tag'; + fakePolicy.requiresTag = false; + + const fakePolicyTags = createRandomPolicyTags(tagListName); + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); + + setPolicyRequiresTag(fakePolicy.id, true); + await waitForBatchedUpdates(); + + let updatePolicyTags: PolicyTagLists | undefined; + + await TestHelper.getOnyxData({ + key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, + callback: (val) => (updatePolicyTags = val), + }); + + expect(updatePolicyTags?.[tagListName]?.required).toBeTruthy(); + }); }); describe('renamePolicyTagList', () => { @@ -170,7 +192,7 @@ describe('actions/Policy', () => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - Tag.renamePolicyTagList( + renamePolicyTagList( fakePolicy.id, { oldName: oldTagListName, @@ -238,7 +260,7 @@ describe('actions/Policy', () => { .then(() => { mockFetch?.fail?.(); - Tag.renamePolicyTagList( + renamePolicyTagList( fakePolicy.id, { oldName: oldTagListName, @@ -288,7 +310,7 @@ describe('actions/Policy', () => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - Tag.createPolicyTag(fakePolicy.id, newTagName); + createPolicyTag(fakePolicy.id, newTagName); return waitForBatchedUpdates(); }) .then( @@ -350,7 +372,7 @@ describe('actions/Policy', () => { .then(() => { mockFetch?.fail?.(); - Tag.createPolicyTag(fakePolicy.id, newTagName); + createPolicyTag(fakePolicy.id, newTagName); return waitForBatchedUpdates(); }) .then(mockFetch?.resume) @@ -397,7 +419,7 @@ describe('actions/Policy', () => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - Tag.setWorkspaceTagEnabled(fakePolicy.id, tagsToUpdate, 0); + setWorkspaceTagEnabled(fakePolicy.id, tagsToUpdate, 0); return waitForBatchedUpdates(); }) .then( @@ -470,7 +492,7 @@ describe('actions/Policy', () => { .then(() => { mockFetch?.fail?.(); - Tag.setWorkspaceTagEnabled(fakePolicy.id, tagsToUpdate, 0); + setWorkspaceTagEnabled(fakePolicy.id, tagsToUpdate, 0); return waitForBatchedUpdates(); }) .then(mockFetch?.resume) @@ -516,7 +538,7 @@ describe('actions/Policy', () => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - Tag.renamePolicyTag( + renamePolicyTag( fakePolicy.id, { oldName: oldTagName ?? '', @@ -586,7 +608,7 @@ describe('actions/Policy', () => { .then(() => { mockFetch?.fail?.(); - Tag.renamePolicyTag( + renamePolicyTag( fakePolicy.id, { oldName: oldTagName, @@ -635,7 +657,7 @@ describe('actions/Policy', () => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - Tag.deletePolicyTags(fakePolicy.id, tagsToDelete); + deletePolicyTags(fakePolicy.id, tagsToDelete); return waitForBatchedUpdates(); }) .then( @@ -695,7 +717,7 @@ describe('actions/Policy', () => { .then(() => { mockFetch?.fail?.(); - Tag.deletePolicyTags(fakePolicy.id, tagsToDelete); + deletePolicyTags(fakePolicy.id, tagsToDelete); return waitForBatchedUpdates(); }) .then(mockFetch?.resume)