photoprism/frontend/tests/unit/model/rest_test.js

127 lines
4.7 KiB
JavaScript
Raw Normal View History

import Rest from "model/rest";
2019-08-09 10:54:05 +02:00
import Album from "model/album";
import Label from "model/label";
2019-08-12 15:20:28 +02:00
import MockAdapter from "axios-mock-adapter";
import Api from "common/api";
2019-08-09 10:54:05 +02:00
2019-08-13 08:01:16 +02:00
let chai = require('../../../node_modules/chai/chai');
let assert = chai.assert;
2019-08-09 10:54:05 +02:00
describe("model/abstract", () => {
2019-08-12 15:20:28 +02:00
const mock = new MockAdapter(Api);
2019-08-09 10:54:05 +02:00
it("should set values", () => {
const values = {id: 5, Name: "Black Cat", Slug: "black-cat"};
2019-08-09 10:54:05 +02:00
const label = new Label(values);
assert.equal(label.Name, "Black Cat");
assert.equal(label.Slug, "black-cat");
2019-08-09 10:54:05 +02:00
label.setValues();
assert.equal(label.Name, "Black Cat");
assert.equal(label.Slug, "black-cat");
const values2 = {id: 6, Name: "White Cat", Slug: "white-cat"};
2019-08-09 10:54:05 +02:00
label.setValues(values2);
assert.equal(label.Name, "White Cat");
assert.equal(label.Slug, "white-cat");
2019-08-09 10:54:05 +02:00
});
it("should get values", () => {
const values = {id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66};
2019-08-09 10:54:05 +02:00
const album = new Album(values);
const result = album.getValues();
assert.equal(result.Name, "Christmas 2019");
assert.equal(result.UID, 66);
2019-08-09 10:54:05 +02:00
});
it("should get id", () => {
const values = {id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66};
2019-08-09 10:54:05 +02:00
const album = new Album(values);
const result = album.getId();
assert.equal(result, 66);
});
it("should test if id exists", () => {
const values = {id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66};
2019-08-09 10:54:05 +02:00
const album = new Album(values);
const result = album.hasId();
assert.equal(result, true);
});
it("should get model name", () => {
const result = Rest.getModelName();
2019-08-09 10:54:05 +02:00
assert.equal(result, "Item");
});
2019-08-12 15:20:28 +02:00
it("should update album", async() => {
mock.onPut().reply(200, {Description: "Test description"});
const values = {id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66};
2019-08-12 15:20:28 +02:00
const album = new Album(values);
assert.equal(album.Description, undefined);
2019-08-12 15:20:28 +02:00
await album.update();
assert.equal(album.Description, "Test description");
2019-08-12 15:20:28 +02:00
mock.reset();
});
it("should save album", async() => {
mock.onPut().reply(200, {Description: "Test description"});
const values = {id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66};
2019-08-12 15:20:28 +02:00
const album = new Album(values);
assert.equal(album.Description, undefined);
2019-08-12 15:20:28 +02:00
await album.save();
assert.equal(album.Description, "Test description");
2019-08-12 15:20:28 +02:00
mock.reset();
});
it("should save album", async() => {
mock.onPost().reply(200, {Description: "Test description"});
const values = {id: 5, Name: "Christmas 2019", Slug: "christmas-2019"};
2019-08-12 15:20:28 +02:00
const album = new Album(values);
assert.equal(album.Description, undefined);
2019-08-12 15:20:28 +02:00
await album.save();
assert.equal(album.Description, "Test description");
2019-08-12 15:20:28 +02:00
mock.reset();
});
it("should remove album", async() => {
mock.onDelete().reply(200);
const values = {id: 5, Name: "Christmas 2019", Slug: "christmas-2019"};
2019-08-12 15:20:28 +02:00
const album = new Album(values);
assert.equal(album.Name, "Christmas 2019");
2019-08-12 15:20:28 +02:00
await album.remove();
mock.reset();
});
it("should get edit form", async() => {
mock.onAny().reply(200, "editForm");
const values = {id: 5, Name: "Christmas 2019", Slug: "christmas-2019"};
2019-08-12 15:20:28 +02:00
const album = new Album(values);
const result = await album.getEditForm();
assert.equal(result.definition, "editForm");
mock.reset();
});
it("should get create form", async() => {
mock.onAny().reply(200, "createForm");
const result = await Album.getCreateForm();
assert.equal(result.definition, "createForm");
mock.reset();
});
it("should get search form", async() => {
mock.onAny().reply(200, "searchForm");
const result = await Album.getSearchForm();
assert.equal(result.definition, "searchForm");
mock.reset();
});
2019-08-12 15:30:52 +02:00
it("should search label", async() => {
mock.onAny().reply(200, {"ID":51,"CreatedAt":"2019-07-03T18:48:07Z","UpdatedAt":"2019-07-25T01:04:44Z","DeletedAt":"0001-01-01T00:00:00Z","Slug":"tabby-cat","Name":"tabby cat","Priority":5,"LabelCount":9,"Favorite":false,"Description":"","Notes":""});
2019-08-12 15:20:28 +02:00
const result = await Album.search();
assert.equal(result.data.ID, 51);
assert.equal(result.data.Name, "tabby cat");
2019-08-12 15:20:28 +02:00
mock.reset();
});
2019-08-13 08:01:16 +02:00
it("should get collection resource", () => {
assert.equal(Rest.getCollectionResource(), "");
2019-08-13 08:01:16 +02:00
});
});