docusaurus/packages/docusaurus-plugin-content-blog/src/__tests__/authorsSocials.test.ts
Sébastien Lorber f6bdc3123b
refactor(theme): refactor CodeBlock parseLines logic + use inline snapshots to ease review (#11058)
* refactor codeblock parseLines logic + use inline snapshots

* refactor: apply lint autofix

* eslint

---------

Co-authored-by: slorber <749374+slorber@users.noreply.github.com>
2025-04-04 13:22:51 +02:00

136 lines
4.3 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {normalizeSocials} from '../authorsSocials';
import type {AuthorSocials} from '@docusaurus/plugin-content-blog';
describe('normalizeSocials', () => {
it('only username', () => {
const socials: AuthorSocials = {
twitter: 'ozakione',
linkedin: 'ozakione',
github: 'ozakione',
stackoverflow: 'ozakione',
threads: 'gingergeekuk',
bluesky: 'gingergeek.co.uk',
instagram: 'thisweekinreact',
twitch: 'gingergeek',
youtube: 'gingergeekuk',
mastodon: 'Mastodon',
};
expect(normalizeSocials(socials)).toMatchInlineSnapshot(`
{
"bluesky": "https://bsky.app/profile/gingergeek.co.uk",
"github": "https://github.com/ozakione",
"instagram": "https://www.instagram.com/thisweekinreact",
"linkedin": "https://www.linkedin.com/in/ozakione/",
"mastodon": "https://mastodon.social/@Mastodon",
"stackoverflow": "https://stackoverflow.com/users/ozakione",
"threads": "https://www.threads.net/@gingergeekuk",
"twitch": "https://twitch.tv/gingergeek",
"twitter": "https://twitter.com/ozakione",
"youtube": "https://youtube.com/@gingergeekuk",
}
`);
});
it('only username - case insensitive', () => {
const socials: AuthorSocials = {
Twitter: 'ozakione',
linkedIn: 'ozakione',
gitHub: 'ozakione',
STACKoverflow: 'ozakione',
instaGRam: 'thisweekinreact',
BLUESKY: 'gingergeek.co.uk',
tHrEaDs: 'gingergeekuk',
};
expect(normalizeSocials(socials)).toMatchInlineSnapshot(`
{
"bluesky": "https://bsky.app/profile/gingergeek.co.uk",
"github": "https://github.com/ozakione",
"instagram": "https://www.instagram.com/thisweekinreact",
"linkedin": "https://www.linkedin.com/in/ozakione/",
"stackoverflow": "https://stackoverflow.com/users/ozakione",
"threads": "https://www.threads.net/@gingergeekuk",
"twitter": "https://twitter.com/ozakione",
}
`);
});
it('only links', () => {
const socials: AuthorSocials = {
twitter: 'https://x.com/ozakione',
linkedin: 'https://linkedin.com/ozakione',
github: 'https://github.com/ozakione',
stackoverflow: 'https://stackoverflow.com/ozakione',
};
expect(normalizeSocials(socials)).toEqual(socials);
});
it('mixed links', () => {
const socials: AuthorSocials = {
twitter: 'ozakione',
linkedin: 'ozakione',
github: 'https://github.com/ozakione',
stackoverflow: 'https://stackoverflow.com/ozakione',
mastodon: 'https://hachyderm.io/@hachyderm',
};
expect(normalizeSocials(socials)).toMatchInlineSnapshot(`
{
"github": "https://github.com/ozakione",
"linkedin": "https://www.linkedin.com/in/ozakione/",
"mastodon": "https://hachyderm.io/@hachyderm",
"stackoverflow": "https://stackoverflow.com/ozakione",
"twitter": "https://twitter.com/ozakione",
}
`);
});
it('one link', () => {
const socials: AuthorSocials = {
twitter: 'ozakione',
};
expect(normalizeSocials(socials)).toMatchInlineSnapshot(`
{
"twitter": "https://twitter.com/ozakione",
}
`);
});
it('rejects strings that do not look like username/userId/handle or fully-qualified URLs', () => {
const socials: AuthorSocials = {
twitter: '/ozakione/XYZ',
};
expect(() => normalizeSocials(socials)).toThrowErrorMatchingInlineSnapshot(`
"Author socials should be usernames/userIds/handles, or fully qualified HTTP(s) absolute URLs.
Social platform 'twitter' has illegal value '/ozakione/XYZ'"
`);
});
it('allow other form of urls', () => {
const socials: AuthorSocials = {
twitter: 'https://bit.ly/sebastienlorber-twitter',
};
expect(normalizeSocials(socials)).toEqual(socials);
});
it('allow unknown social platforms urls', () => {
const socials: AuthorSocials = {
twitch: 'https://www.twitch.tv/sebastienlorber',
newsletter: 'https://thisweekinreact.com',
};
expect(normalizeSocials(socials)).toEqual(socials);
});
});