Trying to stub a request with SinonJS has been a challenge for me.
I set up the mock request before each test to use fake data, but it's not working as expected. I attempted to use Promise.resolve
for resolving, but that didn't work out either.
Below is the code for the test:
describe("Store | Users actions", () => {
let commit = null;
let page = 1;
let itemsPerPage = 2;
const users_response = {
status: 200,
data: [{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1f2c8cfc2c4d3c4e1c0d1d3c8cd8fc3c8db">[email protected]</a>"
},
{
"id": 2,
"name": Ervin Howell",
"username": Antonette",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b68535a55555a7b565e575248485a154f4d">[email protected]</a>"
}]
};
beforeEach(() => {
commit = sinon.spy();
sinon
.stub(api.users, "list").resolves();
});
afterEach(() => {
api.users.list.restore();
});
it("should list users", () => {
users.actions.list({ commit }, { page, itemsPerPage });
expect(commit).to.have.been.calledWith("UNSET_ERROR");
expect(commit).to.have.been.calledWith("GET_PAGINATED", users_response);
});
});
The error message I'm encountering is as follows:
1) Store | Users actions
should list users:
AssertionError: expected spy to have been called with arguments GET_PAGINATED, {
data: [{ email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="34675d5a57514651745544465d581a565d4e">[email protected]</a>", id: 1, name: "Leanne Graham", username: "Bret" }, { email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cdfe4ede2e2edcce1e9e0e5ffffeda2f8fa">[email protected]</a>", id: 2, name: "Ervin Howell", username: "Antonette" }],
status: 200
}
"UNSET_ERROR" "GET_PAGINATED"
{
data: [{ email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9fccf6f1fcfaedfadffeefedf6f3b1fdf6e5">[email protected]</a>", id: 1, name: "Leanne Graham", username: "Bret" }, { email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aaf9c2cbc4c4cbeac7cfc6c3d9d9cb84dedc">[email protected]</a>", id: 2, name: "Ervin Howell", username: "Antonette" }],
status: 200
}
at Context.<anonymous> (dist/js/webpack:/tests/unit/store/users.spec.js:184:1)
list({ commit }, { page, itemsPerPage, sort, search }) {
commit("UNSET_ERROR");
return api.users
.list(page, itemsPerPage, sort, search)
.then((users) => commit("GET_PAGINATED", users.data))
.catch((error) => commit("SET_ERROR", error));
}
I would greatly appreciate any assistance in identifying where I might be going wrong here.
Edit: added list function