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
16 changes: 9 additions & 7 deletions src/libs/actions/Policy/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,14 +975,16 @@ function setPolicyRequiresTag(policyID: string, requiresTag: boolean) {
API.write(WRITE_COMMANDS.SET_POLICY_REQUIRES_TAG, parameters, onyxData);
}

function setPolicyTagsRequired(policyID: string, requiresTag: boolean, tagListIndex: number) {
const policyTag = PolicyUtils.getTagLists(allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {})?.at(tagListIndex);

if (!policyTag) {
return;
}
type SetPolicyTagsRequiredProps = {
policyID: string;
requiresTag: boolean;
tagListIndex: number;
policyTags: OnyxEntry<PolicyTagLists>;
};

if (!policyTag.name) {
function setPolicyTagsRequired({policyID, requiresTag, tagListIndex, policyTags}: SetPolicyTagsRequiredProps) {
const policyTag = PolicyUtils.getTagLists(policyTags ?? {})?.at(tagListIndex);
if (!policyTag?.name) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/workspace/tags/WorkspaceTagsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {

const updateWorkspaceRequiresTag = useCallback(
(value: boolean, orderWeight: number) => {
setPolicyTagsRequired(policyID, value, orderWeight);
setPolicyTagsRequired({policyID, requiresTag: value, tagListIndex: orderWeight, policyTags});
},
[policyID],
[policyID, policyTags],
);

const tagList = useMemo<TagListItem[]>(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/workspace/tags/WorkspaceViewTagsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function WorkspaceViewTagsPage({route}: WorkspaceViewTagsProps) {
};

if (!!currentPolicyTag?.required && !Object.values(currentPolicyTag?.tags ?? {}).some((tag) => tag.enabled)) {
setPolicyTagsRequired(policyID, false, route.params.orderWeight);
setPolicyTagsRequired({policyID, requiresTag: false, tagListIndex: route.params.orderWeight, policyTags});
}

const navigateToEditTag = () => {
Expand Down Expand Up @@ -376,7 +376,7 @@ function WorkspaceViewTagsPage({route}: WorkspaceViewTagsProps) {
return;
}

setPolicyTagsRequired(policyID, on, route.params.orderWeight);
setPolicyTagsRequired({policyID, requiresTag: on, tagListIndex: route.params.orderWeight, policyTags});
}}
pendingAction={currentPolicyTag.pendingFields?.required}
errors={currentPolicyTag?.errorFields?.required ?? undefined}
Expand Down
149 changes: 149 additions & 0 deletions tests/actions/PolicyTagTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
renamePolicyTag,
renamePolicyTagList,
setPolicyRequiresTag,
setPolicyTagsRequired,
setWorkspaceTagEnabled,
} from '@libs/actions/Policy/Tag';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -1529,4 +1530,152 @@ describe('actions/Policy', () => {
});
});
});

