chore: Refactors help center article url helper (#8269)
This commit is contained in:
committed by
GitHub
parent
ebe9daea00
commit
23ea829510
@@ -64,3 +64,35 @@ export const isValidURL = value => {
|
||||
/^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/gm;
|
||||
return URL_REGEX.test(value);
|
||||
};
|
||||
|
||||
export const getArticleSearchURL = ({
|
||||
host,
|
||||
portalSlug,
|
||||
pageNumber,
|
||||
locale,
|
||||
status,
|
||||
authorId,
|
||||
categorySlug,
|
||||
sort,
|
||||
query,
|
||||
}) => {
|
||||
const queryParams = new URLSearchParams({});
|
||||
|
||||
const params = {
|
||||
page: pageNumber,
|
||||
locale,
|
||||
status,
|
||||
author_id: authorId,
|
||||
category_slug: categorySlug,
|
||||
sort,
|
||||
query,
|
||||
};
|
||||
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== null && value !== undefined) {
|
||||
queryParams.set(key, value);
|
||||
}
|
||||
});
|
||||
|
||||
return `${host}/${portalSlug}/articles?${queryParams.toString()}`;
|
||||
};
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
conversationUrl,
|
||||
isValidURL,
|
||||
conversationListPageURL,
|
||||
getArticleSearchURL,
|
||||
} from '../URLHelper';
|
||||
|
||||
describe('#URL Helpers', () => {
|
||||
@@ -75,4 +76,92 @@ describe('#URL Helpers', () => {
|
||||
expect(isValidURL('alert.window')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getArticleSearchURL', () => {
|
||||
it('should generate a basic URL without optional parameters', () => {
|
||||
const url = getArticleSearchURL({
|
||||
portalSlug: 'news',
|
||||
pageNumber: 1,
|
||||
locale: 'en',
|
||||
host: 'myurl.com',
|
||||
});
|
||||
expect(url).toBe('myurl.com/news/articles?page=1&locale=en');
|
||||
});
|
||||
|
||||
it('should include status parameter if provided', () => {
|
||||
const url = getArticleSearchURL({
|
||||
portalSlug: 'news',
|
||||
pageNumber: 1,
|
||||
locale: 'en',
|
||||
status: 'published',
|
||||
host: 'myurl.com',
|
||||
});
|
||||
expect(url).toBe(
|
||||
'myurl.com/news/articles?page=1&locale=en&status=published'
|
||||
);
|
||||
});
|
||||
|
||||
it('should include author_id parameter if provided', () => {
|
||||
const url = getArticleSearchURL({
|
||||
portalSlug: 'news',
|
||||
pageNumber: 1,
|
||||
locale: 'en',
|
||||
authorId: 123,
|
||||
host: 'myurl.com',
|
||||
});
|
||||
expect(url).toBe(
|
||||
'myurl.com/news/articles?page=1&locale=en&author_id=123'
|
||||
);
|
||||
});
|
||||
|
||||
it('should include category_slug parameter if provided', () => {
|
||||
const url = getArticleSearchURL({
|
||||
portalSlug: 'news',
|
||||
pageNumber: 1,
|
||||
locale: 'en',
|
||||
categorySlug: 'technology',
|
||||
host: 'myurl.com',
|
||||
});
|
||||
expect(url).toBe(
|
||||
'myurl.com/news/articles?page=1&locale=en&category_slug=technology'
|
||||
);
|
||||
});
|
||||
|
||||
it('should include sort parameter if provided', () => {
|
||||
const url = getArticleSearchURL({
|
||||
portalSlug: 'news',
|
||||
pageNumber: 1,
|
||||
locale: 'en',
|
||||
sort: 'views',
|
||||
host: 'myurl.com',
|
||||
});
|
||||
expect(url).toBe('myurl.com/news/articles?page=1&locale=en&sort=views');
|
||||
});
|
||||
|
||||
it('should handle multiple optional parameters', () => {
|
||||
const url = getArticleSearchURL({
|
||||
portalSlug: 'news',
|
||||
pageNumber: 1,
|
||||
locale: 'en',
|
||||
status: 'draft',
|
||||
authorId: 456,
|
||||
categorySlug: 'science',
|
||||
sort: 'views',
|
||||
host: 'myurl.com',
|
||||
});
|
||||
expect(url).toBe(
|
||||
'myurl.com/news/articles?page=1&locale=en&status=draft&author_id=456&category_slug=science&sort=views'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle missing optional parameters gracefully', () => {
|
||||
const url = getArticleSearchURL({
|
||||
portalSlug: 'news',
|
||||
pageNumber: 1,
|
||||
locale: 'en',
|
||||
host: 'myurl.com',
|
||||
});
|
||||
expect(url).toBe('myurl.com/news/articles?page=1&locale=en');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user