|
| 1 | +import Onyx from 'react-native-onyx'; |
| 2 | +import type {OnyxEntry} from 'react-native-onyx'; |
| 3 | +import {shouldUseUpdateNetSuiteTokens} from '@libs/actions/connections'; |
| 4 | +import {connectPolicyToNetSuite, updateNetSuiteTokens} from '@libs/actions/connections/NetSuiteCommands'; |
| 5 | +// eslint-disable-next-line no-restricted-syntax -- this is required to allow mocking |
| 6 | +import * as API from '@libs/API'; |
| 7 | +import type {WriteCommand} from '@libs/API/types'; |
| 8 | +import {WRITE_COMMANDS} from '@libs/API/types'; |
| 9 | +import CONST from '@src/CONST'; |
| 10 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 11 | +import type {Policy} from '@src/types/onyx'; |
| 12 | +import type {AnyOnyxData} from '@src/types/onyx/Request'; |
| 13 | +import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates'; |
| 14 | + |
| 15 | +jest.mock('@libs/API'); |
| 16 | + |
| 17 | +const writeSpy = jest.spyOn(API, 'write'); |
| 18 | + |
| 19 | +const MOCK_POLICY_ID = 'MOCK_POLICY_ID'; |
| 20 | +const MOCK_CREDENTIALS = { |
| 21 | + netSuiteAccountID: 'account-123', |
| 22 | + netSuiteTokenID: 'token-123', |
| 23 | + netSuiteTokenSecret: 'secret-123', |
| 24 | +}; |
| 25 | + |
| 26 | +function getFirstWriteCall(): {command: WriteCommand; onyxData?: AnyOnyxData} { |
| 27 | + const call = writeSpy.mock.calls.at(0); |
| 28 | + if (!call) { |
| 29 | + throw new Error('API.write was not called'); |
| 30 | + } |
| 31 | + const [command, , onyxData] = call; |
| 32 | + return {command, onyxData}; |
| 33 | +} |
| 34 | + |
| 35 | +function createPolicy(options: {isAuthError?: boolean; verified?: boolean}): OnyxEntry<Policy> { |
| 36 | + return { |
| 37 | + id: MOCK_POLICY_ID, |
| 38 | + connections: { |
| 39 | + netsuite: { |
| 40 | + verified: options.verified ?? false, |
| 41 | + lastSync: { |
| 42 | + isAuthenticationError: options.isAuthError ?? false, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } as unknown as Policy; |
| 47 | +} |
| 48 | + |
| 49 | +describe('actions/connections/NetSuite', () => { |
| 50 | + beforeAll(() => { |
| 51 | + Onyx.init({ |
| 52 | + keys: ONYXKEYS, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + jest.clearAllMocks(); |
| 58 | + return Onyx.clear().then(waitForBatchedUpdates); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('connectPolicyToNetSuite', () => { |
| 62 | + it('writes the ConnectPolicyToNetSuite command', () => { |
| 63 | + connectPolicyToNetSuite(MOCK_POLICY_ID, MOCK_CREDENTIALS); |
| 64 | + |
| 65 | + const {command} = getFirstWriteCall(); |
| 66 | + expect(command).toBe(WRITE_COMMANDS.CONNECT_POLICY_TO_NETSUITE); |
| 67 | + }); |
| 68 | + |
| 69 | + it('passes the policyID and credentials as parameters', () => { |
| 70 | + connectPolicyToNetSuite(MOCK_POLICY_ID, MOCK_CREDENTIALS); |
| 71 | + |
| 72 | + const call = writeSpy.mock.calls.at(0); |
| 73 | + expect(call).toBeDefined(); |
| 74 | + const params = call?.[1]; |
| 75 | + expect(params).toEqual({ |
| 76 | + policyID: MOCK_POLICY_ID, |
| 77 | + ...MOCK_CREDENTIALS, |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('sets optimistic sync progress data', () => { |
| 82 | + connectPolicyToNetSuite(MOCK_POLICY_ID, MOCK_CREDENTIALS); |
| 83 | + |
| 84 | + const {onyxData} = getFirstWriteCall(); |
| 85 | + const optimisticUpdate = onyxData?.optimisticData?.at(0); |
| 86 | + |
| 87 | + expect(optimisticUpdate?.onyxMethod).toBe(Onyx.METHOD.MERGE); |
| 88 | + expect(optimisticUpdate?.key).toBe(`${ONYXKEYS.COLLECTION.POLICY_CONNECTION_SYNC_PROGRESS}${MOCK_POLICY_ID}`); |
| 89 | + expect(optimisticUpdate?.value).toEqual( |
| 90 | + expect.objectContaining({ |
| 91 | + stageInProgress: CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.NETSUITE_SYNC_CONNECTION, |
| 92 | + connectionName: CONST.POLICY.CONNECTIONS.NAME.NETSUITE, |
| 93 | + }), |
| 94 | + ); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('updateNetSuiteTokens', () => { |
| 99 | + it('writes the UpdateNetSuiteTokens command', () => { |
| 100 | + updateNetSuiteTokens(MOCK_POLICY_ID, MOCK_CREDENTIALS); |
| 101 | + |
| 102 | + const {command} = getFirstWriteCall(); |
| 103 | + expect(command).toBe(WRITE_COMMANDS.UPDATE_NETSUITE_TOKENS); |
| 104 | + }); |
| 105 | + |
| 106 | + it('passes the policyID and credentials as parameters', () => { |
| 107 | + updateNetSuiteTokens(MOCK_POLICY_ID, MOCK_CREDENTIALS); |
| 108 | + |
| 109 | + const call = writeSpy.mock.calls.at(0); |
| 110 | + expect(call).toBeDefined(); |
| 111 | + const params = call?.[1]; |
| 112 | + expect(params).toEqual({ |
| 113 | + policyID: MOCK_POLICY_ID, |
| 114 | + ...MOCK_CREDENTIALS, |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('sets optimistic sync progress data', () => { |
| 119 | + updateNetSuiteTokens(MOCK_POLICY_ID, MOCK_CREDENTIALS); |
| 120 | + |
| 121 | + const {onyxData} = getFirstWriteCall(); |
| 122 | + const optimisticUpdate = onyxData?.optimisticData?.at(0); |
| 123 | + |
| 124 | + expect(optimisticUpdate?.onyxMethod).toBe(Onyx.METHOD.MERGE); |
| 125 | + expect(optimisticUpdate?.key).toBe(`${ONYXKEYS.COLLECTION.POLICY_CONNECTION_SYNC_PROGRESS}${MOCK_POLICY_ID}`); |
| 126 | + expect(optimisticUpdate?.value).toEqual( |
| 127 | + expect.objectContaining({ |
| 128 | + stageInProgress: CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.NETSUITE_SYNC_CONNECTION, |
| 129 | + connectionName: CONST.POLICY.CONNECTIONS.NAME.NETSUITE, |
| 130 | + }), |
| 131 | + ); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('shouldUseUpdateNetSuiteTokens', () => { |
| 136 | + it('returns false for unverified connection with auth error (regression case)', () => { |
| 137 | + const policy = createPolicy({isAuthError: true, verified: false}); |
| 138 | + expect(shouldUseUpdateNetSuiteTokens(policy)).toBe(false); |
| 139 | + }); |
| 140 | + |
| 141 | + it('returns true for verified connection with auth error', () => { |
| 142 | + const policy = createPolicy({isAuthError: true, verified: true}); |
| 143 | + expect(shouldUseUpdateNetSuiteTokens(policy)).toBe(true); |
| 144 | + }); |
| 145 | + |
| 146 | + it('returns false when there is no auth error', () => { |
| 147 | + const policy = createPolicy({isAuthError: false, verified: true}); |
| 148 | + expect(shouldUseUpdateNetSuiteTokens(policy)).toBe(false); |
| 149 | + }); |
| 150 | + |
| 151 | + it('returns false when policy is undefined', () => { |
| 152 | + expect(shouldUseUpdateNetSuiteTokens(undefined)).toBe(false); |
| 153 | + }); |
| 154 | + |
| 155 | + it('returns false for unverified connection without auth error', () => { |
| 156 | + const policy = createPolicy({isAuthError: false, verified: false}); |
| 157 | + expect(shouldUseUpdateNetSuiteTokens(policy)).toBe(false); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments