mirror of
https://github.com/mediacms-io/mediacms.git
synced 2026-03-22 20:43:10 -04:00
chore: minor code enhancements
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { apiConfig } from '../../../src/static/js/utils/settings/api';
|
||||
|
||||
const sampleGlobal = {
|
||||
site: { api: 'https://example.com/api///' },
|
||||
site: { api: 'http://example.com/api///' },
|
||||
// endpoints below intentionally contain leading slashes to ensure they are stripped
|
||||
api: {
|
||||
media: '/v1/media/',
|
||||
@@ -20,43 +20,41 @@ const sampleGlobal = {
|
||||
|
||||
describe('utils/settings', () => {
|
||||
describe('api', () => {
|
||||
test('trims trailing slashes on base and ensures single slash joins', () => {
|
||||
const cfg = apiConfig(sampleGlobal.site.api as any, sampleGlobal.api as any);
|
||||
expect(cfg.media).toBe('https://example.com/api/v1/media/');
|
||||
// base should not end with a slash and endpoint leading slash stripped
|
||||
expect(cfg.users).toBe('https://example.com/api/v1/users');
|
||||
test('Builds search endpoints with expected query fragments', () => {
|
||||
const cfg = apiConfig(sampleGlobal.site.api, sampleGlobal.api as any);
|
||||
|
||||
expect(cfg).toStrictEqual({
|
||||
media: 'http://example.com/api/v1/media/',
|
||||
featured: 'http://example.com/api/v1/media/?show=featured',
|
||||
recommended: 'http://example.com/api/v1/media/?show=recommended',
|
||||
playlists: 'http://example.com/api/v1/playlists',
|
||||
users: 'http://example.com/api/v1/users',
|
||||
user: {
|
||||
liked: 'http://example.com/api/v1/user/liked',
|
||||
history: 'http://example.com/api/v1/user/history',
|
||||
playlists: 'http://example.com/api/v1/playlists?author=',
|
||||
},
|
||||
archive: {
|
||||
tags: 'http://example.com/api/v1/tags',
|
||||
categories: 'http://example.com/api/v1/categories',
|
||||
},
|
||||
manage: {
|
||||
media: 'http://example.com/api/v1/manage/media',
|
||||
users: 'http://example.com/api/v1/manage/users',
|
||||
comments: 'http://example.com/api/v1/manage/comments',
|
||||
},
|
||||
search: {
|
||||
query: 'http://example.com/api/v1/search?q=',
|
||||
titles: 'http://example.com/api/v1/search?show=titles&q=',
|
||||
tag: 'http://example.com/api/v1/search?t=',
|
||||
category: 'http://example.com/api/v1/search?c=',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('adds featured/recommended query to media variants', () => {
|
||||
const cfg = apiConfig(sampleGlobal.site.api as any, sampleGlobal.api as any);
|
||||
expect(cfg.featured).toBe('https://example.com/api/v1/media/?show=featured');
|
||||
expect(cfg.recommended).toBe('https://example.com/api/v1/media/?show=recommended');
|
||||
});
|
||||
test('Handles base url with path and endpoint with existing query', () => {
|
||||
const base = 'https://domain.com/base/';
|
||||
|
||||
test('builds nested user, archive, manage maps', () => {
|
||||
const cfg = apiConfig(sampleGlobal.site.api as any, sampleGlobal.api as any);
|
||||
expect(cfg.user.liked).toBe('https://example.com/api/v1/user/liked');
|
||||
expect(cfg.user.history).toBe('https://example.com/api/v1/user/history');
|
||||
expect(cfg.user.playlists).toBe('https://example.com/api/v1/playlists?author=');
|
||||
|
||||
expect(cfg.archive.tags).toBe('https://example.com/api/v1/tags');
|
||||
expect(cfg.archive.categories).toBe('https://example.com/api/v1/categories');
|
||||
|
||||
expect(cfg.manage.media).toBe('https://example.com/api/v1/manage/media');
|
||||
expect(cfg.manage.users).toBe('https://example.com/api/v1/manage/users');
|
||||
expect(cfg.manage.comments).toBe('https://example.com/api/v1/manage/comments');
|
||||
});
|
||||
|
||||
test('builds search endpoints with expected query fragments', () => {
|
||||
const cfg = apiConfig(sampleGlobal.site.api as any, sampleGlobal.api as any);
|
||||
expect(cfg.search.query).toBe('https://example.com/api/v1/search?q=');
|
||||
expect(cfg.search.titles).toBe('https://example.com/api/v1/search?show=titles&q=');
|
||||
expect(cfg.search.tag).toBe('https://example.com/api/v1/search?t=');
|
||||
expect(cfg.search.category).toBe('https://example.com/api/v1/search?c=');
|
||||
});
|
||||
|
||||
test('handles base url with path and endpoint with existing query', () => {
|
||||
const base = 'https://example.com/base/';
|
||||
const endpoints = {
|
||||
media: 'items?x=1',
|
||||
playlists: '/pls/',
|
||||
@@ -69,13 +67,37 @@ describe('utils/settings', () => {
|
||||
manage_users: 'm/users',
|
||||
manage_comments: 'm/comments',
|
||||
search: '/s',
|
||||
} as any;
|
||||
const cfg = apiConfig(base as any, endpoints);
|
||||
expect(cfg.media).toBe('https://example.com/base/items?x=1');
|
||||
expect(cfg.playlists).toBe('https://example.com/base/pls/');
|
||||
expect(cfg.user.liked).toBe('https://example.com/base/me/liked');
|
||||
expect(cfg.archive.categories).toBe('https://example.com/base/c');
|
||||
expect(cfg.search.query).toBe('https://example.com/base/s?q=');
|
||||
};
|
||||
|
||||
const cfg = apiConfig(base, endpoints as any);
|
||||
|
||||
expect(cfg).toStrictEqual({
|
||||
media: 'https://domain.com/base/items?x=1',
|
||||
featured: 'https://domain.com/base/items?x=1?show=featured',
|
||||
recommended: 'https://domain.com/base/items?x=1?show=recommended',
|
||||
playlists: 'https://domain.com/base/pls/',
|
||||
users: 'https://domain.com/base/users',
|
||||
user: {
|
||||
liked: 'https://domain.com/base/me/liked',
|
||||
history: 'https://domain.com/base/me/history',
|
||||
playlists: 'https://domain.com/base/pls/?author=',
|
||||
},
|
||||
archive: {
|
||||
tags: 'https://domain.com/base/t',
|
||||
categories: 'https://domain.com/base/c',
|
||||
},
|
||||
manage: {
|
||||
media: 'https://domain.com/base/m/media',
|
||||
users: 'https://domain.com/base/m/users',
|
||||
comments: 'https://domain.com/base/m/comments',
|
||||
},
|
||||
search: {
|
||||
query: 'https://domain.com/base/s?q=',
|
||||
titles: 'https://domain.com/base/s?show=titles&q=',
|
||||
tag: 'https://domain.com/base/s?t=',
|
||||
category: 'https://domain.com/base/s?c=',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -51,8 +51,8 @@ describe('utils/settings', () => {
|
||||
can: {
|
||||
changePassword: true,
|
||||
deleteProfile: true,
|
||||
addComment: false,
|
||||
mentionComment: false,
|
||||
addComment: true,
|
||||
mentionComment: true,
|
||||
deleteComment: true,
|
||||
editMedia: true,
|
||||
deleteMedia: true,
|
||||
@@ -85,8 +85,8 @@ describe('utils/settings', () => {
|
||||
changePassword: true,
|
||||
deleteProfile: true,
|
||||
readComment: true,
|
||||
addComment: false,
|
||||
mentionComment: false,
|
||||
addComment: true,
|
||||
mentionComment: true,
|
||||
deleteComment: true,
|
||||
editMedia: true,
|
||||
deleteMedia: true,
|
||||
|
||||
@@ -56,10 +56,10 @@ describe('utils/settings', () => {
|
||||
const def = optionsPagesConfig();
|
||||
expect(def.search.advancedFilters).toBe(false);
|
||||
|
||||
const falsy = optionsPagesConfig(undefined, { advancedFilters: false } as any);
|
||||
const falsy = optionsPagesConfig(undefined, { advancedFilters: false });
|
||||
expect(falsy.search.advancedFilters).toBe(false);
|
||||
|
||||
const truthy = optionsPagesConfig(undefined, { advancedFilters: true } as any);
|
||||
const truthy = optionsPagesConfig(undefined, { advancedFilters: true });
|
||||
expect(truthy.search.advancedFilters).toBe(true);
|
||||
});
|
||||
|
||||
@@ -87,7 +87,7 @@ describe('utils/settings', () => {
|
||||
const cfg1 = optionsPagesConfig(undefined, undefined, { related: { initialSize: NaN } } as any);
|
||||
expect(cfg1.media.related.initialSize).toBe(10);
|
||||
|
||||
const cfg2 = optionsPagesConfig(undefined, undefined, { related: { initialSize: '12' as any } } as any);
|
||||
const cfg2 = optionsPagesConfig(undefined, undefined, { related: { initialSize: '12' } } as any);
|
||||
expect(cfg2.media.related.initialSize).toBe(10);
|
||||
});
|
||||
|
||||
@@ -112,7 +112,7 @@ describe('utils/settings', () => {
|
||||
const search = { advancedFilters: true };
|
||||
const media = { hideViews: true, related: { initialSize: 5 } };
|
||||
const profile = { includeHistory: true };
|
||||
const validPages: any = { latest: { title: 'L' }, featured: { title: 'F' }, recommended: { title: 'R' } };
|
||||
const validPages = { latest: { title: 'L' }, featured: { title: 'F' }, recommended: { title: 'R' } };
|
||||
|
||||
const homeCopy = JSON.parse(JSON.stringify(home));
|
||||
const searchCopy = JSON.parse(JSON.stringify(search));
|
||||
@@ -120,7 +120,7 @@ describe('utils/settings', () => {
|
||||
const profileCopy = JSON.parse(JSON.stringify(profile));
|
||||
const validPagesCopy = JSON.parse(JSON.stringify(validPages));
|
||||
|
||||
optionsPagesConfig(home, search, media, profile, validPages);
|
||||
optionsPagesConfig(home, search, media, profile, validPages as any);
|
||||
|
||||
expect(home).toStrictEqual(homeCopy);
|
||||
expect(search).toStrictEqual(searchCopy);
|
||||
|
||||
@@ -18,8 +18,8 @@ describe('utils/settings', () => {
|
||||
});
|
||||
|
||||
test('Returns default when provided mediaTypes is non-array or undefined/null', () => {
|
||||
expect(playlistsConfig({} as any).mediaTypes).toEqual(['audio', 'video']);
|
||||
expect(playlistsConfig({ mediaTypes: undefined } as any).mediaTypes).toEqual(['audio', 'video']);
|
||||
expect(playlistsConfig({}).mediaTypes).toEqual(['audio', 'video']);
|
||||
expect(playlistsConfig({ mediaTypes: undefined }).mediaTypes).toEqual(['audio', 'video']);
|
||||
expect(playlistsConfig({ mediaTypes: null as any }).mediaTypes).toEqual(['audio', 'video']);
|
||||
expect(playlistsConfig({ mediaTypes: 'audio' as any }).mediaTypes).toEqual(['audio', 'video']);
|
||||
expect(playlistsConfig({ mediaTypes: 123 as any }).mediaTypes).toEqual(['audio', 'video']);
|
||||
|
||||
@@ -58,12 +58,12 @@ describe('utils/settings', () => {
|
||||
});
|
||||
|
||||
test('Does not mutate input objects', () => {
|
||||
const themeIn: any = { mode: ' dark ', switch: { enabled: false, position: ' sidebar ' } };
|
||||
const themeIn = { mode: ' dark ', switch: { enabled: false, position: ' sidebar ' } };
|
||||
const logoIn = { lightMode: { img: ' x ', svg: ' y ' }, darkMode: { img: ' z ', svg: ' w ' } };
|
||||
const themeCopy = JSON.parse(JSON.stringify(themeIn));
|
||||
const logoCopy = JSON.parse(JSON.stringify(logoIn));
|
||||
|
||||
themeConfig(themeIn, logoIn);
|
||||
themeConfig(themeIn as any, logoIn);
|
||||
|
||||
expect(themeIn).toStrictEqual(themeCopy);
|
||||
expect(logoIn).toStrictEqual(logoCopy);
|
||||
|
||||
Reference in New Issue
Block a user