LearnTest.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 deleteDict = () =>
  26. request.del({
  27. url: `/user/${USER_ID}`
  28. })
  29. describe('learning words', () => {
  30. it('should return status 200 when posting a word successfully', async () => {
  31. const response = await learnWord('abcd')
  32. expect(response.statusCode).to.equal(200)
  33. })
  34. it('should return no misspellings after a word is learnt', async () => {
  35. const response = await checkWord(['abv'])
  36. const responseBody = JSON.parse(response.body)
  37. expect(responseBody.misspellings.length).to.equals(1)
  38. await learnWord('abv')
  39. const response2 = await checkWord(['abv'])
  40. const responseBody2 = JSON.parse(response2.body)
  41. expect(responseBody2.misspellings.length).to.equals(0)
  42. })
  43. it('should return misspellings again after a personal dictionary is deleted', async () => {
  44. await learnWord('bvc')
  45. await deleteDict()
  46. const response = await checkWord(['bvc'])
  47. const responseBody = JSON.parse(response.body)
  48. expect(responseBody.misspellings.length).to.equals(1)
  49. })
  50. })
  51. describe('unlearning words', () => {
  52. it('should return status 200 when posting a word successfully', async () => {
  53. const response = await unlearnWord('anything')
  54. expect(response.statusCode).to.equal(200)
  55. })
  56. it('should return misspellings after a word is unlearnt', async () => {
  57. await learnWord('abv')
  58. const response = await checkWord(['abv'])
  59. const responseBody = JSON.parse(response.body)
  60. expect(responseBody.misspellings.length).to.equals(0)
  61. await unlearnWord('abv')
  62. const response2 = await checkWord(['abv'])
  63. const responseBody2 = JSON.parse(response2.body)
  64. expect(responseBody2.misspellings.length).to.equals(1)
  65. })
  66. })