LearnTest.js 1.5 KB

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