integration-widget.test.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { screen, fireEvent, render } from '@testing-library/react'
  4. import { IntegrationLinkingWidget } from '../../../../../../frontend/js/features/settings/components/linking/integration-widget'
  5. import * as eventTracking from '@/infrastructure/event-tracking'
  6. describe('<IntegrationLinkingWidgetTest/>', function () {
  7. const defaultProps = {
  8. logo: <div />,
  9. title: 'Integration',
  10. description: 'paragraph1',
  11. helpPath: '/learn',
  12. linkPath: '/link',
  13. unlinkPath: '/unlink',
  14. unlinkConfirmationTitle: 'confirm unlink',
  15. unlinkConfirmationText: 'you will be unlinked',
  16. }
  17. describe('when the feature is not available', function () {
  18. let sendMBSpy: sinon.SinonSpy
  19. beforeEach(function () {
  20. sendMBSpy = sinon.spy(eventTracking, 'sendMB')
  21. render(<IntegrationLinkingWidget {...defaultProps} hasFeature={false} />)
  22. })
  23. afterEach(function () {
  24. sendMBSpy.restore()
  25. })
  26. it("should render 'Premium feature' label", function () {
  27. screen.getByText('Premium feature')
  28. })
  29. it('should render an upgrade link and track clicks', function () {
  30. const upgradeLink = screen.getByRole('link', { name: 'Upgrade' })
  31. expect(upgradeLink.getAttribute('href')).to.equal(
  32. '/user/subscription/plans'
  33. )
  34. fireEvent.click(upgradeLink)
  35. expect(sendMBSpy).to.be.calledOnce
  36. expect(sendMBSpy).calledWith('settings-upgrade-click')
  37. })
  38. })
  39. describe('when the integration is not linked', function () {
  40. beforeEach(function () {
  41. render(
  42. <IntegrationLinkingWidget {...defaultProps} hasFeature linked={false} />
  43. )
  44. })
  45. it('should render a link to initiate integration linking', function () {
  46. expect(
  47. screen.getByRole('link', { name: 'Link' }).getAttribute('href')
  48. ).to.equal('/link')
  49. })
  50. it("should not render 'premium feature' labels", function () {
  51. expect(screen.queryByText('premium_feature')).to.not.exist
  52. expect(screen.queryByText('integration_is_a_premium_feature')).to.not
  53. .exist
  54. })
  55. })
  56. describe('when the integration is linked', function () {
  57. beforeEach(function () {
  58. render(
  59. <IntegrationLinkingWidget
  60. {...defaultProps}
  61. hasFeature
  62. linked
  63. statusIndicator={<div>status indicator</div>}
  64. />
  65. )
  66. })
  67. it('should render a status indicator', function () {
  68. screen.getByText('status indicator')
  69. })
  70. it("should not render 'premium feature' labels", function () {
  71. expect(screen.queryByText('premium_feature')).to.not.exist
  72. expect(screen.queryByText('integration_is_a_premium_feature')).to.not
  73. .exist
  74. })
  75. it('should display an `unlink` button', function () {
  76. screen.getByRole('button', { name: 'Unlink' })
  77. })
  78. it('should open a modal with a link to confirm integration unlinking', function () {
  79. fireEvent.click(screen.getByRole('button', { name: 'Unlink' }))
  80. screen.getByText('confirm unlink')
  81. screen.getByText('you will be unlinked')
  82. screen.getByRole('button', { name: 'Cancel' })
  83. screen.getByRole('button', { name: 'Unlink' })
  84. })
  85. it('should cancel unlinking when clicking "cancel" in the confirmation modal', async function () {
  86. fireEvent.click(screen.getByRole('button', { name: 'Unlink' }))
  87. screen.getByText('confirm unlink')
  88. const cancelBtn = screen.getByRole('button', {
  89. name: 'Cancel',
  90. hidden: false,
  91. })
  92. fireEvent.click(cancelBtn)
  93. await screen.findByRole('button', { name: 'Cancel', hidden: true })
  94. })
  95. })
  96. })