Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 19 additions & 224 deletions apps/web/src/common/utils/url.test.ts
Original file line number Diff line number Diff line change
@@ -1,226 +1,21 @@
import {
getPathname,
getProvider,
getNameSpace,
getRepoName,
getLastPathSegment,
getRepoOrigin,
getRepoPath,
getRepoUrlByName,
removeExtname,
removeTrailingSlash,
getNameSpacePng,
} from './url';

describe('utils url ', () => {
it('getPathname', () => {
const testCases = [
{
input: 'https://github.com/EdmondFrank/.emacs.d',
result: 'EdmondFrank/.emacs.d',
},
{
input: 'https://github.com/ant-design/ant-design',
result: 'ant-design/ant-design',
},
{
input: 'https://gitee.com/dotnetchina/MiniWord',
result: 'dotnetchina/MiniWord',
},
];
testCases.map((item) => {
expect(getPathname(item.input)).toEqual(item.result);
});
});

it('getProvider', () => {
const testCases = [
{ input: 'https://github.com/EdmondFrank/.emacs.d', result: 'github' },
{ input: 'https://github.com/ant-design/ant-design', result: 'github' },
{ input: 'https://gitee.com/dotnetchina/MiniWord', result: 'gitee' },
{ input: 'http://gitcode.com/org/repo', result: 'gitcode' },
];
testCases.map((item) => {
expect(getProvider(item.input)).toEqual(item.result);
});
});

it('getRepoOrigin', () => {
const testCases = [
{ input: 'https://github.com/ant-design/ant-design', result: 'github' },
{ input: 'gitee:dotnetchina/MiniWord', result: 'gitee' },
{ input: 'gitcode.com/org/repo', result: 'gitcode' },
{ input: 'owner/repo', result: 'gitcode', fallbackOrigin: 'gitcode' },
];

testCases.map((item) => {
expect(getRepoOrigin(item.input, item.fallbackOrigin)).toEqual(
item.result
);
});
});

it('getRepoPath', () => {
const testCases = [
{
input: 'https://github.com/ant-design/ant-design',
result: 'ant-design/ant-design',
},
{ input: 'gitee:dotnetchina/MiniWord', result: 'dotnetchina/MiniWord' },
{ input: 'gitcode.com/org/repo', result: 'org/repo' },
{ input: 'owner/repo', result: 'owner/repo' },
];

testCases.map((item) => {
expect(getRepoPath(item.input)).toEqual(item.result);
});
});

it('getRepoUrlByName', () => {
const testCases = [
{
input: 'https://github.com/ant-design/ant-design/',
result: 'https://github.com/ant-design/ant-design',
},
{
input: 'gitee:dotnetchina/MiniWord',
result: 'https://gitee.com/dotnetchina/MiniWord',
},
{
input: 'gitcode.com/org/repo',
result: 'https://gitcode.com/org/repo',
},
{
input: 'owner/repo',
result: 'https://gitcode.com/owner/repo',
fallbackOrigin: 'gitcode',
},
];

testCases.map((item) => {
expect(getRepoUrlByName(item.input, item.fallbackOrigin)).toEqual(
item.result
);
});
});

it('getLastPathSegment', () => {
const testCases = [
{ input: 'https://github.com/EdmondFrank/.emacs.d', result: '.emacs.d' },
{
input: 'https://github.com/ant-design/ant-design',
result: 'ant-design',
},
{ input: 'https://gitee.com/dotnetchina/MiniWord', result: 'MiniWord' },
];

testCases.map((item) => {
expect(getLastPathSegment(item.input)).toEqual(item.result);
});
});

it('getNameSpace', function () {
const testCases = [
{
input: 'https://github.com/EdmondFrank/.emacs.d',
result: 'EdmondFrank',
},
{
input: 'https://github.com/ant-design/ant-design',
result: 'ant-design',
},
{
input: 'https://gitee.com/dotnetchina/MiniWord/',
result: 'dotnetchina',
},
];

testCases.map((item) => {
expect(getNameSpace(item.input)).toEqual(item.result);
});
});

it('getRepoName', function () {
const testCases = [
{
input: 'https://github.com/EdmondFrank/.emacs.d',
result: '.emacs.d',
},
{
input: 'https://github.com/ant-design/ant-design',
result: 'ant-design',
},
{
input: 'https://gitee.com/dotnetchina/MiniWord/',
result: 'MiniWord',
},
];

testCases.map((item) => {
expect(getRepoName(item.input)).toEqual(item.result);
});
});

it('removeUrlDotGit', function () {
const testCases = [
{
input: 'https://github.com/EdmondFrank/.emacs.d',
result: 'https://github.com/EdmondFrank/.emacs.d',
},
{
input: 'https://github.com/ant-design/ant-design.git',
result: 'https://github.com/ant-design/ant-design',
},
{
input: undefined,
result: '',
},
];

testCases.map((item) => {
expect(removeExtname(item.input, '.git')).toEqual(item.result);
});
});

it('removeTrailingSlash', function () {
const testCases = [
{
input: 'EdmondFrank/.emacs.d/',
result: 'EdmondFrank/.emacs.d',
},
{
input: 'ant-design/ant-design/',
result: 'ant-design/ant-design',
},
{
input: undefined,
result: '',
},
];

testCases.map((item) => {
expect(removeTrailingSlash(item.input)).toEqual(item.result);
});
});

it('getNameSpacePng', function () {
const testCases = [
{
input: 'https://github.com/EdmondFrank/.emacs.d',
result: 'https://github.com/EdmondFrank.png',
},
{
input: 'https://github.com/ant-design/ant-design.git',
result: 'https://github.com/ant-design.png',
},
{
input: undefined,
result: '/images/default.png',
},
];

testCases.map((item) => {
expect(getNameSpacePng(item.input)).toEqual(item.result);
});
import { fillHttps } from './url';

describe('fillHttps', () => {
it('adds the scheme only when the url has none', () => {
expect(fillHttps('github.com/cli/cli')).toEqual(
'https://github.com/cli/cli'
);
expect(fillHttps('github.com/cli/cli/')).toEqual(
'https://github.com/cli/cli'
);
expect(fillHttps('https://github.com/cli/cli')).toEqual(
'https://github.com/cli/cli'
);
});

it('keeps an http url instead of stacking a second scheme', () => {
expect(fillHttps('http://github.com/cli/cli')).toEqual(
'http://github.com/cli/cli'
);
});
});
4 changes: 3 additions & 1 deletion apps/web/src/common/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export function getProvider(url: string) {
export function fillHttps(url?: string): string {
if (!url) return '';
const trimmedUrl = url.endsWith('/') ? url.slice(0, -1) : url;
if (trimmedUrl.indexOf('https') === -1) {
// `indexOf('https') === -1` treated an existing "http://" as "no scheme" and
// produced "https://http://...". Only add the scheme when there is none.
if (!/^https?:\/\//i.test(trimmedUrl)) {
return `https://${trimmedUrl}`;
}
return trimmedUrl;
Expand Down