This repository has been archived on 2023-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
freecodecamp-projects/7-quality-assurance/5-american-british-translator/tests/2_functional-tests.js

95 lines
2.7 KiB
JavaScript

const chai = require("chai");
const chaiHttp = require("chai-http");
const assert = chai.assert;
const server = require("../server.js");
chai.use(chaiHttp);
suite("Functional Tests", () => {
test("Translation with text and locale fields: POST request to /api/translate", (done) => {
chai
.request(server)
.post("/api/translate")
.send({
locale: "american-to-british",
text: "Mangoes are my favorite fruit.",
})
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.text, "Mangoes are my favorite fruit.");
assert.equal(
res.body.translation,
'Mangoes are my <span class="highlight">favourite</span> fruit.'
);
});
done();
});
test("Translation with text and invalid locale field: POST request to /api/translate", (done) => {
chai
.request(server)
.post("/api/translate")
.send({
locale: "american-to-nyanya",
text: "Mangoes are my favorite fruit.",
})
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "Invalid value for locale field");
});
done();
});
test("Translation with missing text field: POST request to /api/translate", (done) => {
chai
.request(server)
.post("/api/translate")
.send({
locale: "american-to-british",
})
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "Required field(s) missing");
});
done();
});
test("Translation with missing locale field: POST request to /api/translate", (done) => {
chai
.request(server)
.post("/api/translate")
.send({
text: "Mangoes are my favorite fruit.",
})
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "Required field(s) missing");
});
done();
});
test("Translation with empty text: POST request to /api/translate", (done) => {
chai
.request(server)
.post("/api/translate")
.send({
locale: "american-to-british",
text: "",
})
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "No text to translate");
});
done();
});
test("Translation with text that needs no translation: POST request to /api/translate", (done) => {
chai
.request(server)
.post("/api/translate")
.send({
locale: "british-to-american",
text: "Mangoes are my favorite fruit.",
})
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.translation, "Everything looks good to me!");
});
done();
});
});