From 39f80f992f6a52bd21ab014e1ee3e8f26849269f Mon Sep 17 00:00:00 2001 From: KJ21-ENG <140263938+KJ21-ENG@users.noreply.github.com> Date: Sat, 25 Jul 2026 01:55:52 +0530 Subject: [PATCH 1/3] Cleanup DomainSelectorsTest unsafe type assertions --- tests/unit/DomainSelectorsTest.ts | 271 +++++++++++++++--------------- 1 file changed, 134 insertions(+), 137 deletions(-) diff --git a/tests/unit/DomainSelectorsTest.ts b/tests/unit/DomainSelectorsTest.ts index 0db58ab45406..9e677d59c799 100644 --- a/tests/unit/DomainSelectorsTest.ts +++ b/tests/unit/DomainSelectorsTest.ts @@ -24,6 +24,8 @@ import { vacationDelegateSelector, } from '@selectors/Domain'; +import createMock from '../utils/createMock'; + describe('domainSelectors', () => { const userID1 = 123; const userID2 = 456; @@ -33,35 +35,36 @@ describe('domainSelectors', () => { }); it('Should return an array of admin IDs when keys start with the admin access prefix', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123`]: 321, [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}321`]: 123, - } as unknown as OnyxEntry; + }); expect(adminAccountIDsSelector(domain)).toEqual([321, 123]); }); it('Should ignore keys that do not start with the admin access prefix', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123`]: 321, - somOtherProperty: 'value', - } as unknown as OnyxEntry; + email: 'test@example.com', + }); expect(adminAccountIDsSelector(domain)).toEqual([321]); }); it('Should ignore keys with falsy values even if they have the correct prefix', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123`]: 123, [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}0`]: undefined, + // @ts-expect-error -- a null admin value is a deliberately malformed runtime entry. [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}999`]: null, - } as unknown as OnyxEntry; + }); expect(adminAccountIDsSelector(domain)).toEqual([123]); }); it('Should return an empty array if the domain object is empty', () => { - const domain = {} as OnyxEntry; + const domain = createMock>({}); expect(adminAccountIDsSelector(domain)).toEqual([]); }); }); @@ -75,7 +78,7 @@ describe('domainSelectors', () => { }); it('Should return undefined values if shared NVP is empty', () => { - const domainMemberSharedNVP = {} as OnyxEntry; + const domainMemberSharedNVP = createMock>({}); expect(technicalContactSettingsSelector(domainMemberSharedNVP)).toEqual({ technicalContactEmail: undefined, @@ -84,12 +87,12 @@ describe('domainSelectors', () => { }); it('Should return technical contact settings when present', () => { - const domainMemberSharedNVP = { + const domainMemberSharedNVP = createMock>({ settings: { technicalContactEmail: 'tech@example.com', useTechnicalContactBillingCard: true, }, - } as OnyxEntry; + }); expect(technicalContactSettingsSelector(domainMemberSharedNVP)).toEqual({ technicalContactEmail: 'tech@example.com', @@ -98,11 +101,11 @@ describe('domainSelectors', () => { }); it('Should handle partial settings correctly', () => { - const domainMemberSharedNVP = { + const domainMemberSharedNVP = createMock>({ settings: { technicalContactEmail: 'tech@example.com', }, - } as OnyxEntry; + }); expect(technicalContactSettingsSelector(domainMemberSharedNVP)).toEqual({ technicalContactEmail: 'tech@example.com', @@ -111,9 +114,9 @@ describe('domainSelectors', () => { }); it('Should return undefined values if settings are empty', () => { - const domainMemberSharedNVP = { + const domainMemberSharedNVP = createMock>({ settings: {}, - } as OnyxEntry; + }); expect(technicalContactSettingsSelector(domainMemberSharedNVP)).toEqual({ technicalContactEmail: undefined, @@ -124,9 +127,9 @@ describe('domainSelectors', () => { describe('domainEmailSelector', () => { it('Should return the email when it exists in the domain object', () => { - const domain = { + const domain = createMock>({ email: '+@expensify.com', - } as OnyxEntry; + }); expect(domainEmailSelector(domain)).toBe('+@expensify.com'); }); @@ -136,7 +139,7 @@ describe('domainSelectors', () => { }); it('Should return undefined if the email property is missing', () => { - const domain = {} as OnyxEntry; + const domain = createMock>({}); expect(domainEmailSelector(domain)).toBeUndefined(); }); @@ -145,18 +148,18 @@ describe('domainSelectors', () => { describe('domainSettingsPrimaryContactSelector', () => { it.each([ ['undefined', undefined, undefined], - ['empty object', {} as OnyxEntry, undefined], - ['settings without technicalContactEmail', {settings: {}} as OnyxEntry, undefined], + ['empty object', createMock>({}), undefined], + ['settings without technicalContactEmail', createMock>({settings: {}}), undefined], ])('Should return undefined when domainSettings is %s', (_description, domainSettings, expected) => { expect(domainSettingsPrimaryContactSelector(domainSettings)).toBe(expected); }); it('Should return the technical contact email when it exists', () => { - const domainSettings = { + const domainSettings = createMock>({ settings: { technicalContactEmail: 'admin@example.com', }, - } as OnyxEntry; + }); expect(domainSettingsPrimaryContactSelector(domainSettings)).toBe('admin@example.com'); }); @@ -165,7 +168,7 @@ describe('domainSelectors', () => { describe('adminPendingActionSelector', () => { it.each([ ['undefined', undefined, {}], - ['empty object', {} as OnyxEntry, {}], + ['empty object', createMock>({}), {}], ])('Should return empty object when pendingAction is %s', (_description, pendingAction, expected) => { expect(adminPendingActionSelector(pendingAction)).toEqual(expected); }); @@ -194,86 +197,82 @@ describe('domainSelectors', () => { }); it('Should return an empty array if the domain object is empty', () => { - const domain = {} as OnyxEntry; + const domain = createMock>({}); expect(memberAccountIDsSelector(domain)).toEqual([]); }); it('Should return member IDs when keys start with the security group prefix', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention - '100': 'value', + '100': 'read', // eslint-disable-next-line @typescript-eslint/naming-convention - '200': 'value', + '200': 'read', }, }, [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention - '300': 'value', + '300': 'read', }, }, - } as unknown as OnyxEntry; + }); expect(memberAccountIDsSelector(domain).sort()).toEqual([100, 200, 300]); }); it('Should return unique member IDs if they appear in multiple security groups', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention - '123': 'value', + '123': 'read', }, }, [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention - '123': 'value', + '123': 'read', }, }, - } as unknown as OnyxEntry; + }); expect(memberAccountIDsSelector(domain)).toEqual([123]); }); it('Should ignore keys that do not start with the security group prefix', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention - '456': 'value', + '456': 'read', }, }, - someOtherKey: { - shared: { - // eslint-disable-next-line @typescript-eslint/naming-convention - '789': 'value', - }, - }, - } as unknown as OnyxEntry; + email: 'test@example.com', + }); expect(memberAccountIDsSelector(domain)).toEqual([456]); }); it('Should ignore groups that do not have a shared property', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: {}, + // @ts-expect-error -- a null shared value is a deliberately malformed runtime entry. [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: {shared: null}, [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}3`]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention - '111': 'value', + '111': 'read', }, }, - } as unknown as OnyxEntry; + }); expect(memberAccountIDsSelector(domain)).toEqual([111]); }); it('Should filter out members with null or undefined permission values', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention @@ -286,24 +285,24 @@ describe('domainSelectors', () => { '400': 'read', }, }, - } as unknown as OnyxEntry; + }); expect(memberAccountIDsSelector(domain).sort()).toEqual([100, 400]); }); it('Should filter out non-numeric shared keys', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention - '123': 'value', + '123': 'read', // eslint-disable-next-line @typescript-eslint/naming-convention - 'not-a-number': 'value', + 'not-a-number': 'read', // eslint-disable-next-line @typescript-eslint/naming-convention - '456': 'value', + '456': 'read', }, }, - } as unknown as OnyxEntry; + }); expect(memberAccountIDsSelector(domain).sort()).toEqual([123, 456]); }); @@ -311,10 +310,10 @@ describe('domainSelectors', () => { describe('defaultSecurityGroupIDSelector', () => { it('Should return the default security group ID when it exists', () => { - const domain = { + const domain = createMock>({ // eslint-disable-next-line @typescript-eslint/naming-convention domain_defaultSecurityGroupID: '12345', - } as unknown as OnyxEntry; + }); expect(defaultSecurityGroupIDSelector(domain)).toBe('12345'); }); @@ -324,7 +323,7 @@ describe('domainSelectors', () => { }); it('Should return undefined if the domain_defaultSecurityGroupID property is missing', () => { - const domain = {} as OnyxEntry; + const domain = createMock>({}); expect(defaultSecurityGroupIDSelector(domain)).toBeUndefined(); }); @@ -332,11 +331,11 @@ describe('domainSelectors', () => { describe('selectSecurityGroupForAccount', () => { it('Should return undefined when domain has no security groups', () => { - const domain = { + const domain = createMock({ validated: true, accountID: 1, email: 'test@example.com', - } as Domain; + }); const result = selectSecurityGroupForAccount(123)(domain); @@ -344,7 +343,7 @@ describe('domainSelectors', () => { }); it('Should return undefined when account is not in any security group', () => { - const securityGroup = { + const securityGroup = createMock({ enableRestrictedPrimaryLogin: false, enableRestrictedPolicyCreation: false, shared: { @@ -353,16 +352,16 @@ describe('domainSelectors', () => { // eslint-disable-next-line @typescript-eslint/naming-convention '789': 'read', }, - } as DomainSecurityGroup; + }); - const domain: Domain = { + const domain = createMock({ validated: true, accountID: 1, email: 'test@example.com', // eslint-disable-next-line @typescript-eslint/naming-convention domain_defaultSecurityGroupID: '1', [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: securityGroup, - } as unknown as Domain; + }); const result = selectSecurityGroupForAccount(123)(domain); @@ -371,13 +370,13 @@ describe('domainSelectors', () => { it('Should return the security group data when account belongs to a group', () => { /* eslint-disable @typescript-eslint/naming-convention */ - const group1 = {shared: {'123': 'read', '456': 'read'}, enableRestrictedPrimaryLogin: true, enableRestrictedPolicyCreation: true} as DomainSecurityGroup; - const group2 = {shared: {'789': 'read'}, enableRestrictedPrimaryLogin: true, enableRestrictedPolicyCreation: true} as DomainSecurityGroup; + const group1 = createMock({shared: {'123': 'read', '456': 'read'}, enableRestrictedPrimaryLogin: true, enableRestrictedPolicyCreation: true}); + const group2 = createMock({shared: {'789': 'read'}, enableRestrictedPrimaryLogin: true, enableRestrictedPolicyCreation: true}); const key1 = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`; const key2 = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`; - const domain: Domain = { + const domain = createMock({ validated: true, accountID: 1, email: 'test@example.com', @@ -385,7 +384,7 @@ describe('domainSelectors', () => { domain_defaultSecurityGroupID: '1', [key1]: group1, [key2]: group2, - } as unknown as Domain; + }); const result = selectSecurityGroupForAccount(123)(domain); @@ -402,13 +401,13 @@ describe('domainSelectors', () => { const key1 = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`; const key2 = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`; - const domain: Domain = { + const domain = createMock({ validated: true, accountID: 1, email: 'test@example.com', [key1]: {shared: {'123': null}, enableRestrictedPrimaryLogin: false, enableRestrictedPolicyCreation: false}, [key2]: {shared: {'123': 'read'}, enableRestrictedPrimaryLogin: false, enableRestrictedPolicyCreation: false}, - } as unknown as Domain; + }); const result = selectSecurityGroupForAccount(123)(domain); @@ -418,12 +417,12 @@ describe('domainSelectors', () => { it('Should return undefined when the only matching shared entry is a null tombstone', () => { const key1 = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`; - const domain: Domain = { + const domain = createMock({ validated: true, accountID: 1, email: 'test@example.com', [key1]: {shared: {'123': null}, enableRestrictedPrimaryLogin: false, enableRestrictedPolicyCreation: false}, - } as unknown as Domain; + }); expect(selectSecurityGroupForAccount(123)(domain)).toBeUndefined(); }); @@ -462,46 +461,46 @@ describe('domainSelectors', () => { }); it('Should return undefined when domainPendingActions is empty', () => { - const domainPendingActions = {} as OnyxEntry; + const domainPendingActions = createMock>({}); expect(domainSecurityGroupSettingPendingActionSelector('name', '1')(domainPendingActions)).toBeUndefined(); }); it('Should return undefined when groupID is undefined', () => { - const domainPendingActions = { + const domainPendingActions = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, - } as unknown as OnyxEntry; + }); expect(domainSecurityGroupSettingPendingActionSelector('name', undefined)(domainPendingActions)).toBeUndefined(); }); it('Should return undefined when the group key does not exist in domainPendingActions', () => { - const domainPendingActions = { + const domainPendingActions = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, - } as unknown as OnyxEntry; + }); expect(domainSecurityGroupSettingPendingActionSelector('name', '999')(domainPendingActions)).toBeUndefined(); }); it('Should return the pending action for the given groupID', () => { - const domainPendingActions = { + const domainPendingActions = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}42`]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, - } as unknown as OnyxEntry; + }); expect(domainSecurityGroupSettingPendingActionSelector('name', '42')(domainPendingActions)).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); }); it('Should return the correct pending action when multiple groups are present', () => { - const domainPendingActions = { + const domainPendingActions = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }, [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, - } as unknown as OnyxEntry; + }); expect(domainSecurityGroupSettingPendingActionSelector('name', '1')(domainPendingActions)).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); expect(domainSecurityGroupSettingPendingActionSelector('name', '2')(domainPendingActions)).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); }); @@ -513,49 +512,49 @@ describe('domainSelectors', () => { }); it('Should return undefined when domainErrors is empty', () => { - const domainErrors = {} as OnyxEntry; + const domainErrors = createMock>({}); expect(domainSecurityGroupSettingErrorsSelector('nameErrors', '1')(domainErrors)).toBeUndefined(); }); it('Should return undefined when groupID is undefined', () => { - const domainErrors = { + const domainErrors = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { nameErrors: {errorMessage: 'some error'}, }, - } as unknown as OnyxEntry; + }); expect(domainSecurityGroupSettingErrorsSelector('nameErrors', undefined)(domainErrors)).toBeUndefined(); }); it('Should return undefined when the group key does not exist in domainErrors', () => { - const domainErrors = { + const domainErrors = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { nameErrors: {errorMessage: 'some error'}, }, - } as unknown as OnyxEntry; + }); expect(domainSecurityGroupSettingErrorsSelector('nameErrors', '999')(domainErrors)).toBeUndefined(); }); it('Should return the errors for the given groupID', () => { const errors = {errorMessage: 'failed to update'}; - const domainErrors = { + const domainErrors = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}42`]: { nameErrors: errors, }, - } as unknown as OnyxEntry; + }); expect(domainSecurityGroupSettingErrorsSelector('nameErrors', '42')(domainErrors)).toEqual(errors); }); it('Should return the correct errors when multiple groups are present', () => { const errors1 = {errorMessage: 'error for group 1'}; const errors2 = {errorMessage: 'error for group 2'}; - const domainErrors = { + const domainErrors = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { nameErrors: errors1, }, [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: { nameErrors: errors2, }, - } as unknown as OnyxEntry; + }); expect(domainSecurityGroupSettingErrorsSelector('nameErrors', '1')(domainErrors)).toEqual(errors1); expect(domainSecurityGroupSettingErrorsSelector('nameErrors', '2')(domainErrors)).toEqual(errors2); }); @@ -567,66 +566,66 @@ describe('domainSelectors', () => { }); it('Should return false when domainPendingActions is empty', () => { - const domainPendingActions = {} as OnyxEntry; + const domainPendingActions = createMock>({}); expect(isSecurityGroupPendingDeleteSelector('1')(domainPendingActions)).toBe(false); }); it('Should return false when groupID is undefined', () => { - const domainPendingActions = { + const domainPendingActions = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, - } as unknown as OnyxEntry; + }); expect(isSecurityGroupPendingDeleteSelector(undefined)(domainPendingActions)).toBe(false); }); it('Should return false when the group key does not exist in domainPendingActions', () => { - const domainPendingActions = { + const domainPendingActions = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, - } as unknown as OnyxEntry; + }); expect(isSecurityGroupPendingDeleteSelector('999')(domainPendingActions)).toBe(false); }); it('Should return false when the group has only non-delete pending actions', () => { - const domainPendingActions = { + const domainPendingActions = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }, - } as unknown as OnyxEntry; + }); expect(isSecurityGroupPendingDeleteSelector('1')(domainPendingActions)).toBe(false); }); it('Should return true when the group has a top-level delete pending action', () => { - const domainPendingActions = { + const domainPendingActions = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, - } as unknown as OnyxEntry; + }); expect(isSecurityGroupPendingDeleteSelector('1')(domainPendingActions)).toBe(true); }); it('Should return true when at least one field-level pending action is delete', () => { - const domainPendingActions = { + const domainPendingActions = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, - } as unknown as OnyxEntry; + }); expect(isSecurityGroupPendingDeleteSelector('1')(domainPendingActions)).toBe(true); }); it('Should distinguish between groups when multiple are present', () => { - const domainPendingActions = { + const domainPendingActions = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, - } as unknown as OnyxEntry; + }); expect(isSecurityGroupPendingDeleteSelector('1')(domainPendingActions)).toBe(false); expect(isSecurityGroupPendingDeleteSelector('2')(domainPendingActions)).toBe(true); }); @@ -638,15 +637,15 @@ describe('domainSelectors', () => { }); it('Should return an empty array if the domain object is empty', () => { - const domain = {} as OnyxEntry; + const domain = createMock>({}); expect(groupsSelector(domain)).toEqual([]); }); it('Should return an array of groups when keys start with the security group prefix', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}123`]: {name: 'Group 1', shared: {}}, [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}456`]: {name: 'Group 2', shared: {}}, - } as unknown as OnyxEntry; + }); const expectedGroups = [ {id: '123', details: {name: 'Group 1', shared: {}}}, @@ -657,12 +656,13 @@ describe('domainSelectors', () => { }); it('Should ignore keys that do not start with the security group prefix', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}123`]: {name: 'Group 1', shared: {}}, + // @ts-expect-error -- a null shared value is a deliberately malformed runtime entry. [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}456`]: {name: 'Group 2', shared: null}, [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}789`]: {name: 'Group 3'}, - otherKey: 'value', - } as unknown as OnyxEntry; + email: 'other@example.com', + }); const expectedGroups = [{id: '123', details: {name: 'Group 1', shared: {}}}]; @@ -681,40 +681,40 @@ describe('domainSelectors', () => { creator: 'creator@example.com', }; - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX}${userID1}`]: vacationDelegate, - } as unknown as OnyxEntry; + }); const selector = vacationDelegateSelector(userID1); expect(selector(domain)).toEqual(vacationDelegate); }); it('Should return undefined if the vacation delegate for a specific accountID does not exist', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX}${userID2}`]: { delegate: 'other@example.com', }, - } as unknown as OnyxEntry; + }); const selector = vacationDelegateSelector(userID1); expect(selector(domain)).toBeUndefined(); }); it('Should return the vacation delegate when it exists but has no properties', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX}${userID1}`]: {}, - } as unknown as OnyxEntry; + }); const selector = vacationDelegateSelector(userID1); expect(selector(domain)).toEqual({}); }); it('Should return the vacation delegate when only some fields are present', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX}${userID1}`]: { delegate: 'delegate@example.com', }, - } as unknown as OnyxEntry; + }); const selector = vacationDelegateSelector(userID1); expect(selector(domain)).toEqual({ @@ -723,22 +723,18 @@ describe('domainSelectors', () => { }); it('Should ignore keys that do not start with the vacation delegate prefix', () => { - const domain = { - private_otherPrefix_123: { - delegate: 'wrong@example.com', - }, - } as unknown as OnyxEntry; + const domain = createMock>({domainValidationError: {delegate: 'wrong@example.com'}}); const selector = vacationDelegateSelector(userID1); expect(selector(domain)).toBeUndefined(); }); it('Should not be affected by other vacation delegate entries with different accountIDs', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX}${userID2}`]: { delegate: 'delegate@example.com', }, - } as unknown as OnyxEntry; + }); const selector = vacationDelegateSelector(userID1); expect(selector(domain)).toBeUndefined(); @@ -751,41 +747,42 @@ describe('domainSelectors', () => { }); it('Should return false if accountID is 0', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123456`]: userID1, - } as unknown as OnyxEntry; + }); expect(isAdminSelector(0)(domain)).toBe(false); }); it('Should return true if the accountID is found in admin permission entries', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123456`]: userID1, [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}789101`]: userID2, - } as unknown as OnyxEntry; + }); expect(isAdminSelector(userID1)(domain)).toBe(true); expect(isAdminSelector(userID2)(domain)).toBe(true); }); it('Should return false if the accountID is not in any admin permission entries', () => { - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123456`]: userID1, - } as unknown as OnyxEntry; + }); expect(isAdminSelector(999)(domain)).toBe(false); }); it('Should ignore null/undefined admin permission values', () => { - const domain = { + const domain = createMock>({ + // @ts-expect-error -- a null admin value is a deliberately malformed runtime entry. [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123456`]: null, [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}789101`]: undefined, - } as unknown as OnyxEntry; + }); expect(isAdminSelector(userID1)(domain)).toBe(false); }); it('Should return false for empty domain object', () => { - const domain = {} as OnyxEntry; + const domain = createMock>({}); expect(isAdminSelector(userID1)(domain)).toBe(false); }); }); @@ -793,25 +790,25 @@ describe('domainSelectors', () => { describe('accountLockSelector', () => { it('Should return lock state for the given account ID', () => { const accountID = 123; - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.PRIVATE_LOCKED_ACCOUNT_PREFIX}${accountID}`]: true, - } as unknown as OnyxEntry; + }); expect(accountLockSelector(accountID)(domain)).toBe(true); }); it('Should return false when the lock state is false', () => { const accountID = 123; - const domain = { + const domain = createMock>({ [`${CONST.DOMAIN.PRIVATE_LOCKED_ACCOUNT_PREFIX}${accountID}`]: false, - } as unknown as OnyxEntry; + }); expect(accountLockSelector(accountID)(domain)).toBe(false); }); it('Should return undefined when the domain object is undefined or account key does not exist', () => { const accountID = 123; - const domain = {} as OnyxEntry; + const domain = createMock>({}); expect(accountLockSelector(accountID)(undefined)).toBeUndefined(); expect(accountLockSelector(accountID)(domain)).toBeUndefined(); @@ -820,11 +817,11 @@ describe('domainSelectors', () => { describe('selectRestrictedPrimaryPolicyID', () => { const makeGroup = (enableRestrictedPrimaryPolicy: boolean, restrictedPrimaryPolicyID?: string): DomainSecurityGroup => - ({enableRestrictedPrimaryPolicy, restrictedPrimaryPolicyID, shared: {}}) as unknown as DomainSecurityGroup; + createMock({enableRestrictedPrimaryPolicy, restrictedPrimaryPolicyID, shared: {}}); const makeDomain = (groups: Record): OnyxEntry => { const entries = Object.fromEntries(Object.entries(groups).map(([id, g]) => [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}${id}`, g])); - return entries as unknown as Domain; + return createMock(entries); }; it('returns undefined when domain is undefined', () => { From 08f589e4e1327358532b62b9279e4c384edbf0ad Mon Sep 17 00:00:00 2001 From: KJ21-ENG <140263938+KJ21-ENG@users.noreply.github.com> Date: Sat, 25 Jul 2026 14:30:03 +0530 Subject: [PATCH 2/3] Repair DomainSelectorsTest typed fixtures --- tests/unit/DomainSelectorsTest.ts | 148 ++++++++++++++++-------------- 1 file changed, 80 insertions(+), 68 deletions(-) diff --git a/tests/unit/DomainSelectorsTest.ts b/tests/unit/DomainSelectorsTest.ts index 9e677d59c799..50c4246e829f 100644 --- a/tests/unit/DomainSelectorsTest.ts +++ b/tests/unit/DomainSelectorsTest.ts @@ -26,6 +26,8 @@ import { import createMock from '../utils/createMock'; +const prefixedKey = (prefix: TPrefix, suffix: string): `${TPrefix}${string}` => `${prefix}${suffix}`; + describe('domainSelectors', () => { const userID1 = 123; const userID2 = 456; @@ -36,8 +38,8 @@ describe('domainSelectors', () => { it('Should return an array of admin IDs when keys start with the admin access prefix', () => { const domain = createMock>({ - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123`]: 321, - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}321`]: 123, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '123')]: 321, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '321')]: 123, }); expect(adminAccountIDsSelector(domain)).toEqual([321, 123]); @@ -45,7 +47,7 @@ describe('domainSelectors', () => { it('Should ignore keys that do not start with the admin access prefix', () => { const domain = createMock>({ - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123`]: 321, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '123')]: 321, email: 'test@example.com', }); @@ -54,10 +56,10 @@ describe('domainSelectors', () => { it('Should ignore keys with falsy values even if they have the correct prefix', () => { const domain = createMock>({ - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123`]: 123, - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}0`]: undefined, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '123')]: 123, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '0')]: undefined, // @ts-expect-error -- a null admin value is a deliberately malformed runtime entry. - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}999`]: null, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '999')]: null, }); expect(adminAccountIDsSelector(domain)).toEqual([123]); @@ -203,7 +205,7 @@ describe('domainSelectors', () => { it('Should return member IDs when keys start with the security group prefix', () => { const domain = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention '100': 'read', @@ -211,7 +213,7 @@ describe('domainSelectors', () => { '200': 'read', }, }, - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '2')]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention '300': 'read', @@ -224,13 +226,13 @@ describe('domainSelectors', () => { it('Should return unique member IDs if they appear in multiple security groups', () => { const domain = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention '123': 'read', }, }, - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '2')]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention '123': 'read', @@ -243,13 +245,19 @@ describe('domainSelectors', () => { it('Should ignore keys that do not start with the security group prefix', () => { const domain = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention '456': 'read', }, }, - email: 'test@example.com', + // @ts-expect-error -- a non-prefix group-shaped entry is deliberately malformed runtime input. + someOtherKey: { + shared: { + // eslint-disable-next-line @typescript-eslint/naming-convention + '789': 'read', + }, + }, }); expect(memberAccountIDsSelector(domain)).toEqual([456]); @@ -257,10 +265,10 @@ describe('domainSelectors', () => { it('Should ignore groups that do not have a shared property', () => { const domain = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: {}, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: {}, // @ts-expect-error -- a null shared value is a deliberately malformed runtime entry. - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: {shared: null}, - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}3`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '2')]: {shared: null}, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '3')]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention '111': 'read', @@ -273,7 +281,7 @@ describe('domainSelectors', () => { it('Should filter out members with null or undefined permission values', () => { const domain = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention '100': 'read', @@ -292,7 +300,7 @@ describe('domainSelectors', () => { it('Should filter out non-numeric shared keys', () => { const domain = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { shared: { // eslint-disable-next-line @typescript-eslint/naming-convention '123': 'read', @@ -360,7 +368,7 @@ describe('domainSelectors', () => { email: 'test@example.com', // eslint-disable-next-line @typescript-eslint/naming-convention domain_defaultSecurityGroupID: '1', - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: securityGroup, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: securityGroup, }); const result = selectSecurityGroupForAccount(123)(domain); @@ -373,8 +381,8 @@ describe('domainSelectors', () => { const group1 = createMock({shared: {'123': 'read', '456': 'read'}, enableRestrictedPrimaryLogin: true, enableRestrictedPolicyCreation: true}); const group2 = createMock({shared: {'789': 'read'}, enableRestrictedPrimaryLogin: true, enableRestrictedPolicyCreation: true}); - const key1 = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`; - const key2 = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`; + const key1 = prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1'); + const key2 = prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '2'); const domain = createMock({ validated: true, @@ -398,8 +406,8 @@ describe('domainSelectors', () => { // After changeDomainSecurityGroup fires optimistically, the old group gets // shared[accountID] = null while the new group gets shared[accountID] = 'read'. // The selector must skip the tombstone and return the new (active) group. - const key1 = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`; - const key2 = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`; + const key1 = prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1'); + const key2 = prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '2'); const domain = createMock({ validated: true, @@ -415,7 +423,7 @@ describe('domainSelectors', () => { }); it('Should return undefined when the only matching shared entry is a null tombstone', () => { - const key1 = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`; + const key1 = prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1'); const domain = createMock({ validated: true, @@ -430,7 +438,7 @@ describe('domainSelectors', () => { describe('isSecurityGroupEntry', () => { it('should return true for a valid security group entry', () => { - const entry: [string, unknown] = [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}123`, {shared: {}}]; + const entry: [string, unknown] = [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '123'), {shared: {}}]; expect(isSecurityGroupEntry(entry)).toBe(true); }); @@ -440,17 +448,17 @@ describe('domainSelectors', () => { }); it('should return false if the value is not an object', () => { - const entry: [string, unknown] = [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}123`, 'not an object']; + const entry: [string, unknown] = [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '123'), 'not an object']; expect(isSecurityGroupEntry(entry)).toBe(false); }); it('should return false if the value is null', () => { - const entry: [string, unknown] = [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}123`, null]; + const entry: [string, unknown] = [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '123'), null]; expect(isSecurityGroupEntry(entry)).toBe(false); }); it('should return false if the value does not have a "shared" property', () => { - const entry: [string, unknown] = [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}123`, {other: {}}]; + const entry: [string, unknown] = [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '123'), {other: {}}]; expect(isSecurityGroupEntry(entry)).toBe(false); }); }); @@ -467,7 +475,7 @@ describe('domainSelectors', () => { it('Should return undefined when groupID is undefined', () => { const domainPendingActions = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, }); @@ -476,7 +484,7 @@ describe('domainSelectors', () => { it('Should return undefined when the group key does not exist in domainPendingActions', () => { const domainPendingActions = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, }); @@ -485,7 +493,7 @@ describe('domainSelectors', () => { it('Should return the pending action for the given groupID', () => { const domainPendingActions = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}42`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '42')]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, }); @@ -494,10 +502,10 @@ describe('domainSelectors', () => { it('Should return the correct pending action when multiple groups are present', () => { const domainPendingActions = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }, - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '2')]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, }); @@ -518,7 +526,7 @@ describe('domainSelectors', () => { it('Should return undefined when groupID is undefined', () => { const domainErrors = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { nameErrors: {errorMessage: 'some error'}, }, }); @@ -527,7 +535,7 @@ describe('domainSelectors', () => { it('Should return undefined when the group key does not exist in domainErrors', () => { const domainErrors = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { nameErrors: {errorMessage: 'some error'}, }, }); @@ -537,7 +545,7 @@ describe('domainSelectors', () => { it('Should return the errors for the given groupID', () => { const errors = {errorMessage: 'failed to update'}; const domainErrors = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}42`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '42')]: { nameErrors: errors, }, }); @@ -548,10 +556,10 @@ describe('domainSelectors', () => { const errors1 = {errorMessage: 'error for group 1'}; const errors2 = {errorMessage: 'error for group 2'}; const domainErrors = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { nameErrors: errors1, }, - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '2')]: { nameErrors: errors2, }, }); @@ -572,7 +580,7 @@ describe('domainSelectors', () => { it('Should return false when groupID is undefined', () => { const domainPendingActions = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, }); @@ -581,7 +589,7 @@ describe('domainSelectors', () => { it('Should return false when the group key does not exist in domainPendingActions', () => { const domainPendingActions = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, }); @@ -590,9 +598,9 @@ describe('domainSelectors', () => { it('Should return false when the group has only non-delete pending actions', () => { const domainPendingActions = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + deleteGroup: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }, }); expect(isSecurityGroupPendingDeleteSelector('1')(domainPendingActions)).toBe(false); @@ -600,8 +608,8 @@ describe('domainSelectors', () => { it('Should return true when the group has a top-level delete pending action', () => { const domainPendingActions = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { + deleteGroup: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, }); expect(isSecurityGroupPendingDeleteSelector('1')(domainPendingActions)).toBe(true); @@ -609,9 +617,9 @@ describe('domainSelectors', () => { it('Should return true when at least one field-level pending action is delete', () => { const domainPendingActions = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { - name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { + name: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + enableRestrictedPrimaryPolicy: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, }); expect(isSecurityGroupPendingDeleteSelector('1')(domainPendingActions)).toBe(true); @@ -619,11 +627,11 @@ describe('domainSelectors', () => { it('Should distinguish between groups when multiple are present', () => { const domainPendingActions = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}1`]: { + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '1')]: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}2`]: { - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '2')]: { + deleteGroup: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, }); expect(isSecurityGroupPendingDeleteSelector('1')(domainPendingActions)).toBe(false); @@ -643,8 +651,8 @@ describe('domainSelectors', () => { it('Should return an array of groups when keys start with the security group prefix', () => { const domain = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}123`]: {name: 'Group 1', shared: {}}, - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}456`]: {name: 'Group 2', shared: {}}, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '123')]: {name: 'Group 1', shared: {}}, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '456')]: {name: 'Group 2', shared: {}}, }); const expectedGroups = [ @@ -657,10 +665,10 @@ describe('domainSelectors', () => { it('Should ignore keys that do not start with the security group prefix', () => { const domain = createMock>({ - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}123`]: {name: 'Group 1', shared: {}}, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '123')]: {name: 'Group 1', shared: {}}, // @ts-expect-error -- a null shared value is a deliberately malformed runtime entry. - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}456`]: {name: 'Group 2', shared: null}, - [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}789`]: {name: 'Group 3'}, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '456')]: {name: 'Group 2', shared: null}, + [prefixedKey(CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX, '789')]: {name: 'Group 3'}, email: 'other@example.com', }); @@ -682,7 +690,7 @@ describe('domainSelectors', () => { }; const domain = createMock>({ - [`${CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX}${userID1}`]: vacationDelegate, + [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, String(userID1))]: vacationDelegate, }); const selector = vacationDelegateSelector(userID1); @@ -691,7 +699,7 @@ describe('domainSelectors', () => { it('Should return undefined if the vacation delegate for a specific accountID does not exist', () => { const domain = createMock>({ - [`${CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX}${userID2}`]: { + [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, String(userID2))]: { delegate: 'other@example.com', }, }); @@ -702,7 +710,7 @@ describe('domainSelectors', () => { it('Should return the vacation delegate when it exists but has no properties', () => { const domain = createMock>({ - [`${CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX}${userID1}`]: {}, + [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, String(userID1))]: {}, }); const selector = vacationDelegateSelector(userID1); @@ -711,7 +719,7 @@ describe('domainSelectors', () => { it('Should return the vacation delegate when only some fields are present', () => { const domain = createMock>({ - [`${CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX}${userID1}`]: { + [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, String(userID1))]: { delegate: 'delegate@example.com', }, }); @@ -731,7 +739,7 @@ describe('domainSelectors', () => { it('Should not be affected by other vacation delegate entries with different accountIDs', () => { const domain = createMock>({ - [`${CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX}${userID2}`]: { + [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, String(userID2))]: { delegate: 'delegate@example.com', }, }); @@ -748,15 +756,15 @@ describe('domainSelectors', () => { it('Should return false if accountID is 0', () => { const domain = createMock>({ - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123456`]: userID1, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '123456')]: userID1, }); expect(isAdminSelector(0)(domain)).toBe(false); }); it('Should return true if the accountID is found in admin permission entries', () => { const domain = createMock>({ - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123456`]: userID1, - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}789101`]: userID2, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '123456')]: userID1, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '789101')]: userID2, }); expect(isAdminSelector(userID1)(domain)).toBe(true); @@ -765,7 +773,7 @@ describe('domainSelectors', () => { it('Should return false if the accountID is not in any admin permission entries', () => { const domain = createMock>({ - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123456`]: userID1, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '123456')]: userID1, }); expect(isAdminSelector(999)(domain)).toBe(false); @@ -774,8 +782,8 @@ describe('domainSelectors', () => { it('Should ignore null/undefined admin permission values', () => { const domain = createMock>({ // @ts-expect-error -- a null admin value is a deliberately malformed runtime entry. - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}123456`]: null, - [`${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}789101`]: undefined, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '123456')]: null, + [prefixedKey(CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX, '789101')]: undefined, }); expect(isAdminSelector(userID1)(domain)).toBe(false); @@ -791,7 +799,7 @@ describe('domainSelectors', () => { it('Should return lock state for the given account ID', () => { const accountID = 123; const domain = createMock>({ - [`${CONST.DOMAIN.PRIVATE_LOCKED_ACCOUNT_PREFIX}${accountID}`]: true, + [prefixedKey(CONST.DOMAIN.PRIVATE_LOCKED_ACCOUNT_PREFIX, String(accountID))]: true, }); expect(accountLockSelector(accountID)(domain)).toBe(true); @@ -800,7 +808,7 @@ describe('domainSelectors', () => { it('Should return false when the lock state is false', () => { const accountID = 123; const domain = createMock>({ - [`${CONST.DOMAIN.PRIVATE_LOCKED_ACCOUNT_PREFIX}${accountID}`]: false, + [prefixedKey(CONST.DOMAIN.PRIVATE_LOCKED_ACCOUNT_PREFIX, String(accountID))]: false, }); expect(accountLockSelector(accountID)(domain)).toBe(false); @@ -820,7 +828,11 @@ describe('domainSelectors', () => { createMock({enableRestrictedPrimaryPolicy, restrictedPrimaryPolicyID, shared: {}}); const makeDomain = (groups: Record): OnyxEntry => { - const entries = Object.fromEntries(Object.entries(groups).map(([id, g]) => [`${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}${id}`, g])); + const entries: Partial = {}; + for (const [id, group] of Object.entries(groups)) { + const key: `${typeof CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}${string}` = `${CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX}${id}`; + entries[key] = group; + } return createMock(entries); }; From 5f20d0d4c2e95480585b70977bbb6187a4245b85 Mon Sep 17 00:00:00 2001 From: KJ21-ENG <140263938+KJ21-ENG@users.noreply.github.com> Date: Sat, 25 Jul 2026 16:51:43 +0530 Subject: [PATCH 3/3] Repair DomainSelectorsTest literal key typing --- tests/unit/DomainSelectorsTest.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/unit/DomainSelectorsTest.ts b/tests/unit/DomainSelectorsTest.ts index 50c4246e829f..a05f4e86848d 100644 --- a/tests/unit/DomainSelectorsTest.ts +++ b/tests/unit/DomainSelectorsTest.ts @@ -26,7 +26,7 @@ import { import createMock from '../utils/createMock'; -const prefixedKey = (prefix: TPrefix, suffix: string): `${TPrefix}${string}` => `${prefix}${suffix}`; +const prefixedKey = (prefix: TPrefix, suffix: TSuffix): `${TPrefix}${TSuffix}` => `${prefix}${suffix}`; describe('domainSelectors', () => { const userID1 = 123; @@ -690,7 +690,7 @@ describe('domainSelectors', () => { }; const domain = createMock>({ - [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, String(userID1))]: vacationDelegate, + [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, userID1)]: vacationDelegate, }); const selector = vacationDelegateSelector(userID1); @@ -699,7 +699,7 @@ describe('domainSelectors', () => { it('Should return undefined if the vacation delegate for a specific accountID does not exist', () => { const domain = createMock>({ - [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, String(userID2))]: { + [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, userID2)]: { delegate: 'other@example.com', }, }); @@ -710,7 +710,7 @@ describe('domainSelectors', () => { it('Should return the vacation delegate when it exists but has no properties', () => { const domain = createMock>({ - [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, String(userID1))]: {}, + [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, userID1)]: {}, }); const selector = vacationDelegateSelector(userID1); @@ -719,7 +719,7 @@ describe('domainSelectors', () => { it('Should return the vacation delegate when only some fields are present', () => { const domain = createMock>({ - [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, String(userID1))]: { + [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, userID1)]: { delegate: 'delegate@example.com', }, }); @@ -739,7 +739,7 @@ describe('domainSelectors', () => { it('Should not be affected by other vacation delegate entries with different accountIDs', () => { const domain = createMock>({ - [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, String(userID2))]: { + [prefixedKey(CONST.DOMAIN.PRIVATE_VACATION_DELEGATE_PREFIX, userID2)]: { delegate: 'delegate@example.com', }, }); @@ -799,7 +799,7 @@ describe('domainSelectors', () => { it('Should return lock state for the given account ID', () => { const accountID = 123; const domain = createMock>({ - [prefixedKey(CONST.DOMAIN.PRIVATE_LOCKED_ACCOUNT_PREFIX, String(accountID))]: true, + [prefixedKey(CONST.DOMAIN.PRIVATE_LOCKED_ACCOUNT_PREFIX, accountID)]: true, }); expect(accountLockSelector(accountID)(domain)).toBe(true); @@ -808,7 +808,7 @@ describe('domainSelectors', () => { it('Should return false when the lock state is false', () => { const accountID = 123; const domain = createMock>({ - [prefixedKey(CONST.DOMAIN.PRIVATE_LOCKED_ACCOUNT_PREFIX, String(accountID))]: false, + [prefixedKey(CONST.DOMAIN.PRIVATE_LOCKED_ACCOUNT_PREFIX, accountID)]: false, }); expect(accountLockSelector(accountID)(domain)).toBe(false);