word-count-modal.test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: {} } }
  25. })
  26. render(<WordCountModal {...modalProps} />)
  27. await screen.findByText('Loading…')
  28. })
  29. it('renders an error message and hides loading message on error', async function() {
  30. fetchMock.get('express:/project/:projectId/wordcount', 500)
  31. render(<WordCountModal {...modalProps} />)
  32. await screen.findByText('Sorry, something went wrong')
  33. expect(screen.queryByText(/Loading/)).to.not.exist
  34. })
  35. it('displays messages', async function() {
  36. fetchMock.get('express:/project/:projectId/wordcount', () => {
  37. return {
  38. status: 200,
  39. body: {
  40. texcount: {
  41. messages: 'This is a test'
  42. }
  43. }
  44. }
  45. })
  46. render(<WordCountModal {...modalProps} />)
  47. await screen.findByText('This is a test')
  48. })
  49. it('displays counts data', async function() {
  50. fetchMock.get('express:/project/:projectId/wordcount', () => {
  51. return {
  52. status: 200,
  53. body: {
  54. texcount: {
  55. textWords: 100,
  56. mathDisplay: 200,
  57. mathInline: 300,
  58. headers: 400
  59. }
  60. }
  61. }
  62. })
  63. render(<WordCountModal {...modalProps} />)
  64. await screen.findByText((content, element) =>
  65. element.textContent.trim().match(/^Total Words\s*:\s*100$/)
  66. )
  67. await screen.findByText((content, element) =>
  68. element.textContent.trim().match(/^Math Display\s*:\s*200$/)
  69. )
  70. await screen.findByText((content, element) =>
  71. element.textContent.trim().match(/^Math Inline\s*:\s*300$/)
  72. )
  73. await screen.findByText((content, element) =>
  74. element.textContent.trim().match(/^Headers\s*:\s*400$/)
  75. )
  76. })
  77. })