forked from openedx-unsupported/frontend-app-payment
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add PageLoadingDynamicPaymentMethods with timeout for DPM recei…
…pt redirect (#11) REV-4049
- Loading branch information
1 parent
bc3becb
commit 4aeb2f5
Showing
5 changed files
with
155 additions
and
22 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
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,58 @@ | ||
import React, { useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { getConfig } from '@edx/frontend-platform'; | ||
import { logInfo } from '@edx/frontend-platform/logging'; | ||
|
||
const PageLoadingDynamicPaymentMethods = ({ srMessage, orderNumber }) => { | ||
useEffect(() => { | ||
const timer = setTimeout(() => { | ||
logInfo(`Dynamic Payment Methods payment succeeded for edX order number ${orderNumber}, redirecting to ecommerce receipt page.`); | ||
const queryParams = `order_number=${orderNumber}&disable_back_button=${Number(true)}&dpm_enabled=${true}`; | ||
|
||
if (getConfig().ENVIRONMENT !== 'test') { | ||
/* istanbul ignore next */ | ||
global.location.assign(`${getConfig().ECOMMERCE_BASE_URL}/checkout/receipt/?${queryParams}`); | ||
} | ||
}, 3000); // Delay the redirect to receipt page by 3 seconds to make sure ecomm order fulfillment is done. | ||
|
||
return () => clearTimeout(timer); // On unmount, clear the timer | ||
}, [srMessage, orderNumber]); | ||
|
||
const renderSrMessage = () => { | ||
if (!srMessage) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<span className="sr-only"> | ||
{srMessage} | ||
</span> | ||
); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div | ||
className="d-flex justify-content-center align-items-center flex-column" | ||
style={{ | ||
height: '50vh', | ||
}} | ||
> | ||
<div className="spinner-border text-primary" data-testid="loading-page" role="status"> | ||
{renderSrMessage()} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
PageLoadingDynamicPaymentMethods.propTypes = { | ||
srMessage: PropTypes.string.isRequired, | ||
orderNumber: PropTypes.string, | ||
}; | ||
|
||
PageLoadingDynamicPaymentMethods.defaultProps = { | ||
orderNumber: null, | ||
}; | ||
|
||
export default PageLoadingDynamicPaymentMethods; |
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,77 @@ | ||
import React from 'react'; | ||
import { createStore } from 'redux'; | ||
import { Provider } from 'react-redux'; | ||
import { render, act } from '@testing-library/react'; | ||
import { IntlProvider } from '@edx/frontend-platform/i18n'; | ||
import { logInfo } from '@edx/frontend-platform/logging'; | ||
|
||
import createRootReducer from '../data/reducers'; | ||
import PageLoadingDynamicPaymentMethods from './PageLoadingDynamicPaymentMethods'; | ||
|
||
jest.mock('@edx/frontend-platform/logging', () => ({ | ||
logInfo: jest.fn(), | ||
})); | ||
|
||
describe('PageLoadingDynamicPaymentMethods', () => { | ||
let store; | ||
|
||
beforeEach(() => { | ||
store = createStore(createRootReducer()); | ||
jest.useFakeTimers(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.runOnlyPendingTimers(); | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('renders <PageLoadingDynamicPaymentMethods />', () => { | ||
const component = ( | ||
<IntlProvider locale="en"> | ||
<Provider store={store}> | ||
<PageLoadingDynamicPaymentMethods | ||
srMessage="" | ||
orderNumber="EDX-100001" | ||
/> | ||
</Provider> | ||
</IntlProvider> | ||
); | ||
const { container: tree } = render(component); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('it redirects to receipt page after 3 seconds delay', () => { | ||
const orderNumber = 'EDX-100001'; | ||
const logMessage = `Dynamic Payment Methods payment succeeded for edX order number ${orderNumber}, redirecting to ecommerce receipt page.`; | ||
render( | ||
<IntlProvider locale="en"> | ||
<Provider store={store}> | ||
<PageLoadingDynamicPaymentMethods | ||
srMessage="" | ||
orderNumber={orderNumber} | ||
/> | ||
</Provider> | ||
</IntlProvider>, | ||
); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(3000); | ||
}); | ||
expect(logInfo).toHaveBeenCalledWith(expect.stringMatching(logMessage)); | ||
}); | ||
|
||
it('cleans up the timer on unmount', () => { | ||
const { unmount } = render( | ||
<PageLoadingDynamicPaymentMethods | ||
srMessage="" | ||
orderNumber="EDX-100001" | ||
/>, | ||
); | ||
unmount(); | ||
act(() => { | ||
jest.advanceTimersByTime(3000); | ||
}); | ||
expect(logInfo).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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
18 changes: 18 additions & 0 deletions
18
src/payment/__snapshots__/PageLoadingDynamicPaymentMethods.test.jsx.snap
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,18 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`PageLoadingDynamicPaymentMethods renders <PageLoadingDynamicPaymentMethods /> 1`] = ` | ||
<div> | ||
<div> | ||
<div | ||
class="d-flex justify-content-center align-items-center flex-column" | ||
style="height: 50vh;" | ||
> | ||
<div | ||
class="spinner-border text-primary" | ||
data-testid="loading-page" | ||
role="status" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
`; |