photoprism/frontend/tests/unit/common/api_test.js

100 lines
2.4 KiB
JavaScript
Raw Normal View History

import { Api } from "../fixtures";
2020-07-02 08:03:00 +00:00
let chai = require("chai/chai");
2019-08-13 07:45:10 +00:00
let assert = chai.assert;
2019-08-13 07:45:10 +00:00
describe("common/api", () => {
2021-01-09 03:41:33 +00:00
const getCollectionResponse = [
{ id: 1, name: "John Smith" },
{ id: 1, name: "John Smith" },
];
2021-01-09 03:41:33 +00:00
const getEntityResponse = {
id: 1,
name: "John Smith",
};
2021-01-09 03:41:33 +00:00
const postEntityResponse = {
users: [{ id: 1, name: "John Smith" }],
};
2021-01-09 03:41:33 +00:00
const putEntityResponse = {
users: [{ id: 2, name: "John Foo" }],
};
2021-01-09 03:41:33 +00:00
const deleteEntityResponse = null;
2021-01-09 03:41:33 +00:00
it("get should return a list of results and return with HTTP code 200", (done) => {
Api.get("foo")
.then((response) => {
assert.equal(200, response.status);
assert.deepEqual(getCollectionResponse, response.data);
done();
})
.catch((error) => {
done(error);
});
});
2019-08-13 07:45:10 +00:00
2021-01-09 03:41:33 +00:00
it("get should return one item and return with HTTP code 200", (done) => {
Api.get("foo/123")
.then((response) => {
assert.equal(200, response.status);
assert.deepEqual(getEntityResponse, response.data);
done();
})
.catch((error) => {
done(error);
});
});
2021-01-09 03:41:33 +00:00
it("post should create one item and return with HTTP code 201", (done) => {
Api.post("foo", postEntityResponse)
.then((response) => {
assert.equal(201, response.status);
assert.deepEqual(postEntityResponse, response.data);
done();
})
.catch((error) => {
done(error);
});
});
2021-01-09 03:41:33 +00:00
it("put should update one item and return with HTTP code 200", (done) => {
Api.put("foo/2", putEntityResponse)
.then((response) => {
assert.equal(200, response.status);
assert.deepEqual(putEntityResponse, response.data);
done();
})
.catch((error) => {
done(error);
});
});
2021-01-09 03:41:33 +00:00
it("delete should delete one item and return with HTTP code 204", (done) => {
Api.delete("foo/2", deleteEntityResponse)
.then((response) => {
assert.equal(204, response.status);
assert.deepEqual(deleteEntityResponse, response.data);
done();
})
.catch((error) => {
done(error);
});
});
/*
2021-01-09 03:41:33 +00:00
it("get error", function () {
return Api.get("error")
.then(function (m) {
return Promise.reject("error expected");
2021-01-09 03:41:33 +00:00
})
.catch(function (m) {
assert.equal(m.message, "Request failed with status code 401");
return Promise.resolve();
2021-01-09 03:41:33 +00:00
});
});
*/
2019-05-20 18:06:26 +00:00
});