word-count-modal.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. fetchMock.get('express:/project/:projectId/wordcount', () => {
  17. return { status: 200, body: { texcount: { messages: 'This is a test' } } }
  18. })
  19. const handleHide = sinon.stub()
  20. renderWithEditorContext(
  21. <WordCountModal show handleHide={handleHide} />,
  22. contextProps
  23. )
  24. await screen.findByText('Word Count')
  25. })
  26. it('renders a loading message when loading', async function () {
  27. fetchMock.get('express:/project/:projectId/wordcount', () => {
  28. return { status: 200, body: { texcount: { messages: 'This is a test' } } }
  29. })
  30. const handleHide = sinon.stub()
  31. renderWithEditorContext(
  32. <WordCountModal show handleHide={handleHide} />,
  33. contextProps
  34. )
  35. await screen.findByText('Loading…')
  36. await screen.findByText('This is a test')
  37. })
  38. it('renders an error message and hides loading message on error', async function () {
  39. fetchMock.get('express:/project/:projectId/wordcount', 500)
  40. const handleHide = sinon.stub()
  41. renderWithEditorContext(
  42. <WordCountModal show handleHide={handleHide} />,
  43. contextProps
  44. )
  45. await screen.findByText('Sorry, something went wrong')
  46. expect(screen.queryByText(/Loading/)).to.not.exist
  47. })
  48. it('displays messages', async function () {
  49. fetchMock.get('express:/project/:projectId/wordcount', () => {
  50. return {
  51. status: 200,
  52. body: {
  53. texcount: {
  54. messages: 'This is a test',
  55. },
  56. },
  57. }
  58. })
  59. const handleHide = sinon.stub()
  60. renderWithEditorContext(
  61. <WordCountModal show handleHide={handleHide} />,
  62. contextProps
  63. )
  64. await screen.findByText('This is a test')
  65. })
  66. it('displays counts data', async function () {
  67. fetchMock.get('express:/project/:projectId/wordcount', () => {
  68. return {
  69. status: 200,
  70. body: {
  71. texcount: {
  72. textWords: 100,
  73. mathDisplay: 200,
  74. mathInline: 300,
  75. headers: 400,
  76. },
  77. },
  78. }
  79. })
  80. const handleHide = sinon.stub()
  81. renderWithEditorContext(
  82. <WordCountModal show handleHide={handleHide} />,
  83. contextProps
  84. )
  85. await screen.findByText((content, element) =>
  86. element.textContent.trim().match(/^Total Words\s*:\s*100$/)
  87. )
  88. await screen.findByText((content, element) =>
  89. element.textContent.trim().match(/^Math Display\s*:\s*200$/)
  90. )
  91. await screen.findByText((content, element) =>
  92. element.textContent.trim().match(/^Math Inline\s*:\s*300$/)
  93. )
  94. await screen.findByText((content, element) =>
  95. element.textContent.trim().match(/^Headers\s*:\s*400$/)
  96. )
  97. })
  98. })