Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Aug 26, 2024
1 parent 4f0214f commit df86b3b
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 21 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

192 changes: 191 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe('getBlogPostAuthors', () => {
{
key: 'slorber',
name: 'Sébastien Lorber',
imageURL: '/baseUrl/img/slorber.png',
imageURL: '/img/slorber.png',
page: null,
},
]);
Expand Down Expand Up @@ -522,3 +522,193 @@ describe('groupBlogPostsByAuthorKey', () => {
});
});
});

describe('getBlogPostAuthors baseUrl', () => {
// Global author without baseUrl
it('getBlogPostAuthors do not modify global authors imageUrl without baseUrl', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: ['ozaki'],
},
authorsMap: {
ozaki: {
name: 'ozaki',
key: 'ozaki',
imageURL: 'ozaki.png',
page: null,
},
},
baseUrl: '/',
}),
).toEqual([
{
imageURL: 'ozaki.png',
key: 'ozaki',
name: 'ozaki',
page: null,
},
]);
});

// Global author with baseUrl
it('getBlogPostAuthors do not modify global authors imageUrl with baseUrl', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: ['ozaki'],
},
authorsMap: {
ozaki: {
name: 'ozaki',
key: 'ozaki',
imageURL: 'ozaki.png',
page: null,
},
},
baseUrl: '/img/',
}),
).toEqual([
{
imageURL: 'ozaki.png',
key: 'ozaki',
name: 'ozaki',
page: null,
},
]);
});

// Inline author without baseUrl
it('getBlogPostAuthors can return imageURL without baseUrl for inline authors', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: [{name: 'ozaki', imageURL: 'ozaki.png'}],
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
imageURL: 'ozaki.png',
key: null,
name: 'ozaki',
page: null,
},
]);
});

// Inline author with baseUrl
it('getBlogPostAuthors normalize imageURL with baseUrl for inline authors', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: [{name: 'ozaki', imageURL: '/ozaki.png'}],
},
authorsMap: undefined,
baseUrl: '/img/',
}),
).toEqual([
{
imageURL: '/img/ozaki.png',
key: null,
name: 'ozaki',
page: null,
},
]);
});

// Global author without baseUrl with a subfolder in img
it('getBlogPostAuthors do not modify globalAuthor imageUrl with subfolder without baseUrl', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: ['ozaki'],
},
authorsMap: {
ozaki: {
name: 'ozaki',
key: 'ozaki',
imageURL: 'img/ozaki.png',
page: null,
},
},
baseUrl: '/',
}),
).toEqual([
{
imageURL: 'img/ozaki.png',
key: 'ozaki',
name: 'ozaki',
page: null,
},
]);
});

// Global author with baseUrl with a subfolder in img
it('getBlogPostAuthors do not modify globalAuthor imageUrl with subfolder with baseUrl', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: ['ozaki'],
},
authorsMap: {
ozaki: {
name: 'ozaki',
key: 'ozaki',
imageURL: 'img/ozaki.png',
page: null,
},
},
baseUrl: '/img/',
}),
).toEqual([
{
imageURL: 'img/ozaki.png',
key: 'ozaki',
name: 'ozaki',
page: null,
},
]);
});

// Inline author without baseUrl with a subfolder in img
it('getBlogPostAuthors normalize imageURL from subfolder without baseUrl for inline authors', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: [{name: 'ozaki', imageURL: 'img/ozaki.png'}],
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
imageURL: 'img/ozaki.png',
key: null,
name: 'ozaki',
page: null,
},
]);
});

// Inline author with baseUrl with a subfolder in img
it('getBlogPostAuthors normalize imageURL from subfolder with baseUrl for inline authors', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: [{name: 'ozaki', imageURL: '/img/ozaki.png'}],
},
authorsMap: undefined,
baseUrl: '/img/',
}),
).toEqual([
{
imageURL: '/img/img/ozaki.png',
key: null,
name: 'ozaki',
page: null,
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,6 @@ describe('getAuthorsMap', () => {
}),
).resolves.toBeUndefined();
});

it('getAuthorsMap can return img with relative path if baseUrl is set', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/baseUrl',
});
expect(authorsMap).toBeDefined();
expect(authorsMap?.ozaki?.imageURL).toBe('/baseUrl/img/ozaki.jpg');
});
});

describe('validateAuthorsMapInput', () => {
Expand Down Expand Up @@ -319,3 +308,61 @@ describe('authors socials', () => {
expect(validateAuthorsMap(authorsMap)).toEqual(authorsMap);
});
});

describe('getAuthorsMap global author baseUrl', () => {
const fixturesDir = path.join(__dirname, '__fixtures__/authorsMapFiles');
const contentPaths = {
contentPathLocalized: fixturesDir,
contentPath: fixturesDir,
};

it('getAuthorsMap return imageURL with relative path', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/',
});
expect(authorsMap?.ozaki?.imageURL).toBe('/ozaki.png');
});

it('getAuthorsMap normalize imageURL with baseUrl', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/baseUrl/',
});
expect(authorsMap?.ozaki?.imageURL).toBe('/baseUrl/ozaki.png');
});

it('getAuthorsMap return imageURL with relative subdir path', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/',
});
expect(authorsMap?.ozakione?.imageURL).toBe('/img/ozaki.png');
});

it('getAuthorsMap normalize imageURL with baseUrl and subdir same value', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/img/',
});
expect(authorsMap?.ozakione?.imageURL).toBe('/img/img/ozaki.png');
});

it('getAuthorsMap normalize imageURL subdir with baseUrl', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/blog/',
});
expect(authorsMap?.ozakione?.imageURL).toBe('/blog/img/ozaki.png');
});
});
10 changes: 6 additions & 4 deletions packages/docusaurus-plugin-content-blog/src/authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ type AuthorsParam = {
baseUrl: string;
};

function normalizeImageUrl({
export function normalizeImageUrl({
imageURL,
baseUrl,
}: {
imageURL: string | undefined;
baseUrl: string;
}) {
return imageURL?.startsWith('/') && !imageURL.startsWith(baseUrl)
}): string | undefined {
return imageURL?.startsWith('/')
? normalizeUrl([baseUrl, imageURL])
: imageURL;
}
Expand Down Expand Up @@ -122,7 +122,9 @@ ${Object.keys(authorsMap)
...author,
key: author.key ?? null,
page: author.page ?? null,
imageURL: normalizeImageUrl({imageURL: author.imageURL, baseUrl}),
imageURL: author.key
? author.imageURL
: normalizeImageUrl({imageURL: author.imageURL, baseUrl}),
};
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/docusaurus-plugin-content-blog/src/authorsMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import _ from 'lodash';
import {readDataFile, normalizeUrl} from '@docusaurus/utils';
import {Joi, URISchema} from '@docusaurus/utils-validation';
import {AuthorSocialsSchema, normalizeSocials} from './authorsSocials';
import {normalizeImageUrl} from './authors';
import type {BlogContentPaths} from './types';
import type {
Author,
Expand Down Expand Up @@ -116,9 +117,7 @@ function normalizeAuthor({
...author,
key: authorKey,
page: getAuthorPage(),
imageURL: author.imageURL?.startsWith('/')
? normalizeUrl([baseUrl, author.imageURL])
: author.imageURL,
imageURL: normalizeImageUrl({imageURL: author.imageURL, baseUrl}),
socials: author.socials ? normalizeSocials(author.socials) : undefined,
};
}
Expand Down

0 comments on commit df86b3b

Please sign in to comment.