overleaf/services/spelling/test/acceptance/js/LearnTest.js

104 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

const { expect } = require('chai')
const request = require('./helpers/request')
const USER_ID = 101
2021-07-13 07:04:47 -04:00
const checkWord = words =>
request.post({
url: `/user/${USER_ID}/check`,
body: JSON.stringify({
2021-07-13 07:04:47 -04:00
words,
}),
})
2021-07-13 07:04:47 -04:00
const learnWord = word =>
request.post({
url: `/user/${USER_ID}/learn`,
body: JSON.stringify({
2021-07-13 07:04:47 -04:00
word,
}),
})
2021-07-13 07:04:47 -04:00
const unlearnWord = word =>
2019-07-20 09:04:08 -04:00
request.post({
url: `/user/${USER_ID}/unlearn`,
body: JSON.stringify({
2021-07-13 07:04:47 -04:00
word,
}),
2019-07-20 09:04:08 -04:00
})
2020-02-18 09:03:16 -05:00
const getDict = () =>
request.get({
2021-07-13 07:04:47 -04:00
url: `/user/${USER_ID}`,
2020-02-18 09:03:16 -05:00
})
const deleteDict = () =>
request.del({
2021-07-13 07:04:47 -04:00
url: `/user/${USER_ID}`,
})
2020-08-10 12:23:15 -04:00
describe('learning words', function () {
afterEach(async function () {
2020-02-18 09:03:16 -05:00
await deleteDict()
})
2020-08-10 12:23:15 -04:00
it('should return status 204 when posting a word successfully', async function () {
const response = await learnWord('abcd')
expect(response.statusCode).to.equal(204)
})
2020-08-10 12:23:15 -04:00
it('should not learn the same word twice', async function () {
2020-02-18 09:03:16 -05:00
await learnWord('foobar')
const learnResponse = await learnWord('foobar')
expect(learnResponse.statusCode).to.equal(204)
const dictResponse = await getDict()
const responseBody = JSON.parse(dictResponse.body)
2020-02-18 10:58:03 -05:00
// the response from getlearnedwords filters out duplicates, so this test
// can succeed even if the word is stored twice in the database
2020-02-18 09:03:16 -05:00
expect(responseBody.length).to.equals(1)
})
2020-08-10 12:23:15 -04:00
it('should return no misspellings after a word is learnt', async function () {
const response = await checkWord(['abv'])
const responseBody = JSON.parse(response.body)
expect(responseBody.misspellings.length).to.equals(1)
await learnWord('abv')
const response2 = await checkWord(['abv'])
const responseBody2 = JSON.parse(response2.body)
expect(responseBody2.misspellings.length).to.equals(0)
})
2020-08-10 12:23:15 -04:00
it('should return misspellings again after a personal dictionary is deleted', async function () {
await learnWord('bvc')
await deleteDict()
const response = await checkWord(['bvc'])
const responseBody = JSON.parse(response.body)
expect(responseBody.misspellings.length).to.equals(1)
})
})
2019-07-20 09:04:08 -04:00
2020-08-10 12:23:15 -04:00
describe('unlearning words', function () {
it('should return status 204 when posting a word successfully', async function () {
2019-07-20 09:04:08 -04:00
const response = await unlearnWord('anything')
expect(response.statusCode).to.equal(204)
2019-07-20 09:04:08 -04:00
})
2020-08-10 12:23:15 -04:00
it('should return misspellings after a word is unlearnt', async function () {
2019-07-20 09:04:08 -04:00
await learnWord('abv')
const response = await checkWord(['abv'])
const responseBody = JSON.parse(response.body)
expect(responseBody.misspellings.length).to.equals(0)
await unlearnWord('abv')
const response2 = await checkWord(['abv'])
const responseBody2 = JSON.parse(response2.body)
expect(responseBody2.misspellings.length).to.equals(1)
})
})