LearnTest.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const { expect } = require('chai')
  2. const request = require('./helpers/request')
  3. const USER_ID = 101
  4. const checkWord = (words) =>
  5. request.post({
  6. url: `/user/${USER_ID}/check`,
  7. body: JSON.stringify({
  8. words
  9. })
  10. })
  11. const learnWord = (word) =>
  12. request.post({
  13. url: `/user/${USER_ID}/learn`,
  14. body: JSON.stringify({
  15. word
  16. })
  17. })
  18. const unlearnWord = (word) =>
  19. request.post({
  20. url: `/user/${USER_ID}/unlearn`,
  21. body: JSON.stringify({
  22. word
  23. })
  24. })
  25. const getDict = () =>
  26. request.get({
  27. url: `/user/${USER_ID}`
  28. })
  29. const deleteDict = () =>
  30. request.del({
  31. url: `/user/${USER_ID}`
  32. })
  33. describe('learning words', function () {
  34. afterEach(async function () {
  35. await deleteDict()
  36. })
  37. it('should return status 204 when posting a word successfully', async function () {
  38. const response = await learnWord('abcd')
  39. expect(response.statusCode).to.equal(204)
  40. })
  41. it('should not learn the same word twice', async function () {
  42. await learnWord('foobar')
  43. const learnResponse = await learnWord('foobar')
  44. expect(learnResponse.statusCode).to.equal(204)
  45. const dictResponse = await getDict()
  46. const responseBody = JSON.parse(dictResponse.body)
  47. // the response from getlearnedwords filters out duplicates, so this test
  48. // can succeed even if the word is stored twice in the database
  49. expect(responseBody.length).to.equals(1)
  50. })
  51. it('should return no misspellings after a word is learnt', async function () {
  52. const response = await checkWord(['abv'])
  53. const responseBody = JSON.parse(response.body)
  54. expect(responseBody.misspellings.length).to.equals(1)
  55. await learnWord('abv')
  56. const response2 = await checkWord(['abv'])
  57. const responseBody2 = JSON.parse(response2.body)
  58. expect(responseBody2.misspellings.length).to.equals(0)
  59. })
  60. it('should return misspellings again after a personal dictionary is deleted', async function () {
  61. await learnWord('bvc')
  62. await deleteDict()
  63. const response = await checkWord(['bvc'])
  64. const responseBody = JSON.parse(response.body)
  65. expect(responseBody.misspellings.length).to.equals(1)
  66. })
  67. })
  68. describe('unlearning words', function () {
  69. it('should return status 204 when posting a word successfully', async function () {
  70. const response = await unlearnWord('anything')
  71. expect(response.statusCode).to.equal(204)
  72. })
  73. it('should return misspellings after a word is unlearnt', async function () {
  74. await learnWord('abv')
  75. const response = await checkWord(['abv'])
  76. const responseBody = JSON.parse(response.body)
  77. expect(responseBody.misspellings.length).to.equals(0)
  78. await unlearnWord('abv')
  79. const response2 = await checkWord(['abv'])
  80. const responseBody2 = JSON.parse(response2.body)
  81. expect(responseBody2.misspellings.length).to.equals(1)
  82. })
  83. })