I am writing unit tests for my website at work and I cant seem to figure out how to test an api call.
Lets say I want to test an api call in one of my Vue components
api.ajax("users")
.done((response) => {
stores.usersStore.init(response)
})
usersStore.js
let usersStore = {
store: {
users: []
},
get() {
return this.store
},
set(key, value) {
if (key) {
this.users[key] = value
} else {
console.error("Key required")
}
},
init(initObject) {
this.store.users = initObject
}
}
export default usersStore;
So the api call basically just initializes the array of users. How do I test something like this in jasmine? I tried spies, and mocking.
Any inputs would be greatly appreciated!
Edit --> The test I am attempting to write
describe(" Tests", function() {
var us;
beforeEach(function() {
us = new usersStore()
us.store.users.push("Anand Dharne")
});
describe("when retrieved by name", function() {
var u;
beforeEach(function(done) {
u = us.init(response, {
success: function () {
done();
}
});
});
it("api call", function() {
expect(u.store.users).toEqual("Anand Dharne");
});
});
});