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

121 lines
12 KiB
JavaScript
Raw Normal View History

2019-08-09 11:53:11 +02:00
import Config from "common/config";
2019-08-09 16:00:52 +02:00
import MockAdapter from "axios-mock-adapter";
import Api from "common/api";
import StorageShim from "node-storage-shim";
2019-08-09 11:53:11 +02:00
2021-01-01 22:37:25 +01:00
window.__CONFIG__ = {"name":"PhotoPrism","version":"201213-283748ca-Linux-x86_64-DEBUG","copyright":"(c) 2018-2021 Michael Mayer \u003chello@photoprism.org\u003e","flags":"public debug experimental settings","siteUrl":"http://localhost:2342/","sitePreview":"http://localhost:2342/static/img/preview.jpg","siteTitle":"PhotoPrism","siteCaption":"Browse Your Life","siteDescription":"Open-Source Personal Photo Management","siteAuthor":"@browseyourlife","debug":true,"readonly":false,"uploadNSFW":false,"public":true,"experimental":true,"disableSettings":false,"albumCategories":null,"albums":[],"cameras":[{"ID":6,"Slug":"apple-iphone-5s","Name":"Apple iPhone 5s","Make":"Apple","Model":"iPhone 5s"},{"ID":7,"Slug":"apple-iphone-6","Name":"Apple iPhone 6","Make":"Apple","Model":"iPhone 6"},{"ID":8,"Slug":"apple-iphone-se","Name":"Apple iPhone SE","Make":"Apple","Model":"iPhone SE"},{"ID":2,"Slug":"canon-eos-5d","Name":"Canon EOS 5D","Make":"Canon","Model":"EOS 5D"},{"ID":4,"Slug":"canon-eos-6d","Name":"Canon EOS 6D","Make":"Canon","Model":"EOS 6D"},{"ID":5,"Slug":"canon-eos-7d","Name":"Canon EOS 7D","Make":"Canon","Model":"EOS 7D"},{"ID":9,"Slug":"huawei-p30","Name":"HUAWEI P30","Make":"HUAWEI","Model":"P30"},{"ID":3,"Slug":"olympus-c2500l","Name":"Olympus C2500L","Make":"Olympus","Model":"C2500L"},{"ID":1,"Slug":"zz","Name":"Unknown","Make":"","Model":"Unknown"}],"lenses":[{"ID":2,"Slug":"24-0-105-0-mm","Name":"24.0 - 105.0 mm","Make":"","Model":"24.0 - 105.0 mm","Type":""},{"ID":7,"Slug":"apple-iphone-5s-back-camera-4-12mm-f-2-2","Name":"Apple iPhone 5s back camera 4.12mm f/2.2","Make":"Apple","Model":"iPhone 5s back camera 4.12mm f/2.2","Type":""},{"ID":9,"Slug":"apple-iphone-6-back-camera-4-15mm-f-2-2","Name":"Apple iPhone 6 back camera 4.15mm f/2.2","Make":"Apple","Model":"iPhone 6 back camera 4.15mm f/2.2","Type":""},{"ID":10,"Slug":"apple-iphone-se-back-camera-4-15mm-f-2-2","Name":"Apple iPhone SE back camera 4.15mm f/2.2","Make":"Apple","Model":"iPhone SE back camera 4.15mm f/2.2","Type":""},{"ID":3,"Slug":"ef100mm-f-2-8l-macro-is-usm","Name":"EF100mm f/2.8L Macro IS USM","Make":"","Model":"EF100mm f/2.8L Macro IS USM","Type":""},{"ID":6,"Slug":"ef16-35mm-f-2-8l-ii-usm","Name":"EF16-35mm f/2.8L II USM","Make":"","Model":"EF16-35mm f/2.8L II USM","Type":""},{"ID":4,"Slug":"ef24-105mm-f-4l-is-usm","Name":"EF24-105mm f/4L IS USM","Make":"","Model":"EF24-105mm f/4L IS USM","Type":""},{"ID":8,"Slug":"ef35mm-f-2-is-usm","Name":"EF35mm f/2 IS USM","Make":"","Model":"EF35mm f/2 IS USM","Type":""},{"ID":5,"Slug":"ef70-200mm-f-4l-is-usm","Name":"EF70-200mm f/4L IS USM","Make":"","Model":"EF70-200mm f/4L IS USM","Type":""},{"ID":1,"Slug":"zz","Name":"Unknown","Make":"","Model":"Unknown","Type":""}],"countries":[{"ID":"at","Slug":"austria","Name":"Austria"},{"ID":"bw","Slug":"botswana","Name":"Botswana"},{"ID":"ca","Slug":"canada","Name":"Canada"},{"ID":"cu","Slug":"cuba","Name":"Cuba"},{"ID":"fr","Slug":"france","Name":"France"},{"ID":"de","Slug":"germany","Name":"Germany"},{"ID":"gr","Slug":"greece","Name":"Greece"},{"ID":"it","Slug":"italy","Name":"Italy"},{"ID":"za","Slug":"south-africa","Name":"South Africa"},{"ID":"ch","Slug":"switzerland","Name":"Switzerland"},{"ID":"gb","Slug":"united-kingdom","Name":"United Kingdom"},{"ID":"us","Slug":"usa","Name":"USA"},{"ID":"zz","Slug":"zz","Name":"Unknown"}],"thumbs":[{"size":"fit_720","use":"Mobile, TV","w":720,"h":720},{"size":"fit_1280","use":"Mobile, HD Ready TV","w":1280,"h":1024},{"size":"fit_1920","use":"Mobile, Full HD TV","w":1920,"h":1200},{"size":"fit_2048","use":"Tablets, Cinema 2K","w":2048,"h":2048},{"size":"fit_2560","use":"Quad HD, Retina Display","w":2560,"h":1600},{"size":"fit_4096","use":"Ultra HD, Retina 4K","w":4096,"h":4096},{"size":"fit_7680","use":"8K Ultra HD 2, Retina 6K","w":7680,"h":4320}],"status":"unregistered","mapKey":"jOTd5JGKYQV1fiAW4UZO","downloadToken":"2lbh9x09","previewToken":"public","jsHash":"6d752756","cssHash":"c5bb9de2","settings":{"ui":{"scrollbar":true,"theme":"default","language":"en"},"templates":{"default":"index.tmpl"},"maps":{"animate":0,"s
2020-06-30 16:53:12 +02:00
2020-07-02 10:03:00 +02:00
let chai = require("chai/chai");
2019-08-13 08:11:22 +02:00
let assert = chai.assert;
const config2 = new Config(new StorageShim(), window.__CONFIG__);
2020-06-30 16:53:12 +02:00
2019-08-12 13:00:13 +02:00
describe("common/config", () => {
2019-08-09 16:00:52 +02:00
2019-08-12 13:00:13 +02:00
const mock = new MockAdapter(Api);
2019-08-09 11:53:11 +02:00
it("should get all config values", () => {
const storage = new StorageShim();
const values = {siteTitle: "Foo", name: "testConfig", year: "2300"};
2019-08-09 11:53:11 +02:00
const config = new Config(storage, values);
const result = config.getValues();
assert.equal(result.name, "testConfig");
});
it("should set multiple config values", () => {
const storage = new StorageShim();
const values = {siteTitle: "Foo", country: "Germany", city: "Hamburg", settings: {"ui": {language:"de", theme: "default"}}};
const newValues = {siteTitle: "Foo", new: "xxx", city: "Berlin", debug: true, settings: {"ui": {language:"en", theme: "lavender"}}};
2019-08-09 11:53:11 +02:00
const config = new Config(storage, values);
assert.equal(config.values.settings.ui.theme, "default");
assert.equal(config.values.settings.ui.language, "de");
2019-08-09 11:53:11 +02:00
assert.equal(config.values.new, undefined);
assert.equal(config.values.city, "Hamburg");
config.setValues();
assert.equal(config.values.new, undefined);
assert.equal(config.values.city, "Hamburg");
config.setValues(newValues);
const result = config.getValues();
assert.equal(result.city, "Berlin");
assert.equal(result.new, "xxx");
assert.equal(result.country, "Germany");
assert.equal(config.values.settings.ui.theme, "lavender");
assert.equal(config.values.settings.ui.language, "en");
2019-08-09 11:53:11 +02:00
});
it("should store values", () => {
const storage = new StorageShim();
const values = {siteTitle: "Foo", country: "Germany", city: "Hamburg"};
2019-08-09 11:53:11 +02:00
const config = new Config(storage, values);
assert.equal(config.storage["config"], undefined);
config.storeValues();
const expected = '{"siteTitle":"Foo","country":"Germany","city":"Hamburg"}';
assert.equal(config.storage["config"], expected);
2019-08-09 11:53:11 +02:00
});
it("should set and get single config value", () => {
const storage = new StorageShim();
2019-08-09 11:53:11 +02:00
const values = {};
const config = new Config(storage, values);
config.set("city", "Berlin");
const result = config.get("city");
2019-08-09 11:53:11 +02:00
assert.equal(result, "Berlin");
});
2020-06-30 16:53:12 +02:00
it("should return settings", () => {
const result = config2.settings();
assert.equal(result.ui.theme, "default");
assert.equal(result.ui.language, "en");
2020-06-30 16:53:12 +02:00
});
it("should return feature", () => {
assert.equal(config2.feature("places"), true);
assert.equal(config2.feature("download"), true);
2020-06-30 16:53:12 +02:00
});
//TODO
/*it.only("should test onCount", () => {
const items = [{}, {}, {}];
assert.equal(config2.values.count.cameras, 1);
config2.onCount("add.camera", items);
assert.equal(config2.values.count.cameras, 1);
console.log(config2.values.count);
config2.onCount("add.cameras", items);
console.log(config2.values.count);
assert.equal(config2.values.count.cameras, 4);
config2.onCount("add.lenses", items);
assert.equal(config2.values.count.lenses, 3);
config2.onCount("add.countries", items);
assert.equal(config2.values.count.countries, 5);
config2.onCount("add.photos", items);
assert.equal(config2.values.count.photos, 129);
config2.onCount("add.videos", items);
assert.equal(config2.values.count.videos, 3);
config2.onCount("add.hidden", items);
assert.equal(config2.values.count.hidden, 6);
config2.onCount("add.favorites", items);
assert.equal(config2.values.count.favorites, 4);
config2.onCount("add.private", items);
assert.equal(config2.values.count.private, 3);
config2.onCount("add.review", items);
assert.equal(config2.values.count.review, 3);
config2.onCount("add.states", items);
assert.equal(config2.values.count.states, 5);
config2.onCount("add.albums", items);
assert.equal(config2.values.count.albums, 3);
config2.onCount("add.moments", items);
assert.equal(config2.values.count.moments, 3);
config2.onCount("add.months", items);
assert.equal(config2.values.count.months, 3);
config2.onCount("add.folders", items);
assert.equal(config2.values.count.folders, 3);
config2.onCount("add.files", items);
assert.equal(config2.values.count.files, 258);
config2.onCount("add.places", items);
assert.equal(config2.values.count.places, 3);
config2.onCount("add.labels", items);
assert.equal(config2.values.count.labels, 12);
});*/
});