CheckTest.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { expect } from 'chai'
  2. import * as request from './helpers/request.js'
  3. const USER_ID = 101
  4. const checkWord = (words, language) =>
  5. request.post({
  6. url: `/user/${USER_ID}/check`,
  7. body: JSON.stringify({
  8. words,
  9. language,
  10. }),
  11. })
  12. describe('checking words', function () {
  13. let response
  14. describe('on successful response', function () {
  15. beforeEach(async function () {
  16. response = await checkWord(['anather'])
  17. })
  18. it('should return status 200', async function () {
  19. expect(response.statusCode).to.equal(200)
  20. })
  21. it('should return the list of misspellings', async function () {
  22. const body = JSON.parse(response.body)
  23. expect(body).to.deep.equal({
  24. misspellings: [
  25. {
  26. index: 0,
  27. suggestions: [
  28. 'anther',
  29. 'another',
  30. 'anthers',
  31. 'panther',
  32. 'anathema',
  33. 'anthem',
  34. 'nether',
  35. "anther's",
  36. 'ante',
  37. 'neither',
  38. 'norther',
  39. 'ether',
  40. 'other',
  41. ],
  42. },
  43. ],
  44. })
  45. })
  46. })
  47. describe('when multiple words are submitted', function () {
  48. beforeEach(async function () {
  49. response = await checkWord(['anather', 'anather', 'theorie'])
  50. })
  51. it('should return the misspellings for all the words', async function () {
  52. const body = JSON.parse(response.body)
  53. expect(body.misspellings.length).to.equal(3)
  54. })
  55. it('should have misspelling suggestions with consecutive indexes', async function () {
  56. const body = JSON.parse(response.body)
  57. const indexes = body.misspellings.map(mspl => mspl.index)
  58. expect(indexes).to.deep.equal([0, 1, 2])
  59. })
  60. it('should return identical suggestions for the same entry', async function () {
  61. const body = JSON.parse(response.body)
  62. expect(body.misspellings[0].suggestions).to.deep.equal(
  63. body.misspellings[1].suggestions
  64. )
  65. })
  66. })
  67. describe('when a very long list of words if submitted', function () {
  68. beforeEach(async function () {
  69. const words = []
  70. for (let i = 0; i <= 20000; i++) {
  71. words.push('anather')
  72. }
  73. response = await checkWord(words)
  74. })
  75. it('should return misspellings for the first 10K results only', async function () {
  76. const body = JSON.parse(response.body)
  77. expect(body.misspellings.length).to.equal(10000)
  78. })
  79. it('should have misspelling suggestions with consecutive indexes', async function () {
  80. const body = JSON.parse(response.body)
  81. const indexList = body.misspellings.map(mspl => mspl.index)
  82. expect(indexList.length).to.equal(10000) // avoid testing over an incorrect array
  83. for (let i = 0; i < indexList.length - 1; i++) {
  84. expect(indexList[i] + 1).to.equal(indexList[i + 1])
  85. }
  86. })
  87. })
  88. describe('when a very long list of words with utf8 responses', function () {
  89. beforeEach(async function () {
  90. const words = []
  91. for (let i = 0; i <= 20000; i++) {
  92. words.push('anéther')
  93. }
  94. response = await checkWord(words, 'bg') // use Bulgarian to generate utf8 response
  95. })
  96. it('should return misspellings for the first 10K results only', async function () {
  97. const body = JSON.parse(response.body)
  98. expect(body.misspellings.length).to.equal(10000)
  99. })
  100. it('should have misspelling suggestions with consecutive indexes', async function () {
  101. const body = JSON.parse(response.body)
  102. const indexList = body.misspellings.map(mspl => mspl.index)
  103. expect(indexList.length).to.equal(10000) // avoid testing over an incorrect array
  104. for (let i = 0; i < indexList.length - 1; i++) {
  105. expect(indexList[i] + 1).to.equal(indexList[i + 1])
  106. }
  107. })
  108. })
  109. describe('when multiple words with utf8 are submitted', function () {
  110. beforeEach(async function () {
  111. response = await checkWord(['mneá', 'meniésn', 'meônoi', 'mneá'], 'pt_BR')
  112. })
  113. it('should return the misspellings for all the words', async function () {
  114. const body = JSON.parse(response.body)
  115. expect(body.misspellings.length).to.equal(4)
  116. })
  117. it('should have misspelling suggestions with consecutive indexes', async function () {
  118. const body = JSON.parse(response.body)
  119. const indexes = body.misspellings.map(mspl => mspl.index)
  120. expect(indexes).to.deep.equal([0, 1, 2, 3])
  121. })
  122. it('should return identical suggestions for the same entry', async function () {
  123. const body = JSON.parse(response.body)
  124. expect(body.misspellings[0].suggestions).to.deep.equal(
  125. body.misspellings[3].suggestions
  126. )
  127. })
  128. })
  129. })