diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 0f8cfb879cac7..948ca1821c597 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -1156,10 +1156,10 @@ function hasDependentTags(policy: OnyxEntry, policyTagList: OnyxEntry, policyTagList: OnyxEntry) { - if (!policy?.hasMultipleTagLists) { + if (!policy?.hasMultipleTagLists || hasDependentTags(policy, policyTagList)) { return false; } - return Object.values(policyTagList ?? {}).every((tagList) => Object.values(tagList.tags).every((tag) => !tag.rules?.parentTagsFilter && !tag.parentTagsFilter)); + return Object.values(policyTagList ?? {}).some((tagList) => Object.values(tagList.tags).length > 0); } /** Get the Xero organizations connected to the policy */ diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 2c6826a5c431d..13dd1881fd103 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -23,7 +23,9 @@ import { getTagListByOrderWeight, getUberConnectionErrorDirectlyFromPolicy, getUnitRateValue, + hasDependentTags, hasDynamicExternalWorkflow, + hasIndependentTags, hasOnlyPersonalPolicies, hasOtherControlWorkspaces, hasPolicyWithXeroConnection, @@ -37,7 +39,7 @@ import {isWorkspaceEligibleForReportChange} from '@libs/ReportUtils'; import CONST from '@src/CONST'; import {getPolicyBrickRoadIndicatorStatus} from '@src/libs/PolicyUtils'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {PersonalDetailsList, Policy, PolicyEmployeeList, Report, Transaction} from '@src/types/onyx'; +import type {PersonalDetailsList, Policy, PolicyEmployeeList, PolicyTagLists, Report, Transaction} from '@src/types/onyx'; import type {Connections} from '@src/types/onyx/Policy'; import createCollection from '../utils/collections/createCollection'; import createRandomPolicy from '../utils/collections/policies'; @@ -2081,4 +2083,148 @@ describe('PolicyUtils', () => { expect(result.size).toBe(2); }); }); + + describe('hasDependentTags', () => { + it('returns false when policy has no multiple tag lists', () => { + const policy = {hasMultipleTagLists: false} as Policy; + const policyTagList: PolicyTagLists = {}; + expect(hasDependentTags(policy, policyTagList)).toBe(false); + }); + + it('returns false when policy is undefined', () => { + expect(hasDependentTags(undefined, {})).toBe(false); + }); + + it('returns false when tags have no parentTagsFilter', () => { + const policy = {hasMultipleTagLists: true} as Policy; + const policyTagList = { + Department: { + name: 'Department', + tags: { + Engineering: {name: 'Engineering', enabled: true}, + }, + required: false, + orderWeight: 0, + }, + } as PolicyTagLists; + expect(hasDependentTags(policy, policyTagList)).toBe(false); + }); + + it('returns true when a tag has rules.parentTagsFilter', () => { + const policy = {hasMultipleTagLists: true} as Policy; + const policyTagList = { + Department: { + name: 'Department', + tags: { + Engineering: {name: 'Engineering', enabled: true, rules: {parentTagsFilter: '^California$'}}, + }, + required: false, + orderWeight: 0, + }, + } as PolicyTagLists; + expect(hasDependentTags(policy, policyTagList)).toBe(true); + }); + + it('returns true when a tag has parentTagsFilter at the top level', () => { + const policy = {hasMultipleTagLists: true} as Policy; + const policyTagList = { + Department: { + name: 'Department', + tags: { + Engineering: {name: 'Engineering', enabled: true, parentTagsFilter: '^California$'}, + }, + required: false, + orderWeight: 0, + }, + } as PolicyTagLists; + expect(hasDependentTags(policy, policyTagList)).toBe(true); + }); + }); + + describe('hasIndependentTags', () => { + it('returns false when policy has no multiple tag lists', () => { + const policy = {hasMultipleTagLists: false} as Policy; + const policyTagList: PolicyTagLists = {}; + expect(hasIndependentTags(policy, policyTagList)).toBe(false); + }); + + it('returns false when policy is undefined', () => { + expect(hasIndependentTags(undefined, {})).toBe(false); + }); + + it('returns false when tags are dependent (have parentTagsFilter)', () => { + const policy = {hasMultipleTagLists: true} as Policy; + const policyTagList = { + Department: { + name: 'Department', + tags: { + Engineering: {name: 'Engineering', enabled: true, rules: {parentTagsFilter: '^California$'}}, + }, + required: false, + orderWeight: 0, + }, + } as PolicyTagLists; + expect(hasIndependentTags(policy, policyTagList)).toBe(false); + }); + + it('returns false when all tag lists are empty', () => { + const policy = {hasMultipleTagLists: true} as Policy; + const policyTagList = { + Department: { + name: 'Department', + tags: {}, + required: false, + orderWeight: 0, + }, + Location: { + name: 'Location', + tags: {}, + required: false, + orderWeight: 1, + }, + } as PolicyTagLists; + expect(hasIndependentTags(policy, policyTagList)).toBe(false); + }); + + it('returns true when tags are independent and at least one tag exists', () => { + const policy = {hasMultipleTagLists: true} as Policy; + const policyTagList = { + Department: { + name: 'Department', + tags: { + Engineering: {name: 'Engineering', enabled: true}, + }, + required: false, + orderWeight: 0, + }, + } as PolicyTagLists; + expect(hasIndependentTags(policy, policyTagList)).toBe(true); + }); + + it('returns true when at least one tag list has tags and others are empty', () => { + const policy = {hasMultipleTagLists: true} as Policy; + const policyTagList = { + Department: { + name: 'Department', + tags: {}, + required: false, + orderWeight: 0, + }, + Location: { + name: 'Location', + tags: { + 'New York': {name: 'New York', enabled: true}, + }, + required: false, + orderWeight: 1, + }, + } as PolicyTagLists; + expect(hasIndependentTags(policy, policyTagList)).toBe(true); + }); + + it('returns false when policyTagList is undefined', () => { + const policy = {hasMultipleTagLists: true} as Policy; + expect(hasIndependentTags(policy, undefined)).toBe(false); + }); + }); });