-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display payment error message in the Payment context with Blocks (#9017)
- Loading branch information
Showing
9 changed files
with
302 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: fix | ||
|
||
Display payment error message in the Payment context with Blocks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { usePaymentFailHandler, useFingerprint } from '../hooks'; | ||
import * as fingerprintModule from '../../utils/fingerprint'; | ||
// import { act } from '@testing-library/react'; | ||
|
||
describe( 'usePaymentFailHandler', () => { | ||
let mockOnCheckoutFail; | ||
let mockEmitResponse; | ||
|
||
beforeEach( () => { | ||
mockOnCheckoutFail = jest.fn(); | ||
mockEmitResponse = { | ||
noticeContexts: { | ||
PAYMENTS: 'payments_context', | ||
}, | ||
}; | ||
} ); | ||
|
||
it( 'should return the correct failure response checkout processor payment failure', () => { | ||
const errorMessage = 'Your card was declined.'; | ||
const paymentDetails = { | ||
errorMessage: errorMessage, | ||
}; | ||
|
||
renderHook( () => | ||
usePaymentFailHandler( mockOnCheckoutFail, mockEmitResponse ) | ||
); | ||
|
||
expect( mockOnCheckoutFail ).toHaveBeenCalled(); | ||
const failureResponse = mockOnCheckoutFail.mock.calls[ 0 ][ 0 ]( { | ||
processingResponse: { paymentDetails }, | ||
} ); | ||
|
||
expect( failureResponse ).toEqual( { | ||
type: 'failure', | ||
message: errorMessage, | ||
messageContext: 'payments_context', | ||
} ); | ||
} ); | ||
} ); | ||
|
||
describe( 'useFingerprint', () => { | ||
it( 'should return fingerprint', async () => { | ||
const mockVisitorId = 'test-visitor-id'; | ||
const mockGetFingerprint = jest | ||
.fn() | ||
.mockResolvedValue( { visitorId: mockVisitorId } ); | ||
|
||
jest.spyOn( fingerprintModule, 'getFingerprint' ).mockImplementation( | ||
mockGetFingerprint | ||
); | ||
|
||
let hook; | ||
|
||
await act( async () => { | ||
hook = renderHook( () => useFingerprint() ); | ||
} ); | ||
|
||
const [ fingerprint, error ] = hook.result.current; | ||
|
||
expect( mockGetFingerprint ).toHaveBeenCalledTimes( 1 ); | ||
expect( fingerprint ).toBe( mockVisitorId ); | ||
expect( error ).toBeNull(); | ||
} ); | ||
|
||
it( 'should handle errors when getting fingerprint fails', async () => { | ||
const mockError = new Error( 'Test error' ); | ||
const mockGetFingerprint = jest.fn().mockRejectedValue( mockError ); | ||
|
||
jest.spyOn( fingerprintModule, 'getFingerprint' ).mockImplementation( | ||
mockGetFingerprint | ||
); | ||
|
||
let hook; | ||
|
||
await act( async () => { | ||
hook = renderHook( () => useFingerprint() ); | ||
} ); | ||
|
||
const [ fingerprint, error ] = hook.result.current; | ||
|
||
expect( mockGetFingerprint ).toHaveBeenCalledTimes( 1 ); | ||
expect( fingerprint ).toBe( '' ); | ||
expect( error ).toBe( mockError.message ); | ||
} ); | ||
|
||
it( 'should use generic error message when error has no message', async () => { | ||
const mockGetFingerprint = jest.fn().mockRejectedValue( {} ); | ||
|
||
jest.spyOn( fingerprintModule, 'getFingerprint' ).mockImplementation( | ||
mockGetFingerprint | ||
); | ||
|
||
let hook; | ||
|
||
await act( async () => { | ||
hook = renderHook( () => useFingerprint() ); | ||
} ); | ||
|
||
const [ fingerprint, error ] = hook.result.current; | ||
|
||
expect( mockGetFingerprint ).toHaveBeenCalledTimes( 1 ); | ||
expect( fingerprint ).toBe( '' ); | ||
expect( error ).toBe( fingerprintModule.FINGERPRINT_GENERIC_ERROR ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.