payment-error-notification.test.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { expect } from 'chai'
  2. import { render, screen } from '@testing-library/react'
  3. import PaymentErrorNotification from '../../../../../../frontend/js/features/subscription/components/shared/payment-error-notification'
  4. import { FetchError } from '@/infrastructure/fetch-json'
  5. import { billingPortalUrl } from '@/features/subscription/data/subscription-url'
  6. describe('<PaymentErrorNotification />', function () {
  7. it('does not render if error is missing', function () {
  8. render(<PaymentErrorNotification error={null} />)
  9. expect(screen.queryByRole('link')).to.be.null
  10. })
  11. it('renders a generic error if adviceCode is missing', function () {
  12. const error = { data: { adviceCode: null } } as FetchError
  13. render(<PaymentErrorNotification error={error} />)
  14. expect(
  15. screen.queryAllByText(
  16. (content, element) =>
  17. element?.textContent ===
  18. 'Sorry, something went wrong. Please try again. If the problem continues please contact us.'
  19. ).length
  20. ).to.be.greaterThan(0)
  21. const link = screen.queryByRole('link')
  22. expect(link).to.exist
  23. expect(link?.getAttribute('href')).to.equal('/contact')
  24. })
  25. it('renders an error if adviceCode is missing but clientSecret is present', function () {
  26. const error = { data: { clientSecret: 'cs_12345' } } as FetchError
  27. render(<PaymentErrorNotification error={error} />)
  28. expect(
  29. screen.queryAllByText(
  30. (content, element) =>
  31. element?.textContent ===
  32. 'We couldn’t complete your payment because authentication wasn’t successful. Please try again or choose a different payment method. If the problem continues please contact us.'
  33. ).length
  34. ).to.be.greaterThan(0)
  35. const link = screen.queryByRole('link')
  36. expect(link).to.exist
  37. expect(link?.getAttribute('href')).to.equal('/contact')
  38. })
  39. it('renders a error to try again if adviceCode is try_again_later', function () {
  40. const error = { data: { adviceCode: 'try_again_later' } } as FetchError
  41. render(<PaymentErrorNotification error={error} />)
  42. expect(
  43. screen.queryAllByText(
  44. (content, element) =>
  45. element?.textContent ===
  46. 'We were unable to process your payment. Please try again later or contact us for assistance.'
  47. ).length
  48. ).to.be.greaterThan(0)
  49. const link = screen.queryByRole('link')
  50. expect(link).to.exist
  51. expect(link?.getAttribute('href')).to.equal('/contact')
  52. })
  53. it('renders an error to update payment method if adviceCode do_not_try_again', function () {
  54. const error = { data: { adviceCode: 'do_not_try_again' } } as FetchError
  55. render(<PaymentErrorNotification error={error} />)
  56. expect(
  57. screen.queryAllByText(
  58. (content, element) =>
  59. element?.textContent ===
  60. 'Your payment was declined. Please update your billing information and try again.'
  61. ).length
  62. ).to.be.greaterThan(0)
  63. const link = screen.queryByRole('link')
  64. expect(link).to.exist
  65. expect(link?.getAttribute('href')).to.equal(billingPortalUrl)
  66. })
  67. it('renders an error to update payment method if adviceCode confirm_card_data', function () {
  68. const error = { data: { adviceCode: 'confirm_card_data' } } as FetchError
  69. render(<PaymentErrorNotification error={error} />)
  70. expect(
  71. screen.queryAllByText(
  72. (content, element) =>
  73. element?.textContent ===
  74. 'Your payment was declined. Please update your billing information and try again.'
  75. ).length
  76. ).to.be.greaterThan(0)
  77. const link = screen.queryByRole('link')
  78. expect(link).to.exist
  79. expect(link?.getAttribute('href')).to.equal(billingPortalUrl)
  80. })
  81. })