integration-widget.test.tsx 3.8 KB

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