Skip to content

Commit

Permalink
test: Add PageLoadingDynamicPaymentMethods test
Browse files Browse the repository at this point in the history
  • Loading branch information
julianajlk committed May 15, 2024
1 parent 8348333 commit a056839
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/payment/PageLoadingDynamicPaymentMethods.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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;
let originalLocation;

beforeAll(() => {
originalLocation = global.location;
delete global.location;
global.location = { assign: jest.fn() };
});

afterAll(() => {
global.location = originalLocation;
});

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.`;
const queryParams = `order_number=${orderNumber}&disable_back_button=${Number(true)}&dpm_enabled=${true}`;
render(
<IntlProvider locale="en">
<Provider store={store}>
<PageLoadingDynamicPaymentMethods
srMessage=""
orderNumber={orderNumber}
/>
</Provider>
</IntlProvider>,
);

act(() => {
jest.advanceTimersByTime(3000);
});
expect(logInfo).toHaveBeenCalledWith(expect.stringMatching(logMessage));
expect(global.location.assign).toHaveBeenCalledWith(expect.stringContaining(`/checkout/receipt/?${queryParams}`));
});

it('cleans up the timer on unmount', () => {
const { unmount } = render(
<PageLoadingDynamicPaymentMethods
srMessage=""
orderNumber="EDX-100001"
/>,
);
unmount();
act(() => {
jest.advanceTimersByTime(3000);
});
expect(window.location.assign).not.toHaveBeenCalled();
});
});
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>
`;

0 comments on commit a056839

Please sign in to comment.