diff --git a/react/lib/tests/util/api-client.test.ts b/react/lib/tests/util/api-client.test.ts index bdf1cbdf..8ef99a17 100644 --- a/react/lib/tests/util/api-client.test.ts +++ b/react/lib/tests/util/api-client.test.ts @@ -7,142 +7,247 @@ global.fetch = jest.fn(); describe('API Client Util Tests', () => { describe('shouldTriggerOnSuccess', () => { - it('returns true when all conditions match', () => { - const transaction:Transaction = { - amount: '100.00', - paymentId: '123', - message: 'test message', - rawMessage: 'test message', - hash: '', - timestamp: 0, - address: '' + it('true when amount, opReturn, and paymentId match the ones received in transaction', () => { + const transaction: Transaction = { + amount: '100.00', + paymentId: '123', + message: 'test message', + rawMessage: 'test message', + hash: '', + timestamp: 0, + address: '', }; - expect(shouldTriggerOnSuccess(transaction, resolveNumber('100.00'), '123', resolveNumber('100.00'), 'test message')).toBe(true); - + const expectedAmount = resolveNumber('100.00'); + const expectedOpReturn = 'test message'; + const expectedPaymentId = '123'; + + expect( + shouldTriggerOnSuccess( + transaction, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + ), + ).toBe(true); }); - it('returns true when tx paymentId is empty and component paymentId is undefined', () => { - const transaction:Transaction = { - amount: '101.00', - paymentId: '', - message: 'test opReturn message', - rawMessage: 'test opReturn message', - hash: '', - timestamp: 0, - address: '' - }; - expect(shouldTriggerOnSuccess(transaction, resolveNumber('101.00'), undefined, resolveNumber('101.00'), 'test opReturn message')).toBe(true); + it('true when paymentId is undefined and received paymentId is empty', () => { + const transaction: Transaction = { + amount: '101.00', + paymentId: '', + message: 'test opReturn message', + rawMessage: 'test opReturn message', + hash: '', + timestamp: 0, + address: '', + }; + const expectedPaymentId = undefined; + const expectedAmount = resolveNumber('101.00'); + const expectedOpReturn = 'test opReturn message'; + + expect( + shouldTriggerOnSuccess( + transaction, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + ), + ).toBe(true); }); - it('returns true when tx paymentId is empty and component paymentId is empty', () => { - const transaction:Transaction = { - amount: '101.00', - paymentId: '', - message: 'test opReturn message', - rawMessage: 'test opReturn message', - hash: '', - timestamp: 0, - address: '' - }; - expect(shouldTriggerOnSuccess(transaction, resolveNumber('101.00'),'', resolveNumber('101.00'), 'test opReturn message')).toBe(true); + it('true when both expected and received paymentId are empty ', () => { + const transaction: Transaction = { + amount: '101.00', + paymentId: '', + message: 'test opReturn message', + rawMessage: 'test opReturn message', + hash: '', + timestamp: 0, + address: '', + }; + const expectedPaymentId = ''; + const expectedAmount = resolveNumber('101.00'); + const expectedOpReturn = 'test opReturn message'; + + expect( + shouldTriggerOnSuccess( + transaction, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + ), + ).toBe(true); }); - it('returns false when the payment ID does not match', () => { - const transaction:Transaction = { - amount: '100.00', - paymentId: '123', - message: 'test opReturn message', - rawMessage: 'test opReturn message', - hash: '', - timestamp: 0, - address: '' + it('false when paymentId does not match received paymentId', () => { + const transaction: Transaction = { + amount: '100.00', + paymentId: '123', + message: 'test opReturn message', + rawMessage: 'test opReturn message', + hash: '', + timestamp: 0, + address: '', }; - expect(shouldTriggerOnSuccess(transaction, resolveNumber('100.00'), '999', resolveNumber('100.00'), 'test opReturn message')).toBe(false); + const expectedPaymentId = '999'; + const expectedAmount = resolveNumber('100.00'); + const expectedOpReturn = 'test opReturn message'; + + expect( + shouldTriggerOnSuccess( + transaction, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + ), + ).toBe(false); }); - it('returns false when crypto amounts do not match', () => { - const transaction:Transaction = { - amount: '101.00', - paymentId: '123', - message: 'test opReturn message', - rawMessage: 'test opReturn message', - hash: '', - timestamp: 0, - address: '' - }; - expect(shouldTriggerOnSuccess(transaction, resolveNumber('101.00'), '123', resolveNumber('100.00'), 'test opReturn message')).toBe(false); + it('false when amount does not match received amount', () => { + const transaction: Transaction = { + amount: '101.00', + paymentId: '123', + message: 'test opReturn message', + rawMessage: 'test opReturn message', + hash: '', + timestamp: 0, + address: '', + }; + const expectedPaymentId = '123'; + const expectedAmount = resolveNumber('100.00'); + const expectedOpReturn = 'test opReturn message'; + + expect( + shouldTriggerOnSuccess( + transaction, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + ), + ).toBe(false); }); - it('returns false when OpReturn data does not match', () => { - const transaction:Transaction = { - amount: '101.00', - paymentId: '123', - message: 'test opReturn message', - rawMessage: 'test opReturn message', - hash: '', - timestamp: 0, - address: '' - }; - expect(shouldTriggerOnSuccess(transaction, resolveNumber('101.00'), '123', resolveNumber('101.00'), 'test opReturn')).toBe(false); + it('false when opReturn message does not match received message', () => { + const transaction: Transaction = { + amount: '101.00', + paymentId: '123', + message: 'test opReturn message', + rawMessage: 'test opReturn message', + hash: '', + timestamp: 0, + address: '', + }; + const expectedPaymentId = '123'; + const expectedAmount = resolveNumber('101.00'); + const expectedOpReturn = 'test opReturn'; + + expect( + shouldTriggerOnSuccess( + transaction, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + ), + ).toBe(false); }); - it('should ignore amount validation when amount is not set', () => { - const transaction:Transaction = { - amount: '101.00', - paymentId: '123', - message: 'test opReturn', - rawMessage: 'test opReturn', - hash: '', - timestamp: 0, - address: '' - }; - expect(shouldTriggerOnSuccess(transaction, resolveNumber('101.00'), '123', undefined, 'test opReturn')).toBe(true); + it('should ignore amount validation when expected amount is undefined', () => { + const transaction: Transaction = { + amount: '101.00', + paymentId: '123', + message: 'test opReturn', + rawMessage: 'test opReturn', + hash: '', + timestamp: 0, + address: '', + }; + const expectedPaymentId = '123'; + const expectedAmount = undefined; + const expectedOpReturn = 'test opReturn'; + + expect( + shouldTriggerOnSuccess( + transaction, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + ), + ).toBe(true); }); - it('should ignore paymentId validation when paymentId does not exists', () => { - const transaction:Transaction = { - amount: '101.00', - paymentId: '123', - message: 'test opReturn', - rawMessage: 'test opReturn', - hash: '', - timestamp: 0, - address: '' - }; - expect(shouldTriggerOnSuccess(transaction, resolveNumber('101.00'), undefined, resolveNumber('101.00'), 'test opReturn')).toBe(true); + it('should ignore paymentId validation when paymentId is undefined', () => { + const transaction: Transaction = { + amount: '101.00', + paymentId: '123', + message: 'test opReturn', + rawMessage: 'test opReturn', + hash: '', + timestamp: 0, + address: '', + }; + const expectedPaymentId = undefined; + const expectedAmount = resolveNumber('101.00'); + const expectedOpReturn = 'test opReturn'; + + expect( + shouldTriggerOnSuccess( + transaction, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + ), + ).toBe(true); }); - it('should fail when there is a message but opReturn is not enabled', () => { - const transaction:Transaction = { - amount: '101.00', - paymentId: '123', - message: 'test opReturn', - rawMessage: 'test opReturn', - hash: '', - timestamp: 0, - address: '' - }; - expect(shouldTriggerOnSuccess(transaction, resolveNumber('101.00'), '123', resolveNumber('101.00'), undefined)).toBe(false); + it('false when opReturn is undefined and received opReturn message is not empty or undefined', () => { + const transaction: Transaction = { + amount: '101.00', + paymentId: '123', + message: 'test opReturn', + rawMessage: 'test opReturn', + hash: '', + timestamp: 0, + address: '', + }; + const expectedPaymentId = '123'; + const expectedAmount = resolveNumber('101.00'); + const expectedOpReturn = undefined; + + expect( + shouldTriggerOnSuccess( + transaction, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + ), + ).toBe(false); }); - it('returns true when message is in a different format then rawMessage', () => { - const transaction:Transaction = { - amount: '101.00', - paymentId: '123', - message:JSON.stringify({ - "classOf": "2013", - "bullYears": [ - "2013", - "2017", - "2021" - ] - }), - rawMessage: 'classOf=2013 bullYears=2013|2017|2021', - hash: '', - timestamp: 0, - address: '' - }; - expect(shouldTriggerOnSuccess(transaction, resolveNumber('101.00'), '123', resolveNumber('101.00'), 'classOf=2013 bullYears=2013|2017|2021')).toBe(true); + it('true when opReturn is in a different format than receved opReturn message but rawMessage matches expected opReturn', () => { + const transaction: Transaction = { + amount: '101.00', + paymentId: '123', + message: JSON.stringify({ + classOf: '2013', + bullYears: ['2013', '2017', '2021'], + }), + rawMessage: 'classOf=2013 bullYears=2013|2017|2021', + hash: '', + timestamp: 0, + address: '', + }; + const expectedPaymentId = '123'; + const expectedAmount = resolveNumber('101.00'); + const expectedOpReturn = 'classOf=2013 bullYears=2013|2017|2021'; + + expect( + shouldTriggerOnSuccess( + transaction, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + ), + ).toBe(true); }); }); });