actions-word-count.test.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { screen, waitFor } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import fetchMock from 'fetch-mock'
  4. import ActionsWordCount from '../../../../../frontend/js/features/editor-left-menu/components/actions-word-count'
  5. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  6. describe('<ActionsWordCount />', function () {
  7. afterEach(function () {
  8. fetchMock.removeRoutes().clearHistory()
  9. })
  10. it('shows correct modal when clicked after document is compiled', async function () {
  11. const compileEndpoint = 'express:/project/:projectId/compile'
  12. const wordcountEndpoint = 'express:/project/:projectId/wordcount'
  13. fetchMock.post(compileEndpoint, {
  14. status: 'success',
  15. pdfDownloadDomain: 'https://clsi.test-overleaf.com',
  16. outputFiles: [
  17. {
  18. path: 'output.pdf',
  19. build: 'build-123',
  20. url: '/build/build-123/output.pdf',
  21. type: 'pdf',
  22. },
  23. ],
  24. })
  25. fetchMock.get(wordcountEndpoint, {
  26. texcount: {
  27. encode: 'ascii',
  28. textWords: 0,
  29. headers: 0,
  30. mathInline: 0,
  31. mathDisplay: 0,
  32. },
  33. })
  34. renderWithEditorContext(<ActionsWordCount />, {
  35. projectId: '123abc',
  36. mockCompileOnLoad: true,
  37. scope: {
  38. editor: {
  39. sharejs_doc: {
  40. doc_id: 'test-doc',
  41. getSnapshot: () => 'some doc content',
  42. },
  43. },
  44. },
  45. })
  46. // when loading, we don't render the "Word Count" as button yet
  47. expect(screen.queryByRole('button', { name: 'Word Count' })).to.equal(null)
  48. await waitFor(
  49. () => expect(fetchMock.callHistory.called(compileEndpoint)).to.be.true
  50. )
  51. const button = await screen.findByRole('button', { name: 'Word Count' })
  52. button.click()
  53. await waitFor(
  54. () => expect(fetchMock.callHistory.called(wordcountEndpoint)).to.be.true
  55. )
  56. })
  57. })