word-count-modal.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { screen } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import sinon from 'sinon'
  4. import fetchMock from 'fetch-mock'
  5. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  6. import WordCountModal from '../../../../../frontend/js/features/word-count-modal/components/word-count-modal'
  7. describe('<WordCountModal />', function () {
  8. afterEach(function () {
  9. fetchMock.reset()
  10. })
  11. const contextProps = {
  12. projectId: 'project-1',
  13. clsiServerId: 'clsi-server-1',
  14. }
  15. it('renders the translated modal title', async function () {
  16. const handleHide = sinon.stub()
  17. renderWithEditorContext(
  18. <WordCountModal show handleHide={handleHide} />,
  19. contextProps
  20. )
  21. await screen.findByText('Word Count')
  22. })
  23. it('renders a loading message when loading', async function () {
  24. fetchMock.get('express:/project/:projectId/wordcount', () => {
  25. return { status: 200, body: { texcount: { messages: 'This is a test' } } }
  26. })
  27. const handleHide = sinon.stub()
  28. renderWithEditorContext(
  29. <WordCountModal show handleHide={handleHide} />,
  30. contextProps
  31. )
  32. await screen.findByText('Loading…')
  33. await screen.findByText('This is a test')
  34. })
  35. it('renders an error message and hides loading message on error', async function () {
  36. fetchMock.get('express:/project/:projectId/wordcount', 500)
  37. const handleHide = sinon.stub()
  38. renderWithEditorContext(
  39. <WordCountModal show handleHide={handleHide} />,
  40. contextProps
  41. )
  42. await screen.findByText('Sorry, something went wrong')
  43. expect(screen.queryByText(/Loading/)).to.not.exist
  44. })
  45. it('displays messages', async function () {
  46. fetchMock.get('express:/project/:projectId/wordcount', () => {
  47. return {
  48. status: 200,
  49. body: {
  50. texcount: {
  51. messages: 'This is a test',
  52. },
  53. },
  54. }
  55. })
  56. const handleHide = sinon.stub()
  57. renderWithEditorContext(
  58. <WordCountModal show handleHide={handleHide} />,
  59. contextProps
  60. )
  61. await screen.findByText('This is a test')
  62. })
  63. it('displays counts data', async function () {
  64. fetchMock.get('express:/project/:projectId/wordcount', () => {
  65. return {
  66. status: 200,
  67. body: {
  68. texcount: {
  69. textWords: 100,
  70. mathDisplay: 200,
  71. mathInline: 300,
  72. headers: 400,
  73. },
  74. },
  75. }
  76. })
  77. const handleHide = sinon.stub()
  78. renderWithEditorContext(
  79. <WordCountModal show handleHide={handleHide} />,
  80. contextProps
  81. )
  82. await screen.findByText((content, element) =>
  83. element.textContent.trim().match(/^Total Words\s*:\s*100$/)
  84. )
  85. await screen.findByText((content, element) =>
  86. element.textContent.trim().match(/^Math Display\s*:\s*200$/)
  87. )
  88. await screen.findByText((content, element) =>
  89. element.textContent.trim().match(/^Math Inline\s*:\s*300$/)
  90. )
  91. await screen.findByText((content, element) =>
  92. element.textContent.trim().match(/^Headers\s*:\s*400$/)
  93. )
  94. })
  95. })