managed-publishers.test.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { expect } from 'chai'
  2. import { render, screen } from '@testing-library/react'
  3. import { SubscriptionDashboardProvider } from '../../../../../../frontend/js/features/subscription/context/subscription-dashboard-context'
  4. import fetchMock from 'fetch-mock'
  5. import ManagedPublishers from '../../../../../../frontend/js/features/subscription/components/dashboard/managed-publishers'
  6. import { SplitTestProvider } from '@/shared/context/split-test-context'
  7. import { Publisher } from '../../../../../../types/subscription/dashboard/publisher'
  8. const userId = 'fff999fff999'
  9. const publisher1 = {
  10. slug: 'pub-1',
  11. managerIds: [],
  12. name: 'Pub 1',
  13. partner: 'p1',
  14. }
  15. const publisher2 = {
  16. slug: 'pub-2',
  17. managerIds: [],
  18. name: 'Pub 2',
  19. partner: 'p2',
  20. }
  21. const managedPublishers: Publisher[] = [publisher1, publisher2]
  22. describe('<ManagedPublishers />', function () {
  23. beforeEach(function () {
  24. window.metaAttributesCache.set('ol-managedPublishers', managedPublishers)
  25. window.metaAttributesCache.set('ol-user_id', userId)
  26. })
  27. afterEach(function () {
  28. fetchMock.removeRoutes().clearHistory()
  29. })
  30. it('renders all managed publishers', function () {
  31. render(
  32. <SplitTestProvider>
  33. <SubscriptionDashboardProvider>
  34. <ManagedPublishers />
  35. </SubscriptionDashboardProvider>
  36. </SplitTestProvider>
  37. )
  38. const elements = screen.getAllByText('You are a', {
  39. exact: false,
  40. })
  41. expect(elements.length).to.equal(2)
  42. expect(elements[0].textContent).to.equal('You are a manager of Pub 1')
  43. expect(elements[1].textContent).to.equal('You are a manager of Pub 2')
  44. const links = screen.getAllByRole('link')
  45. expect(links[0].getAttribute('href')).to.equal('/publishers/pub-1/hub')
  46. expect(links[1].getAttribute('href')).to.equal(
  47. '/manage/publishers/pub-1/managers'
  48. )
  49. expect(links[2].getAttribute('href')).to.equal('/publishers/pub-2/hub')
  50. expect(links[3].getAttribute('href')).to.equal(
  51. '/manage/publishers/pub-2/managers'
  52. )
  53. })
  54. it('renders nothing when there are no publishers', function () {
  55. window.metaAttributesCache.set('ol-managedPublishers', undefined)
  56. render(
  57. <SplitTestProvider>
  58. <SubscriptionDashboardProvider>
  59. <ManagedPublishers />
  60. </SubscriptionDashboardProvider>
  61. </SplitTestProvider>
  62. )
  63. const elements = screen.queryAllByText('You are a', {
  64. exact: false,
  65. })
  66. expect(elements.length).to.equal(0)
  67. })
  68. })