managed-publishers.test.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, {
  6. Publisher,
  7. } from '../../../../../../frontend/js/features/subscription/components/dashboard/managed-publishers'
  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 = new Map()
  25. window.metaAttributesCache.set('ol-managedPublishers', managedPublishers)
  26. window.user_id = userId
  27. })
  28. afterEach(function () {
  29. window.metaAttributesCache = new Map()
  30. delete window.user_id
  31. fetchMock.reset()
  32. })
  33. it('renders all managed publishers', function () {
  34. render(
  35. <SubscriptionDashboardProvider>
  36. <ManagedPublishers />
  37. </SubscriptionDashboardProvider>
  38. )
  39. const elements = screen.getAllByText('You are a', {
  40. exact: false,
  41. })
  42. expect(elements.length).to.equal(2)
  43. expect(elements[0].textContent).to.equal('You are a manager of Pub 1')
  44. expect(elements[1].textContent).to.equal('You are a manager of Pub 2')
  45. const links = screen.getAllByRole('link')
  46. expect(links[0].getAttribute('href')).to.equal('/publishers/pub-1/hub')
  47. expect(links[1].getAttribute('href')).to.equal(
  48. '/manage/publishers/pub-1/managers'
  49. )
  50. expect(links[2].getAttribute('href')).to.equal('/publishers/pub-2/hub')
  51. expect(links[3].getAttribute('href')).to.equal(
  52. '/manage/publishers/pub-2/managers'
  53. )
  54. })
  55. it('renders nothing when there are no publishers', function () {
  56. window.metaAttributesCache.set('ol-managedPublishers', undefined)
  57. render(
  58. <SubscriptionDashboardProvider>
  59. <ManagedPublishers />
  60. </SubscriptionDashboardProvider>
  61. )
  62. const elements = screen.queryAllByText('You are a', {
  63. exact: false,
  64. })
  65. expect(elements.length).to.equal(0)
  66. })
  67. })