checkout.spec.tsx 14 KB

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