Помощ за задача от JS Unit Tests
Задачата е BookSelection от JS Advanced Retake Exam - 6 Apr 2022
със expect.to equal няма проблем, но не ми минава тест със to.throw, пускам кода, блъскам си главата 2 дена и не минава по никакъв начин.
let expect = require('chai').expect;
const bookSelection = {
isGenreSuitable(genre, age) {
if (age <= 12 && (genre === "Thriller" || genre === "Horror")) {
return `Books with ${genre} genre are not suitable for kids at ${age} age`;
} else {
return `Those books are suitable`;
}
},
isItAffordable(price, budget) {
if (typeof price !== "number" || typeof budget !== "number") {
throw new Error("Invalid input");
}
let result = budget - price;
if (result < 0) {
return "You don't have enough money";
} else {
return `Book bought. You have ${result}$ left`;
}
},
suitableTitles(array, wantedGenre) {
let resultArr = [];
if (!Array.isArray(array) || typeof wantedGenre !== "string") {
throw new Error("Invalid input");
}
array.map((obj) => {
if (obj.genre === wantedGenre) {
resultArr.push(obj.title);
}
});
return resultArr;
},
};
describe("Tests …", function () {
describe("Is Genre Suitable", function () {
it("Check under 12", function () {
expect(bookSelection.isGenreSuitable('Thriller', 10)).to.equal(`Books with Thriller genre are not suitable for kids at 10 age`);
});
it("Check equal 12", function () {
expect(bookSelection.isGenreSuitable('Thriller', 12)).to.equal(`Books with Thriller genre are not suitable for kids at 12 age`);
});
it("Check over 12", function () {
expect(bookSelection.isGenreSuitable('Thriller', 13)).to.equal(`Those books are suitable`);
});
it("Check under 12", function () {
expect(bookSelection.isGenreSuitable('Horror', 10)).to.equal(`Books with Horror genre are not suitable for kids at 10 age`);
});
it("Check equal 12", function () {
expect(bookSelection.isGenreSuitable('Horror', 12)).to.equal(`Books with Horror genre are not suitable for kids at 12 age`);
});
it("Check over 12", function () {
expect(bookSelection.isGenreSuitable('Horror', 13)).to.equal(`Those books are suitable`);
});
});
describe("isItAffordable", function () {
it("Check for invalid input", function () {
expect(bookSelection.isItAffordable('xaxa', 'xaxa')).to.throw('Invalid input');// Toзи ред не минава
});
});
});