checkout.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import CheckoutPanel from '../../../../../../frontend/js/features/subscription/components/new/checkout/checkout-panel'
  2. import { PaymentProvider } from '../../../../../../frontend/js/features/subscription/context/payment-context'
  3. import { plans } from '../../fixtures/plans'
  4. import {
  5. createFakeRecurly,
  6. defaultSubscription,
  7. ElementsBase,
  8. } from '../../fixtures/recurly-mock'
  9. import { fillForm } from '../../helpers/payment'
  10. import { cloneDeep } from 'lodash'
  11. import { TokenHandler, RecurlyError } from 'recurly__recurly-js'
  12. function CheckoutPanelWithPaymentProvider() {
  13. return (
  14. <PaymentProvider publicKey="0000">
  15. <CheckoutPanel />
  16. </PaymentProvider>
  17. )
  18. }
  19. describe('checkout panel', function () {
  20. const itmCampaign = 'fake_itm_campaign'
  21. const itmContent = 'fake_itm_content'
  22. const itmReferrer = 'fake_itm_referrer'
  23. beforeEach(function () {
  24. const plan = plans.find(({ planCode }) => planCode === 'student-annual')
  25. if (!plan) {
  26. throw new Error('No plan was found while running the test!')
  27. }
  28. cy.window().then(win => {
  29. win.metaAttributesCache = new Map()
  30. win.metaAttributesCache.set('ol-countryCode', '')
  31. win.metaAttributesCache.set('ol-recurlyApiKey', '0000')
  32. win.metaAttributesCache.set('ol-recommendedCurrency', 'USD')
  33. win.metaAttributesCache.set('ol-plan', plan)
  34. win.metaAttributesCache.set('ol-planCode', plan.planCode)
  35. win.metaAttributesCache.set('ol-showCouponField', true)
  36. win.metaAttributesCache.set('ol-itm_campaign', itmCampaign)
  37. win.metaAttributesCache.set('ol-itm_content', itmContent)
  38. win.metaAttributesCache.set('ol-itm_referrer', itmReferrer)
  39. cy.wrap(plan).as('plan')
  40. // init default recurly
  41. win.recurly = createFakeRecurly(defaultSubscription)
  42. cy.interceptEvents()
  43. })
  44. cy.mount(<CheckoutPanelWithPaymentProvider />)
  45. cy.findByTestId('checkout-form').as('form')
  46. })
  47. it('renders heading', function () {
  48. cy.contains(/select a payment method/i)
  49. })
  50. it('renders student disclaimer', function () {
  51. cy.contains(
  52. 'The educational discount applies to all students at secondary and postsecondary institutions ' +
  53. '(schools and universities). We may contact you to confirm that you’re eligible for the discount.'
  54. )
  55. })
  56. it('renders payment method toggle', function () {
  57. cy.findByTestId('payment-method-toggle').within(() => {
  58. cy.findByLabelText(/card payment/i)
  59. cy.findByLabelText(/paypal/i)
  60. })
  61. })
  62. it('renders address first line input', function () {
  63. cy.get('@form').within(() => {
  64. cy.findByLabelText('Address')
  65. cy.findByLabelText(/this address will be shown on the invoice/i)
  66. })
  67. })
  68. it('renders address second line input', function () {
  69. cy.get('@form').within(() => {
  70. cy.findByLabelText(/address second line/i).should('not.exist')
  71. cy.findByRole('button', { name: /add another address line/i }).click()
  72. cy.findByLabelText(/address second line/i)
  73. })
  74. })
  75. it('renders postal code input', function () {
  76. cy.get('@form').within(() => {
  77. cy.findByLabelText(/postal code/i)
  78. })
  79. })
  80. it('renders country dropdown', function () {
  81. cy.get('@form').within(() => {
  82. cy.findByLabelText(/country/i)
  83. })
  84. })
  85. it('renders company details', function () {
  86. cy.get('@form').within(() => {
  87. cy.findByLabelText(/add company details/i).as('checkbox')
  88. cy.get('@checkbox').should('not.be.checked')
  89. cy.findByLabelText(/company name/i).should('not.exist')
  90. cy.findByLabelText(/vat number/i).should('not.exist')
  91. cy.get('@checkbox').click()
  92. cy.findByLabelText(/company name/i)
  93. cy.findByLabelText(/vat number/i)
  94. })
  95. })
  96. it('renders coupon field', function () {
  97. cy.get('@form').within(() => {
  98. cy.findByLabelText(/coupon code/i)
  99. })
  100. })
  101. it('renders tos agreement notice', function () {
  102. cy.contains(/by subscribing, you agree to our terms of service/i)
  103. })
  104. it('renders recurly error', function () {
  105. cy.window().then(win => {
  106. win.recurly = undefined!
  107. })
  108. cy.mount(<CheckoutPanelWithPaymentProvider />)
  109. cy.contains(
  110. /sorry, there was an error talking to our payment provider. Please try again in a few moments/i
  111. )
  112. cy.contains(
  113. /if you are using any ad or script blocking extensions in your browser, you may need to temporarily disable them/i
  114. )
  115. })
  116. it('calls recurly.token on submit', function () {
  117. cy.window().then(win => {
  118. cy.stub(win.recurly, 'token')
  119. })
  120. cy.mount(<CheckoutPanelWithPaymentProvider />)
  121. cy.get('@form').within(() => fillForm())
  122. cy.findByRole('button', { name: /upgrade now/i }).click()
  123. cy.window().then(win => {
  124. expect(win.recurly.token).to.be.calledOnceWith(
  125. Cypress.sinon.match.instanceOf(ElementsBase),
  126. {
  127. first_name: '1',
  128. last_name: '1',
  129. postal_code: '1',
  130. address1: '1',
  131. address2: '',
  132. state: '',
  133. city: '',
  134. country: 'BG',
  135. coupon: '',
  136. },
  137. Cypress.sinon.match.func
  138. )
  139. })
  140. })
  141. it('renders generic error', function () {
  142. const errorMessage = 'generic error'
  143. cy.window().then(win => {
  144. win.recurly = createFakeRecurly(defaultSubscription, {
  145. token: (_1: unknown, _2: unknown, handler: TokenHandler) => {
  146. const err = new Error(errorMessage) as RecurlyError
  147. setTimeout(() => handler(err, { id: '1', type: 'abc' }), 100)
  148. },
  149. })
  150. })
  151. cy.mount(<CheckoutPanelWithPaymentProvider />)
  152. cy.get('@form').within(() => fillForm())
  153. cy.findByRole('button', { name: /upgrade now/i }).as('button')
  154. cy.get('@button').click()
  155. cy.get('@button').within(() => {
  156. cy.contains(/processing/i)
  157. })
  158. cy.findByRole('alert').should('have.text', errorMessage)
  159. cy.get('@button').within(() => {
  160. cy.findByText(/processing/i).should('not.exist')
  161. })
  162. })
  163. it('renders prefilled coupon input', function () {
  164. const couponCode = 'promo_code'
  165. cy.window().then(win => {
  166. win.metaAttributesCache.set('ol-couponCode', couponCode)
  167. })
  168. cy.mount(<CheckoutPanelWithPaymentProvider />)
  169. cy.findByLabelText(/coupon code/i).should('have.value', couponCode)
  170. })
  171. it('calls coupon method when entering coupon code', function () {
  172. const couponCode = 'promo_code'
  173. cy.window().then(win => {
  174. const couponStub = cy.stub().as('coupon')
  175. couponStub.returnsThis()
  176. win.recurly = createFakeRecurly({
  177. ...defaultSubscription,
  178. coupon: couponStub,
  179. })
  180. })
  181. cy.mount(<CheckoutPanelWithPaymentProvider />)
  182. cy.get('@coupon').should('have.been.calledOnce')
  183. cy.findByTestId('checkout-form').within(() => {
  184. cy.findByLabelText(/coupon code/i).type(couponCode, { delay: 0 })
  185. cy.findByLabelText(/coupon code/i).blur()
  186. })
  187. cy.get('@coupon')
  188. .should('have.been.calledTwice')
  189. .and('have.been.calledWith', couponCode)
  190. })
  191. it('enters invalid coupon code', function () {
  192. cy.window().then(win => {
  193. const catchStub = cy.stub().as('catch')
  194. catchStub.onFirstCall().returnsThis()
  195. catchStub
  196. .onSecondCall()
  197. .callsFake(function (this: unknown, cb: (err: RecurlyError) => void) {
  198. const err = {
  199. name: 'api-error',
  200. code: 'not-found',
  201. } as RecurlyError
  202. cb(err)
  203. return this
  204. })
  205. win.recurly = createFakeRecurly({
  206. ...defaultSubscription,
  207. catch: catchStub,
  208. })
  209. })
  210. cy.mount(<CheckoutPanelWithPaymentProvider />)
  211. cy.findByTestId('checkout-form').within(() => {
  212. cy.findByLabelText(/coupon code/i).type('promo_code', { delay: 0 })
  213. cy.findByLabelText(/coupon code/i).blur()
  214. })
  215. cy.findByRole('alert').within(() => {
  216. cy.contains(/coupon code is not valid for selected plan/i)
  217. })
  218. })
  219. it('fails coupon verification', function () {
  220. cy.window().then(win => {
  221. const catchStub = cy.stub().as('catch')
  222. // call original method on change event
  223. catchStub.onFirstCall().returnsThis()
  224. catchStub
  225. .onSecondCall()
  226. .callsFake(function (this: unknown, cb: (err: RecurlyError) => void) {
  227. const err = {} as RecurlyError
  228. try {
  229. cb(err)
  230. } catch (e) {}
  231. return this
  232. })
  233. win.recurly = createFakeRecurly({
  234. ...defaultSubscription,
  235. catch: catchStub,
  236. })
  237. })
  238. cy.mount(<CheckoutPanelWithPaymentProvider />)
  239. cy.get('@catch').should('have.been.calledOnce')
  240. cy.findByTestId('checkout-form').within(() => {
  241. cy.findByLabelText(/coupon code/i).type('promo_code', { delay: 0 })
  242. cy.findByLabelText(/coupon code/i).blur()
  243. })
  244. cy.get('@catch').should('have.been.calledTwice')
  245. cy.findByRole('alert').within(() => {
  246. cy.contains(/an error occurred when verifying the coupon code/i)
  247. })
  248. })
  249. /* The test is disabled due to https://github.com/overleaf/internal/issues/12004
  250. it.skip('creates a new subscription', function () {
  251. cy.stub(locationModule, 'assign').as('assign')
  252. cy.intercept('POST', 'user/subscription/create', {
  253. statusCode: 201,
  254. }).as('create')
  255. cy.mount(<CheckoutPanelWithPaymentProvider />)
  256. cy.findByTestId('checkout-form').within(() => fillForm())
  257. cy.findByRole('button', { name: /upgrade now/i }).click()
  258. // verify itm params are also passed
  259. cy.get('@create')
  260. .its('request.body.subscriptionDetails')
  261. .should('contain', {
  262. ITMCampaign: itmCampaign,
  263. ITMContent: itmContent,
  264. ITMReferrer: itmReferrer,
  265. })
  266. cy.get('@assign')
  267. .should('have.been.calledOnce')
  268. .and('have.been.calledWith', '/user/subscription/thank-you')
  269. })
  270. */
  271. it('fails to create a new subscription', function () {
  272. cy.intercept('POST', 'user/subscription/create', {
  273. statusCode: 404,
  274. })
  275. cy.mount(<CheckoutPanelWithPaymentProvider />)
  276. cy.findByTestId('checkout-form').within(() => fillForm())
  277. cy.findByRole('button', { name: /upgrade now/i }).click()
  278. cy.findByRole('alert').within(() => {
  279. cy.contains(/something went wrong processing the request/i)
  280. })
  281. })
  282. describe('3DS challenge', function () {
  283. it('shows three d secure challenge', function () {
  284. cy.intercept('POST', 'user/subscription/create', {
  285. statusCode: 404,
  286. body: {
  287. threeDSecureActionTokenId: '123',
  288. },
  289. })
  290. cy.mount(<CheckoutPanelWithPaymentProvider />)
  291. cy.findByTestId('checkout-form').within(() => fillForm())
  292. cy.findByRole('button', { name: /upgrade now/i }).click()
  293. cy.findByRole('alert').within(() => {
  294. cy.contains(
  295. /your card must be authenticated with 3D Secure before continuing/i
  296. )
  297. })
  298. cy.contains('3D challenge content')
  299. })
  300. })
  301. describe('card payments', function () {
  302. beforeEach(function () {
  303. cy.findByLabelText(/card payment/i).click()
  304. })
  305. it('renders card element', function () {
  306. cy.get('@form').within(() => {
  307. cy.findByText(/card details/i, { selector: 'label' })
  308. cy.findByTestId('test-card-element')
  309. })
  310. })
  311. it('verifies the card element does not disappear when switching between payment methods', function () {
  312. cy.get('@form').within(() => {
  313. cy.findByText(/card details/i, { selector: 'label' })
  314. cy.findByTestId('test-card-element')
  315. cy.findByLabelText(/paypal/i).click()
  316. cy.findByLabelText(/card payment/i).click()
  317. cy.findByText(/card details/i, { selector: 'label' })
  318. cy.findByTestId('test-card-element')
  319. })
  320. })
  321. it('renders first name input', function () {
  322. cy.get('@form').within(() => {
  323. cy.findByLabelText(/first name/i)
  324. })
  325. })
  326. it('renders last name input', function () {
  327. cy.get('@form').within(() => {
  328. cy.findByLabelText(/last name/i)
  329. })
  330. })
  331. describe('submit button', function () {
  332. it('renders trial button', function () {
  333. cy.get('@form').within(() => {
  334. cy.findByRole('button', { name: /upgrade now, pay after \d+ days/i })
  335. })
  336. })
  337. it('renders non-trial button', function () {
  338. cy.window().then(win => {
  339. const clone = cloneDeep(defaultSubscription)
  340. clone.items.plan!.trial = undefined
  341. win.recurly = createFakeRecurly(clone)
  342. })
  343. cy.mount(<CheckoutPanelWithPaymentProvider />)
  344. cy.findByTestId('checkout-form').within(() => {
  345. cy.findByRole('button', { name: 'Upgrade Now' })
  346. })
  347. })
  348. it('handles the disabled state of submit button', function () {
  349. cy.get('@form').within(() => {
  350. cy.findByRole('button', { name: /upgrade now/i }).should(
  351. 'be.disabled'
  352. )
  353. fillForm()
  354. cy.findByRole('button', { name: /upgrade now/i }).should(
  355. 'not.be.disabled'
  356. )
  357. })
  358. })
  359. })
  360. })
  361. describe('paypal payments', function () {
  362. beforeEach(function () {
  363. cy.findByLabelText(/paypal/i).click()
  364. })
  365. it('should not render card element', function () {
  366. cy.get('@form').within(() => {
  367. cy.findByLabelText(/card details/i).should('not.exist')
  368. })
  369. })
  370. it('should not render first name input', function () {
  371. cy.get('@form').within(() => {
  372. cy.findByLabelText(/first name/i).should('not.exist')
  373. })
  374. })
  375. it('should not render last name input', function () {
  376. cy.get('@form').within(() => {
  377. cy.findByLabelText(/last name/i).should('not.exist')
  378. })
  379. })
  380. it('renders proceeding to PayPal notice', function () {
  381. cy.get('@form').within(() => {
  382. cy.contains(
  383. /proceeding to PayPal will take you to the PayPal site to pay for your subscription/i
  384. )
  385. })
  386. })
  387. it('handles the disabled state of submit button', function () {
  388. cy.get('@form').within(() => {
  389. cy.findByRole('button', { name: /proceed to paypal/i }).should(
  390. 'be.disabled'
  391. )
  392. cy.findByLabelText(/country/i).select('Bulgaria')
  393. cy.findByRole('button', { name: /proceed to paypal/i }).should(
  394. 'not.be.disabled'
  395. )
  396. })
  397. })
  398. })
  399. })