2019-10-20 16:59:02 +02:00
|
|
|
import { apiClient, createUrl } from "./apiclient";
|
|
|
|
|
import fetchMock from "fetch-mock";
|
|
|
|
|
import { BackendError } from "./errors";
|
2019-10-19 16:38:07 +02:00
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
describe("create url", () => {
|
|
|
|
|
it("should not change absolute urls", () => {
|
|
|
|
|
expect(createUrl("https://www.scm-manager.org")).toBe(
|
|
|
|
|
"https://www.scm-manager.org"
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
it("should add prefix for api", () => {
|
|
|
|
|
expect(createUrl("/users")).toBe("/api/v2/users");
|
|
|
|
|
expect(createUrl("users")).toBe("/api/v2/users");
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
describe("error handling tests", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const earthNotFoundError = {
|
2019-10-20 16:59:02 +02:00
|
|
|
transactionId: "42t",
|
|
|
|
|
errorCode: "42e",
|
|
|
|
|
message: "earth not found",
|
2019-10-19 16:38:07 +02:00
|
|
|
context: [
|
|
|
|
|
{
|
2019-10-20 16:59:02 +02:00
|
|
|
type: "planet",
|
|
|
|
|
id: "earth"
|
|
|
|
|
}
|
|
|
|
|
]
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
fetchMock.reset();
|
|
|
|
|
fetchMock.restore();
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
it("should create a normal error, if the content type is not scmm-error", done => {
|
|
|
|
|
fetchMock.getOnce("/api/v2/error", {
|
|
|
|
|
status: 404
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
apiClient.get("/error").catch((err: Error) => {
|
|
|
|
|
expect(err.name).toEqual("Error");
|
|
|
|
|
expect(err.message).toContain("404");
|
2019-10-19 16:38:07 +02:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
it("should create an backend error, if the content type is scmm-error", done => {
|
|
|
|
|
fetchMock.getOnce("/api/v2/error", {
|
2019-10-19 16:38:07 +02:00
|
|
|
status: 404,
|
|
|
|
|
headers: {
|
2019-10-20 16:59:02 +02:00
|
|
|
"Content-Type": "application/vnd.scmm-error+json;v=2"
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
2019-10-20 16:59:02 +02:00
|
|
|
body: earthNotFoundError
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
apiClient.get("/error").catch((err: BackendError) => {
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(err).toBeInstanceOf(BackendError);
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
expect(err.message).toEqual("earth not found");
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(err.statusCode).toBe(404);
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
expect(err.transactionId).toEqual("42t");
|
|
|
|
|
expect(err.errorCode).toEqual("42e");
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(err.context).toEqual([
|
|
|
|
|
{
|
2019-10-20 16:59:02 +02:00
|
|
|
type: "planet",
|
|
|
|
|
id: "earth"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
]);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|