@@ -11,12 +11,16 @@ import {
1111 setMoneyRequestTaxAmount ,
1212 setMoneyRequestTaxRate ,
1313} from '@libs/actions/IOU' ;
14+ import { resetSplitShares , setSplitShares } from '@libs/actions/IOU/Split' ;
1415import ONYXKEYS from '@src/ONYXKEYS' ;
16+ import type Transaction from '@src/types/onyx/Transaction' ;
1517import createRandomTransaction from '../../utils/collections/transaction' ;
1618import getOnyxValue from '../../utils/getOnyxValue' ;
1719import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates' ;
1820
1921const TRANSACTION_ID = 'test-txn-1' ;
22+ const USER_ACCOUNT_ID = 1 ;
23+ const PARTICIPANT_ACCOUNT_ID = 2 ;
2024
2125describe ( 'IOU setter functions' , ( ) => {
2226 beforeAll ( ( ) => {
@@ -238,4 +242,123 @@ describe('IOU setter functions', () => {
238242 expect ( draft ?. reimbursable ) . toBe ( false ) ;
239243 } ) ;
240244 } ) ;
245+
246+ describe ( 'setSplitShares' , ( ) => {
247+ function createTransactionWithSplitShares ( ) : Transaction {
248+ const transaction = createRandomTransaction ( 1 ) ;
249+ transaction . transactionID = TRANSACTION_ID ;
250+ transaction . amount = 10000 ;
251+ transaction . currency = 'USD' ;
252+ transaction . splitShares = {
253+ [ USER_ACCOUNT_ID ] : { amount : 5000 , isModified : false } ,
254+ [ PARTICIPANT_ACCOUNT_ID ] : { amount : 5000 , isModified : false } ,
255+ } ;
256+ return transaction ;
257+ }
258+
259+ beforeEach ( async ( ) => {
260+ await Onyx . set ( ONYXKEYS . SESSION , { accountID : USER_ACCOUNT_ID } ) ;
261+ await waitForBatchedUpdates ( ) ;
262+ } ) ;
263+
264+ it ( 'should write splitShares to TRANSACTION_DRAFT by default' , async ( ) => {
265+ const transaction = createTransactionWithSplitShares ( ) ;
266+ await Onyx . set ( `${ ONYXKEYS . COLLECTION . TRANSACTION_DRAFT } ${ TRANSACTION_ID } ` , transaction ) ;
267+ await waitForBatchedUpdates ( ) ;
268+
269+ setSplitShares ( transaction , 10000 , 'USD' , [ USER_ACCOUNT_ID , PARTICIPANT_ACCOUNT_ID ] ) ;
270+ await waitForBatchedUpdates ( ) ;
271+
272+ const draft = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . TRANSACTION_DRAFT } ${ TRANSACTION_ID } ` ) ;
273+ expect ( draft ?. splitShares ) . toBeDefined ( ) ;
274+ expect ( draft ?. splitShares ?. [ USER_ACCOUNT_ID ] ) . toBeDefined ( ) ;
275+ expect ( draft ?. splitShares ?. [ PARTICIPANT_ACCOUNT_ID ] ) . toBeDefined ( ) ;
276+ } ) ;
277+
278+ it ( 'should write splitShares to TRANSACTION when isDraft is false' , async ( ) => {
279+ const transaction = createTransactionWithSplitShares ( ) ;
280+ await Onyx . set ( `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ TRANSACTION_ID } ` , transaction ) ;
281+ await waitForBatchedUpdates ( ) ;
282+
283+ setSplitShares ( transaction , 10000 , 'USD' , [ USER_ACCOUNT_ID , PARTICIPANT_ACCOUNT_ID ] , false ) ;
284+ await waitForBatchedUpdates ( ) ;
285+
286+ const updated = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ TRANSACTION_ID } ` ) ;
287+ expect ( updated ?. splitShares ) . toBeDefined ( ) ;
288+ expect ( updated ?. splitShares ?. [ USER_ACCOUNT_ID ] ) . toBeDefined ( ) ;
289+ expect ( updated ?. splitShares ?. [ PARTICIPANT_ACCOUNT_ID ] ) . toBeDefined ( ) ;
290+ } ) ;
291+
292+ it ( 'should not create an orphaned TRANSACTION_DRAFT entry when isDraft is false' , async ( ) => {
293+ const transaction = createTransactionWithSplitShares ( ) ;
294+ await Onyx . set ( `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ TRANSACTION_ID } ` , transaction ) ;
295+ await waitForBatchedUpdates ( ) ;
296+
297+ setSplitShares ( transaction , 8000 , 'USD' , [ USER_ACCOUNT_ID , PARTICIPANT_ACCOUNT_ID ] , false ) ;
298+ await waitForBatchedUpdates ( ) ;
299+
300+ const draft = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . TRANSACTION_DRAFT } ${ TRANSACTION_ID } ` ) ;
301+ expect ( draft ) . toBeUndefined ( ) ;
302+ } ) ;
303+
304+ it ( 'should do nothing when transaction is null' , async ( ) => {
305+ setSplitShares ( null , 10000 , 'USD' , [ USER_ACCOUNT_ID , PARTICIPANT_ACCOUNT_ID ] ) ;
306+ await waitForBatchedUpdates ( ) ;
307+
308+ const draft = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . TRANSACTION_DRAFT } ${ TRANSACTION_ID } ` ) ;
309+ expect ( draft ) . toBeUndefined ( ) ;
310+ } ) ;
311+ } ) ;
312+
313+ describe ( 'resetSplitShares' , ( ) => {
314+ beforeEach ( async ( ) => {
315+ await Onyx . set ( ONYXKEYS . SESSION , { accountID : USER_ACCOUNT_ID } ) ;
316+ await waitForBatchedUpdates ( ) ;
317+ } ) ;
318+
319+ it ( 'should reset splitShares on TRANSACTION_DRAFT by default' , async ( ) => {
320+ const transaction = createRandomTransaction ( 1 ) ;
321+ transaction . transactionID = TRANSACTION_ID ;
322+ transaction . amount = 6000 ;
323+ transaction . currency = 'USD' ;
324+ transaction . splitShares = {
325+ [ USER_ACCOUNT_ID ] : { amount : 3000 , isModified : false } ,
326+ [ PARTICIPANT_ACCOUNT_ID ] : { amount : 3000 , isModified : false } ,
327+ } ;
328+ await Onyx . set ( `${ ONYXKEYS . COLLECTION . TRANSACTION_DRAFT } ${ TRANSACTION_ID } ` , transaction ) ;
329+ await waitForBatchedUpdates ( ) ;
330+
331+ resetSplitShares ( transaction , 8000 , 'USD' ) ;
332+ await waitForBatchedUpdates ( ) ;
333+
334+ const draft = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . TRANSACTION_DRAFT } ${ TRANSACTION_ID } ` ) ;
335+ expect ( draft ?. splitShares ) . toBeDefined ( ) ;
336+ expect ( draft ?. splitShares ?. [ USER_ACCOUNT_ID ] ) . toBeDefined ( ) ;
337+ expect ( draft ?. splitShares ?. [ PARTICIPANT_ACCOUNT_ID ] ) . toBeDefined ( ) ;
338+ } ) ;
339+
340+ it ( 'should reset splitShares on TRANSACTION when isDraft is false' , async ( ) => {
341+ const transaction = createRandomTransaction ( 1 ) ;
342+ transaction . transactionID = TRANSACTION_ID ;
343+ transaction . amount = 6000 ;
344+ transaction . currency = 'USD' ;
345+ transaction . splitShares = {
346+ [ USER_ACCOUNT_ID ] : { amount : 3000 , isModified : false } ,
347+ [ PARTICIPANT_ACCOUNT_ID ] : { amount : 3000 , isModified : false } ,
348+ } ;
349+ await Onyx . set ( `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ TRANSACTION_ID } ` , transaction ) ;
350+ await waitForBatchedUpdates ( ) ;
351+
352+ resetSplitShares ( transaction , 8000 , 'USD' , false ) ;
353+ await waitForBatchedUpdates ( ) ;
354+
355+ const updated = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ TRANSACTION_ID } ` ) ;
356+ expect ( updated ?. splitShares ) . toBeDefined ( ) ;
357+ expect ( updated ?. splitShares ?. [ USER_ACCOUNT_ID ] ) . toBeDefined ( ) ;
358+ expect ( updated ?. splitShares ?. [ PARTICIPANT_ACCOUNT_ID ] ) . toBeDefined ( ) ;
359+
360+ const draft = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . TRANSACTION_DRAFT } ${ TRANSACTION_ID } ` ) ;
361+ expect ( draft ) . toBeUndefined ( ) ;
362+ } ) ;
363+ } ) ;
241364} ) ;
0 commit comments