Hey everyone I am trying to solve a problem statement using Javascript which has a simple button to add movie to favourites in its onclick. The function should throw a Promise.reject()
if the movie has already been added once in the favourites.
So there are some test cases for this addFavourite()
and some of the test cases are failing. Can you let me know why ? I can't change the test files so what should I change in my code to resolve the error ?
This is my addFavourite()
function. (Note: allMovies is a list of all movie objects):
function addFavourite(searchId) {
var favMovie = [];
allMovies.forEach((iterator) => {
if (iterator.id == searchId) {
favMovie = iterator;
}
});
var movieRepeated = false;
allFavourites.forEach((iterator) => {
if (iterator.id == favMovie.id) {
movieRepeated = true;
}
});
if (movieRepeated) {
return Promise.reject(new Error("Movie repeated!")).then(
resolved,
rejected
);
} else {
fetch("http://localhost:3000/favourites", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify(favMovie),
})
.then((response) => {
if (response.ok) {
return response.json();
}
})
.then((addedMovie) => {
allFavourites.push(addedMovie);
displayFavs(allFavourites);
return allFavourites;
});
}
}
These are the testcases that are failing:
1.
it('adding of same movie as favourite twice shall be restricted', (done) => {
fetchMock.get('http://localhost:3000/movies', moviesTestData);
fetchMock.get('http://localhost:3000/favourites', favouritesTestData);
fetchMock.post('http://localhost:3000/favourites', moviesTestData[0]);
script.getMovies()
.then(() => {
return script.getFavourites();
})
.then(() => {
return script.addFavourite(476307);
})
.catch((err) => {
expect(err).to.not.equal(null, err);
expect(err.message).to.equal('Movie is already added to favourites');
done();
});
});
it(`addFavourites() shall hit the correct api with correct data and
return correct response`, (done) => {
fetchMock.get('http://localhost:3000/movies', moviesTestData);
fetchMock.get('http://localhost:3000/favourites', favouritesTestData);
fetchMock.post('http://localhost:3000/favourites', moviesTestData[0]);
script.getMovies()
.then(() => {
return script.getFavourites();
})
.then(() => {
return script.addFavourite(476307);
})
.then((res) => {
const lastCallArgs = fetchMock.lastCall();
expect(lastCallArgs[0]).to.equal('http://localhost:3000/favourites');
expect(lastCallArgs[1].method).to.equal('POST');
expect(lastCallArgs[1].body).to.equal(JSON.stringify(moviesTestData[0]));
favouritesTestData.push(moviesTestData[0]);
expect(res).to.deep.equal(favouritesTestData);
expect(document.getElementById('favouritesList').innerHTML)
.to.include('The Unique Lama');
done();
})
.catch((err) => {
expect(err).to.equal(null, err);
done();
});
});
Here is the error that I get:
addFavourites() shall hit the correct api with correct data and return correct response:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)