|
| 1 | +// eslint-disable-next-line no-restricted-syntax |
| 2 | +import * as PolicyUtils from '@libs/PolicyUtils'; |
| 3 | +import {isQuickActionAllowed} from '@libs/QuickActionUtils'; |
| 4 | +import CONST from '@src/CONST'; |
| 5 | +import type {Policy} from '@src/types/onyx'; |
| 6 | + |
| 7 | +// Mock the PolicyUtils module |
| 8 | +jest.mock('@libs/PolicyUtils'); |
| 9 | + |
| 10 | +const mockedPolicyUtils = PolicyUtils as jest.Mocked<typeof PolicyUtils>; |
| 11 | + |
| 12 | +describe('QuickActionUtils', () => { |
| 13 | + describe('isQuickActionAllowed', () => { |
| 14 | + describe('CREATE_REPORT action', () => { |
| 15 | + const createReportAction = { |
| 16 | + action: CONST.QUICK_ACTIONS.CREATE_REPORT, |
| 17 | + targetAccountID: 123, |
| 18 | + isFirstQuickAction: false, |
| 19 | + }; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should return true when shouldShowPolicy returns true and isPolicyExpenseChatEnabled is true', () => { |
| 26 | + const policy: Partial<Policy> = { |
| 27 | + id: 'policy123', |
| 28 | + isPolicyExpenseChatEnabled: true, |
| 29 | + }; |
| 30 | + |
| 31 | + mockedPolicyUtils.shouldShowPolicy.mockReturnValue(true); |
| 32 | + |
| 33 | + const result = isQuickActionAllowed(createReportAction, undefined, policy as Policy); |
| 34 | + |
| 35 | + expect(result).toBe(true); |
| 36 | + expect(mockedPolicyUtils.shouldShowPolicy).toHaveBeenCalledWith(policy, false, undefined); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return false when shouldShowPolicy returns false even if isPolicyExpenseChatEnabled is true', () => { |
| 40 | + const policy: Partial<Policy> = { |
| 41 | + id: 'policy123', |
| 42 | + isPolicyExpenseChatEnabled: true, |
| 43 | + }; |
| 44 | + |
| 45 | + mockedPolicyUtils.shouldShowPolicy.mockReturnValue(false); |
| 46 | + |
| 47 | + const result = isQuickActionAllowed(createReportAction, undefined, policy as Policy); |
| 48 | + |
| 49 | + expect(result).toBe(false); |
| 50 | + expect(mockedPolicyUtils.shouldShowPolicy).toHaveBeenCalledWith(policy, false, undefined); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return false when shouldShowPolicy returns true but isPolicyExpenseChatEnabled is false', () => { |
| 54 | + const policy: Partial<Policy> = { |
| 55 | + id: 'policy123', |
| 56 | + isPolicyExpenseChatEnabled: false, |
| 57 | + }; |
| 58 | + |
| 59 | + mockedPolicyUtils.shouldShowPolicy.mockReturnValue(true); |
| 60 | + |
| 61 | + const result = isQuickActionAllowed(createReportAction, undefined, policy as Policy); |
| 62 | + |
| 63 | + expect(result).toBe(false); |
| 64 | + expect(mockedPolicyUtils.shouldShowPolicy).toHaveBeenCalledWith(policy, false, undefined); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return false when shouldShowPolicy returns true but isPolicyExpenseChatEnabled is undefined', () => { |
| 68 | + const policy: Partial<Policy> = { |
| 69 | + id: 'policy123', |
| 70 | + // isPolicyExpenseChatEnabled is undefined |
| 71 | + }; |
| 72 | + |
| 73 | + mockedPolicyUtils.shouldShowPolicy.mockReturnValue(true); |
| 74 | + |
| 75 | + const result = isQuickActionAllowed(createReportAction, undefined, policy as Policy); |
| 76 | + |
| 77 | + expect(result).toBe(false); |
| 78 | + expect(mockedPolicyUtils.shouldShowPolicy).toHaveBeenCalledWith(policy, false, undefined); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should return false when policy is undefined', () => { |
| 82 | + mockedPolicyUtils.shouldShowPolicy.mockReturnValue(false); |
| 83 | + |
| 84 | + const result = isQuickActionAllowed(createReportAction, undefined, undefined); |
| 85 | + |
| 86 | + expect(result).toBe(false); |
| 87 | + expect(mockedPolicyUtils.shouldShowPolicy).toHaveBeenCalledWith(undefined, false, undefined); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should return false when both conditions are false', () => { |
| 91 | + const policy: Partial<Policy> = { |
| 92 | + id: 'policy123', |
| 93 | + isPolicyExpenseChatEnabled: false, |
| 94 | + }; |
| 95 | + |
| 96 | + mockedPolicyUtils.shouldShowPolicy.mockReturnValue(false); |
| 97 | + |
| 98 | + const result = isQuickActionAllowed(createReportAction, undefined, policy as Policy); |
| 99 | + |
| 100 | + expect(result).toBe(false); |
| 101 | + expect(mockedPolicyUtils.shouldShowPolicy).toHaveBeenCalledWith(policy, false, undefined); |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments