-
Notifications
You must be signed in to change notification settings - Fork 231
test(payments): Add test coverage for Checkout - PayPal flows #20710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xlisachan
wants to merge
2
commits into
main
Choose a base branch
from
PAY-3688_paypal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
325 changes: 325 additions & 0 deletions
325
apps/payments/next/app/[locale]/subscriptions/manage/page.test.tsx
This file contains hidden or 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,325 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { render, screen } from '@testing-library/react'; | ||
| import '@testing-library/jest-dom'; | ||
| import { | ||
| DefaultPaymentMethodFactory, | ||
| DefaultPaymentMethodErrorFactory, | ||
| SubPlatPaymentMethodType, | ||
| } from '@fxa/payments/customer/testing'; | ||
| import { SubscriptionContentFactory } from '@fxa/payments/management/testing'; | ||
| import { SessionFactory } from '@fxa/payments/ui-auth/testing'; | ||
| import Manage from './page'; | ||
|
|
||
| const mockGetSubManPageContentAction = jest.fn(); | ||
| const mockGetExperimentsAction = jest.fn(); | ||
| const mockAuth = jest.fn(); | ||
| const mockGetL10n = jest.fn(); | ||
| const mockRedirect = jest.fn(); | ||
| const mockHeaders = jest.fn(); | ||
|
|
||
| jest.mock('@fxa/payments/ui/actions', () => ({ | ||
| __esModule: true, | ||
| getSubManPageContentAction: (...args: unknown[]) => | ||
| mockGetSubManPageContentAction(...args), | ||
| getExperimentsAction: (...args: unknown[]) => | ||
| mockGetExperimentsAction(...args), | ||
| })); | ||
|
|
||
| jest.mock('apps/payments/next/auth', () => ({ | ||
| __esModule: true, | ||
| auth: () => mockAuth(), | ||
| })); | ||
|
|
||
| jest.mock('@fxa/payments/ui/server', () => ({ | ||
| __esModule: true, | ||
| getApp: () => ({ | ||
| getL10n: (...args: unknown[]) => mockGetL10n(...args), | ||
| }), | ||
| })); | ||
|
|
||
| jest.mock('apps/payments/next/config', () => ({ | ||
| __esModule: true, | ||
| config: { | ||
| paymentsNextHostedUrl: 'https://payments.example.com', | ||
| paypal: { clientId: 'paypal-client-id' }, | ||
| csp: { paypalApi: 'https://paypal.example.com' }, | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('next/navigation', () => ({ | ||
| __esModule: true, | ||
| redirect: (...args: unknown[]) => mockRedirect(...args), | ||
| })); | ||
|
|
||
| jest.mock('next/headers', () => ({ | ||
| __esModule: true, | ||
| headers: () => mockHeaders(), | ||
| })); | ||
|
|
||
| // eslint-disable-next-line @next/next/no-img-element | ||
| jest.mock('next/image', () => ({ | ||
| __esModule: true, | ||
| default: ({ | ||
| alt, | ||
| className, | ||
| }: { | ||
| alt?: string; | ||
| className?: string; | ||
| src?: unknown; | ||
| // eslint-disable-next-line @next/next/no-img-element | ||
| }) => <img alt={alt ?? ''} className={className} src="mock-image" />, | ||
| })); | ||
|
|
||
| jest.mock('@fxa/payments/customer', () => { | ||
| const actual = jest.requireActual('@fxa/payments/customer/testing'); | ||
| return { | ||
| __esModule: true, | ||
| ...actual, | ||
| SubPlatPaymentMethodType: { | ||
| PayPal: 'external_paypal', | ||
| Card: 'card', | ||
| ApplePay: 'apple_pay', | ||
| GooglePay: 'google_pay', | ||
| Link: 'link', | ||
| Stripe: 'stripe', | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| jest.mock('@fxa/payments/ui', () => ({ | ||
| __esModule: true, | ||
| Banner: ({ | ||
| children, | ||
| variant, | ||
| }: { | ||
| children: React.ReactNode; | ||
| variant: string; | ||
| }) => ( | ||
| <div data-testid={`banner-${variant}`} role="alert"> | ||
| {children} | ||
| </div> | ||
| ), | ||
| BannerVariant: { | ||
| Error: 'error', | ||
| Info: 'info', | ||
| Success: 'success', | ||
| Warning: 'warning', | ||
| }, | ||
| formatPlanInterval: jest.fn(() => 'monthly'), | ||
| FreeTrialContent: () => <div data-testid="free-trial-content" />, | ||
| getCardIcon: jest.fn(() => ({ | ||
| img: 'mock-card.svg', | ||
| altText: 'PayPal', | ||
| width: 40, | ||
| height: 24, | ||
| })), | ||
| GleanPageView: () => null, | ||
| SubscriptionContent: () => <div data-testid="subscription-content" />, | ||
| })); | ||
|
|
||
| jest.mock('@fxa/shared/react', () => ({ | ||
| __esModule: true, | ||
| LinkExternal: ({ | ||
| children, | ||
| href, | ||
| ...props | ||
| }: { | ||
| children: React.ReactNode; | ||
| href: string; | ||
| [key: string]: unknown; | ||
| }) => ( | ||
| <a href={href} {...(props as React.AnchorHTMLAttributes<HTMLAnchorElement>)}> | ||
| {children} | ||
| </a> | ||
| ), | ||
| })); | ||
|
|
||
| jest.mock('clsx', () => ({ | ||
| __esModule: true, | ||
| default: (...args: unknown[]) => args.filter(Boolean).join(' '), | ||
| })); | ||
|
|
||
| jest.mock( | ||
| '@fxa/shared/assets/images/alert-yellow.svg', | ||
| () => 'alert-yellow.svg', | ||
| { virtual: true } | ||
| ); | ||
| jest.mock( | ||
| '@fxa/shared/assets/images/arrow-down.svg', | ||
| () => 'arrow-down.svg', | ||
| { virtual: true } | ||
| ); | ||
| jest.mock( | ||
| '@fxa/shared/assets/images/apple-logo.svg', | ||
| () => 'apple-logo.svg', | ||
| { virtual: true } | ||
| ); | ||
| jest.mock( | ||
| '@fxa/shared/assets/images/google-logo.svg', | ||
| () => 'google-logo.svg', | ||
| { virtual: true } | ||
| ); | ||
| jest.mock( | ||
| '@fxa/shared/assets/images/new-window.svg', | ||
| () => 'new-window.svg', | ||
| { virtual: true } | ||
| ); | ||
| jest.mock('@fxa/shared/assets/images/error.svg', () => 'error.svg', { | ||
| virtual: true, | ||
| }); | ||
|
|
||
| const baseSession = SessionFactory(); | ||
|
|
||
| const basePageContent = { | ||
| accountCreditBalance: { balance: 0, currency: null }, | ||
| defaultPaymentMethod: undefined as | ||
| | ReturnType<typeof DefaultPaymentMethodFactory> | ||
| | undefined, | ||
| isStripeCustomer: true, | ||
| subscriptions: [] as ReturnType<typeof SubscriptionContentFactory>[], | ||
| appleIapSubscriptions: [], | ||
| googleIapSubscriptions: [], | ||
| trialSubscriptions: [], | ||
| }; | ||
|
|
||
| const mockL10n = { | ||
| getString: (_id: string, ...rest: unknown[]) => { | ||
| const fallback = rest.length === 1 ? rest[0] : rest[1]; | ||
| return typeof fallback === 'string' ? fallback : ''; | ||
| }, | ||
| getLocalizedMonthYearString: () => '12/2030', | ||
| getLocalizedCurrencyString: () => '$0.00', | ||
| }; | ||
|
|
||
| const defaultParams = Promise.resolve({ locale: 'en' }); | ||
| const defaultSearchParams = Promise.resolve({}); | ||
|
|
||
| async function renderPage( | ||
| paramsOverride?: Promise<Record<string, string>>, | ||
| searchParamsOverride?: Promise<Record<string, string | string[]>> | ||
| ) { | ||
| const jsx = await Manage({ | ||
| params: (paramsOverride ?? defaultParams) as any, | ||
| searchParams: (searchParamsOverride ?? defaultSearchParams) as any, | ||
| }); | ||
| return render(jsx); | ||
| } | ||
|
|
||
| describe('Manage page — payment method error banner', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockHeaders.mockResolvedValue({ get: () => 'en-US' }); | ||
| mockAuth.mockResolvedValue(baseSession); | ||
| mockGetL10n.mockReturnValue(mockL10n); | ||
| mockGetExperimentsAction.mockResolvedValue({ Features: {} }); | ||
| mockGetSubManPageContentAction.mockResolvedValue(basePageContent); | ||
| }); | ||
|
|
||
| it('does not render error banner when there is no payment method error', async () => { | ||
| mockGetSubManPageContentAction.mockResolvedValue({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: could this use a factory instead, where possible? This also applies to all other similar instances in this test. |
||
| ...basePageContent, | ||
| defaultPaymentMethod: DefaultPaymentMethodFactory({ | ||
| type: SubPlatPaymentMethodType.PayPal, | ||
| billingAgreementId: 'ba_active', | ||
| hasPaymentMethodError: undefined, | ||
| }), | ||
| subscriptions: [SubscriptionContentFactory()], | ||
| }); | ||
|
|
||
| await renderPage(); | ||
|
|
||
| expect(screen.queryByTestId('banner-error')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders error banner with PayPal funding source error content', async () => { | ||
| const paypalFundingSourceError = DefaultPaymentMethodErrorFactory({ | ||
| paymentMethodType: SubPlatPaymentMethodType.PayPal, | ||
| bannerTitle: 'Invalid payment information', | ||
| bannerTitleFtl: | ||
| 'error-payment-method-banner-title-invalid-payment-information', | ||
| bannerMessage: 'There is an issue with your account.', | ||
| bannerMessageFtl: 'error-payment-method-banner-message-account-issue', | ||
| bannerLinkLabel: 'Manage payment method', | ||
| bannerLinkLabelFtl: | ||
| 'subscription-management-button-manage-payment-method-1', | ||
| message: | ||
| 'There is an issue with your PayPal account. Please resolve the issue to maintain your active subscriptions.', | ||
| messageFtl: 'subscription-management-error-paypal-billing-agreement', | ||
| }); | ||
|
|
||
| mockGetSubManPageContentAction.mockResolvedValue({ | ||
| ...basePageContent, | ||
| defaultPaymentMethod: DefaultPaymentMethodFactory({ | ||
| type: SubPlatPaymentMethodType.PayPal, | ||
| billingAgreementId: 'ba_123', | ||
| hasPaymentMethodError: paypalFundingSourceError, | ||
| }), | ||
| subscriptions: [SubscriptionContentFactory()], | ||
| }); | ||
|
|
||
| await renderPage(); | ||
|
|
||
| const errorBanner = screen.getByTestId('banner-error'); | ||
| expect(errorBanner).toBeInTheDocument(); | ||
| expect(errorBanner).toHaveTextContent('Invalid payment information'); | ||
| expect(errorBanner).toHaveTextContent( | ||
| 'There is an issue with your account.' | ||
| ); | ||
| expect(errorBanner).toHaveTextContent('Manage payment method'); | ||
| }); | ||
|
|
||
| it('links to PayPal payment management page when error is on PayPal method', async () => { | ||
| const paypalError = DefaultPaymentMethodErrorFactory({ | ||
| paymentMethodType: SubPlatPaymentMethodType.PayPal, | ||
| }); | ||
|
|
||
| mockGetSubManPageContentAction.mockResolvedValue({ | ||
| ...basePageContent, | ||
| defaultPaymentMethod: DefaultPaymentMethodFactory({ | ||
| type: SubPlatPaymentMethodType.PayPal, | ||
| billingAgreementId: 'ba_123', | ||
| hasPaymentMethodError: paypalError, | ||
| }), | ||
| subscriptions: [SubscriptionContentFactory()], | ||
| }); | ||
|
|
||
| await renderPage(); | ||
|
|
||
| const errorBanner = screen.getByTestId('banner-error'); | ||
| const bannerLink = errorBanner.querySelector('a'); | ||
| expect(bannerLink).toHaveAttribute( | ||
| 'href', | ||
| 'https://payments.example.com/en/subscriptions/payments/paypal' | ||
| ); | ||
| }); | ||
|
|
||
| it('renders inline error message in payment method details section', async () => { | ||
| const paypalError = DefaultPaymentMethodErrorFactory({ | ||
| paymentMethodType: SubPlatPaymentMethodType.PayPal, | ||
| message: | ||
| 'There is an issue with your PayPal account. Please resolve the issue to maintain your active subscriptions.', | ||
| messageFtl: 'subscription-management-error-paypal-billing-agreement', | ||
| }); | ||
|
|
||
| mockGetSubManPageContentAction.mockResolvedValue({ | ||
| ...basePageContent, | ||
| defaultPaymentMethod: DefaultPaymentMethodFactory({ | ||
| type: SubPlatPaymentMethodType.PayPal, | ||
| billingAgreementId: 'ba_123', | ||
| hasPaymentMethodError: paypalError, | ||
| }), | ||
| subscriptions: [SubscriptionContentFactory()], | ||
| }); | ||
|
|
||
| await renderPage(); | ||
|
|
||
| expect( | ||
| screen.getByText( | ||
| /There is an issue with your PayPal account\. Please resolve the issue to maintain your active subscriptions\./ | ||
| ) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: are the tests excluded from the build?
The tests and imports of those tests should not be included in the final build of
payments-next.