actions-word-count.test.jsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.reset()
  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. scope: {
  37. editor: {
  38. sharejs_doc: {
  39. doc_id: 'test-doc',
  40. getSnapshot: () => 'some doc content',
  41. },
  42. },
  43. },
  44. })
  45. // when loading, we don't render the "Word Count" as button yet
  46. expect(screen.queryByRole('button', { name: 'Word Count' })).to.equal(null)
  47. await waitFor(() => expect(fetchMock.called(compileEndpoint)).to.be.true)
  48. const button = await screen.findByRole('button', { name: 'Word Count' })
  49. button.click()
  50. await waitFor(() => expect(fetchMock.called(wordcountEndpoint)).to.be.true)
  51. })
  52. })