Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions src/libs/actions/Policy/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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<PolicyTagLists>((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<PolicyTagLists>((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,
Expand Down
52 changes: 37 additions & 15 deletions tests/actions/PolicyTagTest.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
})

Expand All @@ -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', () => {
Expand All @@ -170,7 +192,7 @@ describe('actions/Policy', () => {
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);
})
.then(() => {
Tag.renamePolicyTagList(
renamePolicyTagList(
fakePolicy.id,
{
oldName: oldTagListName,
Expand Down Expand Up @@ -238,7 +260,7 @@ describe('actions/Policy', () => {
.then(() => {
mockFetch?.fail?.();

Tag.renamePolicyTagList(
renamePolicyTagList(
fakePolicy.id,
{
oldName: oldTagListName,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -350,7 +372,7 @@ describe('actions/Policy', () => {
.then(() => {
mockFetch?.fail?.();

Tag.createPolicyTag(fakePolicy.id, newTagName);
createPolicyTag(fakePolicy.id, newTagName);
return waitForBatchedUpdates();
})
.then(mockFetch?.resume)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -516,7 +538,7 @@ describe('actions/Policy', () => {
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);
})
.then(() => {
Tag.renamePolicyTag(
renamePolicyTag(
fakePolicy.id,
{
oldName: oldTagName ?? '',
Expand Down Expand Up @@ -586,7 +608,7 @@ describe('actions/Policy', () => {
.then(() => {
mockFetch?.fail?.();

Tag.renamePolicyTag(
renamePolicyTag(
fakePolicy.id,
{
oldName: oldTagName,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -695,7 +717,7 @@ describe('actions/Policy', () => {
.then(() => {
mockFetch?.fail?.();

Tag.deletePolicyTags(fakePolicy.id, tagsToDelete);
deletePolicyTags(fakePolicy.id, tagsToDelete);
return waitForBatchedUpdates();
})
.then(mockFetch?.resume)
Expand Down