CheckTest.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const { expect } = require('chai')
  2. const request = require('./helpers/request')
  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: [{ index: 0, suggestions: ['anther', 'another'] }],
  25. })
  26. })
  27. })
  28. describe('when multiple words are submitted', function () {
  29. beforeEach(async function () {
  30. response = await checkWord(['anather', 'anather', 'theorie'])
  31. })
  32. it('should return the misspellings for all the words', async function () {
  33. const body = JSON.parse(response.body)
  34. expect(body.misspellings.length).to.equal(3)
  35. })
  36. it('should have misspelling suggestions with consecutive indexes', async function () {
  37. const body = JSON.parse(response.body)
  38. const indexes = body.misspellings.map(mspl => mspl.index)
  39. expect(indexes).to.deep.equal([0, 1, 2])
  40. })
  41. it('should return identical suggestions for the same entry', async function () {
  42. const body = JSON.parse(response.body)
  43. expect(body.misspellings[0].suggestions).to.deep.equal(
  44. body.misspellings[1].suggestions
  45. )
  46. })
  47. })
  48. describe('when a very long list of words if submitted', function () {
  49. beforeEach(async function () {
  50. const words = []
  51. for (let i = 0; i <= 20000; i++) {
  52. words.push('anather')
  53. }
  54. response = await checkWord(words)
  55. })
  56. it('should return misspellings for the first 10K results only', async function () {
  57. const body = JSON.parse(response.body)
  58. expect(body.misspellings.length).to.equal(10000)
  59. })
  60. it('should have misspelling suggestions with consecutive indexes', async function () {
  61. const body = JSON.parse(response.body)
  62. const indexList = body.misspellings.map(mspl => mspl.index)
  63. expect(indexList.length).to.equal(10000) // avoid testing over an incorrect array
  64. for (let i = 0; i < indexList.length - 1; i++) {
  65. expect(indexList[i] + 1).to.equal(indexList[i + 1])
  66. }
  67. })
  68. })
  69. describe('when a very long list of words with utf8 responses', function () {
  70. beforeEach(async function () {
  71. const words = []
  72. for (let i = 0; i <= 20000; i++) {
  73. words.push('anéther')
  74. }
  75. response = await checkWord(words, 'bg') // use Bulgarian to generate utf8 response
  76. })
  77. it('should return misspellings for the first 10K results only', async function () {
  78. const body = JSON.parse(response.body)
  79. expect(body.misspellings.length).to.equal(10000)
  80. })
  81. it('should have misspelling suggestions with consecutive indexes', async function () {
  82. const body = JSON.parse(response.body)
  83. const indexList = body.misspellings.map(mspl => mspl.index)
  84. expect(indexList.length).to.equal(10000) // avoid testing over an incorrect array
  85. for (let i = 0; i < indexList.length - 1; i++) {
  86. expect(indexList[i] + 1).to.equal(indexList[i + 1])
  87. }
  88. })
  89. })
  90. describe('when multiple words with utf8 are submitted', function () {
  91. beforeEach(async function () {
  92. response = await checkWord(['mneá', 'meniésn', 'meônoi', 'mneá'], 'pt_BR')
  93. })
  94. it('should return the misspellings for all the words', async function () {
  95. const body = JSON.parse(response.body)
  96. expect(body.misspellings.length).to.equal(4)
  97. })
  98. it('should have misspelling suggestions with consecutive indexes', async function () {
  99. const body = JSON.parse(response.body)
  100. const indexes = body.misspellings.map(mspl => mspl.index)
  101. expect(indexes).to.deep.equal([0, 1, 2, 3])
  102. })
  103. it('should return identical suggestions for the same entry', async function () {
  104. const body = JSON.parse(response.body)
  105. expect(body.misspellings[0].suggestions).to.deep.equal(
  106. body.misspellings[3].suggestions
  107. )
  108. })
  109. })
  110. })