Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

let allPolicies: OnyxCollection<Policy>;

Onyx.connect({

Check warning on line 75 in src/libs/PolicyUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
Expand Down Expand Up @@ -1156,10 +1156,10 @@
}

function hasIndependentTags(policy: OnyxEntry<Policy>, policyTagList: OnyxEntry<PolicyTagLists>) {
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 */
Expand Down
148 changes: 147 additions & 1 deletion tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import {
getTagListByOrderWeight,
getUberConnectionErrorDirectlyFromPolicy,
getUnitRateValue,
hasDependentTags,
hasDynamicExternalWorkflow,
hasIndependentTags,
hasOnlyPersonalPolicies,
hasOtherControlWorkspaces,
hasPolicyWithXeroConnection,
Expand All @@ -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';
Expand Down Expand Up @@ -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);
});
});
});
Loading