Add assertions on request bodies in store tests
#291 (closed) possibly caused a lot of annotations to lack classes and make users believe everything was saved. This commit introduced it, in the middle of a large refactoring.
This bug could have been avoided earlier by having assertions on the request bodies in unit tests. We already test for the request methods and URLs just by having the API mock throw errors for unknown requests, but we do not currently test for request bodies.
According to its README, axios-mock-adapter
allows request bodies when adding mock responses: mock.onPut("/product", { id: 4, name: "foo" }).reply(204);
would only work with { id: 4, name: "foo" }
as a body.
It is also possible to test for a specific order of requests, which would also prevent trying to create a classification before creating the element for example:
// Expected order of requests:
const responses = [
["GET", "/foo", 200, { foo: "bar" }],
["POST", "/bar", 200],
["PUT", "/baz", 200],
];
// Match ALL requests
mock.onAny().reply((config) => {
const [method, url, ...response] = responses.shift();
if (config.url === url && config.method.toUpperCase() === method)
return response;
// Unexpected request, error out
return [500, {}];
});
We could write a new test helper to list expected request methods, URLs, bodies and the mocked responses, and let it use this snippet, to have proper request testing.