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=',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user