I am trying to test a function that makes an AJAX request to an API. The value that the function sends to the API and the API's response are as expected. However, Jasmine keeps giving me Error: Expected Function to be "*insert random string*".
How can I run this test properly and without errors via browser standalone version of Jasmine?
This is the function I am trying to test:
function handleInput(data) {
var dataToSend = data;
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://test.app/controllers/handleinput.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onload = function () {
console.log(this.responseText);
return this.responseText;
};
xhr.send("input=" + dataToSend);
}
This is the specs script:
describe('ajax request', function () {
var randomString = Math.random().toString(36).substring(7);
beforeEach(function (done) {
handleInput(randomString)
done();
});
it('receives the same string that it sends', function (done) {
expect(handleInput).toBe(randomString);
done();
});
});
All I am trying to do is to verify that the function handleInput
sends a random string to the server and receives the same string as the response.