From 797691ac0fa970ec08740c7c3821c2be0c778c36 Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Fri, 3 Jun 2022 12:29:26 -0500 Subject: [PATCH] finished functional tests --- tests/2_functional-tests.js | 144 ++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/tests/2_functional-tests.js b/tests/2_functional-tests.js index 0cd5855..fb05667 100644 --- a/tests/2_functional-tests.js +++ b/tests/2_functional-tests.js @@ -9,4 +9,148 @@ let Translator = require('../components/translator.js'); suite('Functional Tests', () => { + test('/api/translate Translate with text and locale fields', done => { + + const locale = 'american-to-british'; + const text = 'Mangoes are my favorite fruit.'; + const translation = 'Mangoes are my favourite fruit.'; + + chai.request(server) + .post('/api/translate') + .set('Content-Type', 'application/x-www-form-urlencoded') + .type('form') + .send(`locale=${locale}`) + .send(`text=${text}`) + .end((err, res) => { + + if (err) console.log(err); + + assert.equal(res.status, 200); + assert.isObject(res.body); + assert.propertyVal(res.body, 'text', text); + assert.propertyVal(res.body, 'translation', translation); + + done(); + + }); + + }); + + test('/api/translate Translate with text and invalid locale field', done => { + + const locale = 'french-to-american'; + const text = 'SaintPeter and nhcarrigan give their regards!'; + const translation = 'Everything looks good to me!'; + + chai.request(server) + .post('/api/translate') + .set('Content-Type', 'application/x-www-form-urlencoded') + .type('form') + .send(`locale=${locale}`) + .send(`text=${text}`) + .end((err, res) => { + + if (err) console.log(err); + + assert.equal(res.status, 200); + assert.isObject(res.body); + assert.propertyVal(res.body, 'error', 'Invalid value for locale field'); + + done(); + + }); + + }); + + test('/api/translate Translate with missing text field', done => { + + chai.request(server) + .post('/api/translate') + .set('Content-Type', 'application/x-www-form-urlencoded') + .type('form') + .send('locale=american-to-british') + .end((err, res) => { + + if (err) console.log(err); + + assert.equal(res.status, 200); + assert.isObject(res.body); + assert.propertyVal(res.body, 'error', 'Required field(s) missing'); + + done(); + + }); + + }); + + test('/api/translate Translate with missing locale field', done => { + + chai.request(server) + .post('/api/translate') + .set('Content-Type', 'application/x-www-form-urlencoded') + .type('form') + .send('text=hello') + .end((err, res) => { + + if (err) console.log(err); + + assert.equal(res.status, 200); + assert.isObject(res.body); + assert.propertyVal(res.body, 'error', 'Required field(s) missing'); + + done(); + + }); + + }); + + test('/api/translate Translate with empty text', done => { + + chai.request(server) + .post('/api/translate') + .set('Content-Type', 'application/x-www-form-urlencoded') + .type('form') + .send('locale=american-to-british') + .send('text=') + .end((err, res) => { + + if (err) console.log(err); + + assert.equal(res.status, 200); + assert.isObject(res.body); + assert.propertyVal(res.body, 'error', 'No text to translate'); + + done(); + + }); + + }); + + test('/api/translate Translate with text that needs no translation', done => { + + const locale = 'british-to-american'; + const text = 'SaintPeter and nhcarrigan give their regards!'; + const translation = 'Everything looks good to me!'; + + chai.request(server) + .post('/api/translate') + .set('Content-Type', 'application/x-www-form-urlencoded') + .type('form') + .send(`locale=${locale}`) + .send(`text=${text}`) + .end((err, res) => { + + if (err) console.log(err); + + assert.equal(res.status, 200); + assert.isObject(res.body); + assert.propertyVal(res.body, 'text', text); + assert.propertyVal(res.body, 'translation', translation); + + done(); + + }); + + }); + }); \ No newline at end of file