LearnTest.mjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { expect } from 'chai'
  2. import cheerio from 'cheerio'
  3. import UserHelper from './helpers/User.mjs'
  4. const User = UserHelper.promises
  5. describe('Spelling', function () {
  6. let user, projectId
  7. async function learnWord(word) {
  8. const { response } = await user.doRequest('POST', {
  9. url: '/spelling/learn',
  10. json: { word },
  11. })
  12. return response
  13. }
  14. async function getDict() {
  15. const { body, response } = await user.doRequest(
  16. 'GET',
  17. `/project/${projectId}`
  18. )
  19. expect(response.statusCode).to.equal(200)
  20. const dom = cheerio.load(body)
  21. const metaEl = dom('meta[name="ol-learnedWords"]')[0]
  22. return JSON.parse(metaEl.attribs.content)
  23. }
  24. describe('learning words', function () {
  25. beforeEach(async function () {
  26. user = new User()
  27. await user.login()
  28. projectId = await user.createProject('foo')
  29. })
  30. it('should return status 400 when posting an empty word', async function () {
  31. const response = await learnWord('')
  32. expect(response.statusCode).to.equal(400)
  33. })
  34. it('should return status 204 when posting a word successfully', async function () {
  35. const response = await learnWord('abcd')
  36. expect(response.statusCode).to.equal(204)
  37. })
  38. it('should not learn the same word twice', async function () {
  39. await learnWord('foobar')
  40. const learnResponse = await learnWord('foobar')
  41. expect(learnResponse.statusCode).to.equal(204)
  42. const dict = await getDict()
  43. expect(dict.length).to.equals(1)
  44. })
  45. })
  46. })