join-group.test.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { fireEvent, render, screen, waitFor } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import JoinGroup from '../../../../../../frontend/js/features/subscription/components/group-invite/join-group'
  4. import fetchMock from 'fetch-mock'
  5. import sinon from 'sinon'
  6. describe('join group', function () {
  7. const inviteToken = 'token123'
  8. beforeEach(function () {
  9. window.metaAttributesCache.set('ol-inviteToken', inviteToken)
  10. })
  11. afterEach(function () {
  12. fetchMock.removeRoutes().clearHistory()
  13. })
  14. it('shows option to join subscription', async function () {
  15. render(<JoinGroup setView={() => {}} />)
  16. await screen.findByText(
  17. 'Please click the button below to join the group subscription and enjoy the benefits of an upgraded Overleaf account'
  18. )
  19. screen.getByRole('link', { name: 'Not now' })
  20. screen.getByRole('button', { name: 'Accept invitation' })
  21. })
  22. it('handles success when accepting invite', async function () {
  23. fetchMock.put(`/subscription/invites/${inviteToken}`, 200)
  24. const setView = sinon.stub()
  25. render(<JoinGroup setView={setView} />)
  26. const button = await screen.getByRole('button', {
  27. name: 'Accept invitation',
  28. })
  29. fireEvent.click(button)
  30. await waitFor(() => {
  31. expect(setView).to.have.been.calledOnce
  32. })
  33. })
  34. it('handles errors when accepting invite', async function () {
  35. render(<JoinGroup setView={() => {}} />)
  36. const button = await screen.getByRole('button', {
  37. name: 'Accept invitation',
  38. })
  39. fireEvent.click(button)
  40. await waitFor(() => {
  41. screen.getByText('Sorry, something went wrong')
  42. })
  43. })
  44. })