describe('SetPolicyTagsRequired', () => {
it('should set tag list as required when requiresTag is true', async () => {
// Given a policy with a tag list that is not required
const fakePolicy = createRandomPolicy(0);
const tagListName = 'Test Tag List';
const fakePolicyTags = createRandomPolicyTags(tagListName, 2);

fakePolicyTags[tagListName] = {
...fakePolicyTags[tagListName],
required: false,
orderWeight: 0,
};

mockFetch.pause();

await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);

// When setPolicyTagsRequired is called with requiresTag = true
setPolicyTagsRequired({policyID: fakePolicy.id, requiresTag: true, tagListIndex: 0, policyTags: fakePolicyTags});
await waitForBatchedUpdates();

// Then the tag list should be marked as required with pending fields
let updatedPolicyTags = await OnyxUtils.get(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`);

expect(updatedPolicyTags?.[tagListName]?.required).toBe(true);
// Check optimistic data - pendingFields should be set
if (updatedPolicyTags?.[tagListName]?.pendingFields?.required) {
expect(updatedPolicyTags[tagListName].pendingFields.required).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
}

mockFetch.resume();
await waitForBatchedUpdates();

// Then after API success, pending fields should be cleared
updatedPolicyTags = await OnyxUtils.get(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`);

expect(updatedPolicyTags?.[tagListName]?.required).toBe(true);
expect(updatedPolicyTags?.[tagListName]?.pendingFields?.required).toBeUndefined();
});

it('should set tag list as not required when requiresTag is false', async () => {
// Given a policy with a tag list that is required
const fakePolicy = createRandomPolicy(0);
const tagListName = 'Test Tag List';
const fakePolicyTags = createRandomPolicyTags(tagListName, 2);

fakePolicyTags[tagListName] = {
...fakePolicyTags[tagListName],
required: true,
orderWeight: 0,
};

mockFetch.pause();

await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);

// When setPolicyTagsRequired is called with requiresTag = false
setPolicyTagsRequired({policyID: fakePolicy.id, requiresTag: false, tagListIndex: 0, policyTags: fakePolicyTags});
await waitForBatchedUpdates();

// Then the tag list should be marked as not required with pending fields
let updatedPolicyTags = await OnyxUtils.get(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`);

expect(updatedPolicyTags?.[tagListName]?.required).toBe(false);
// Check optimistic data - pendingFields should be set
if (updatedPolicyTags?.[tagListName]?.pendingFields?.required) {
expect(updatedPolicyTags[tagListName].pendingFields.required).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
}

mockFetch.resume();
await waitForBatchedUpdates();

// Then after API success, pending fields should be cleared
updatedPolicyTags = await OnyxUtils.get(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`);

expect(updatedPolicyTags?.[tagListName]?.required).toBe(false);
expect(updatedPolicyTags?.[tagListName]?.pendingFields?.required).toBeUndefined();
});

it('should handle API failure and restore original state with error', async () => {
// Given a policy with a tag list that is not required
const fakePolicy = createRandomPolicy(0);
const tagListName = 'Test Tag List';
const fakePolicyTags = createRandomPolicyTags(tagListName, 2);

fakePolicyTags[tagListName] = {
...fakePolicyTags[tagListName],
required: false,
orderWeight: 0,
};

mockFetch.pause();

await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);

// When setPolicyTagsRequired is called and API fails
mockFetch.fail();
setPolicyTagsRequired({policyID: fakePolicy.id, requiresTag: true, tagListIndex: 0, policyTags: fakePolicyTags});
await waitForBatchedUpdates();

mockFetch.resume();
await waitForBatchedUpdates();

// Then the tag list should be restored to original state with error
const updatedPolicyTags = await OnyxUtils.get(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`);

expect(updatedPolicyTags?.[tagListName]?.required).toBe(false);
expect(updatedPolicyTags?.[tagListName]?.pendingFields?.required).toBeUndefined();
expect(updatedPolicyTags?.[tagListName]?.errorFields?.required).toBeTruthy();
});

it('should work with data from useOnyx hook', async () => {
// Given a policy with a tag list that is not required
const fakePolicy = createRandomPolicy(0);
const tagListName = 'Test Tag List';
const fakePolicyTags = createRandomPolicyTags(tagListName, 2);

fakePolicyTags[tagListName] = {
...fakePolicyTags[tagListName],
required: false,
orderWeight: 0,
};

await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);

const {result} = renderHook(() => useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`));

await waitFor(() => {
expect(result.current[0]).toBeDefined();
});

await act(async () => {
// When setPolicyTagsRequired is called with data from useOnyx
setPolicyTagsRequired({policyID: fakePolicy.id, requiresTag: true, tagListIndex: 0, policyTags: result.current[0]});
await waitForBatchedUpdates();
});

// Then the tag list should be marked as required
const updatedPolicyTags = await OnyxUtils.get(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`);

expect(updatedPolicyTags?.[tagListName]?.required).toBe(true);
// Check optimistic data - pendingFields should be set
if (updatedPolicyTags?.[tagListName]?.pendingFields?.required) {
expect(updatedPolicyTags[tagListName].pendingFields.required).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
}
});
});
});