word-count-modal.test.js 2.6 KB

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