summaryrefslogtreecommitdiff
path: root/source/github-helpers/github-file-url.test.ts
blob: 407561c6a024df85b50ce553e7ae43446834baa5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {test, assert} from 'vitest';

import GitHubFileURL from './github-file-url.js';

test('branch', () => {
	const url = new GitHubFileURL('https://github.com/microsoft/TypeScript/tree/master');
	assert.equal(url.user, 'microsoft');
	assert.equal(url.repository, 'TypeScript');
	assert.equal(url.route, 'tree');
	assert.equal(url.branch, 'master');
	assert.equal(url.filePath, '');
	assert.equal(url.pathname, '/microsoft/TypeScript/tree/master');
	assert.equal(url.href, 'https://github.com/microsoft/TypeScript/tree/master');
	assert.equal(String(url), 'https://github.com/microsoft/TypeScript/tree/master');
});

test('branch with multiple slashes', () => {
	const url = new GitHubFileURL('https://github.com/yakov116/TestR/tree/this/branch%2Fhas%2Fmany%2Fslashes');
	assert.equal(url.user, 'yakov116');
	assert.equal(url.repository, 'TestR');
	assert.equal(url.route, 'tree');
	assert.equal(url.branch, 'this/branch/has/many/slashes');
	assert.equal(url.filePath, '');
	assert.equal(url.pathname, '/yakov116/TestR/tree/this/branch/has/many/slashes');
	assert.equal(url.href, 'https://github.com/yakov116/TestR/tree/this/branch/has/many/slashes');
	assert.equal(String(url), 'https://github.com/yakov116/TestR/tree/this/branch/has/many/slashes');
});

test('object', () => {
	const url = new GitHubFileURL('https://github.com/microsoft/TypeScript/tree/master/src');
	assert.equal(url.user, 'microsoft');
	assert.equal(url.repository, 'TypeScript');
	assert.equal(url.route, 'tree');
	assert.equal(url.branch, 'master');
	assert.equal(url.filePath, 'src');
	assert.equal(url.pathname, '/microsoft/TypeScript/tree/master/src');
	assert.equal(url.href, 'https://github.com/microsoft/TypeScript/tree/master/src');
	assert.equal(String(url), 'https://github.com/microsoft/TypeScript/tree/master/src');
});

test('nested object', () => {
	const url = new GitHubFileURL('https://github.com/microsoft/TypeScript/tree/master/src/index.js');
	assert.equal(url.user, 'microsoft');
	assert.equal(url.repository, 'TypeScript');
	assert.equal(url.route, 'tree');
	assert.equal(url.branch, 'master');
	assert.equal(url.filePath, 'src/index.js');
	assert.equal(url.pathname, '/microsoft/TypeScript/tree/master/src/index.js');
	assert.equal(url.href, 'https://github.com/microsoft/TypeScript/tree/master/src/index.js');
	assert.equal(String(url), 'https://github.com/microsoft/TypeScript/tree/master/src/index.js');
});

test('change branch', () => {
	const url = new GitHubFileURL('https://github.com/microsoft/TypeScript/tree/master/src/index.js').assign({
		branch: 'dev',
	});
	assert.equal(url.user, 'microsoft');
	assert.equal(url.repository, 'TypeScript');
	assert.equal(url.route, 'tree');
	assert.equal(url.branch, 'dev');
	assert.equal(url.filePath, 'src/index.js');
	assert.equal(url.pathname, '/microsoft/TypeScript/tree/dev/src/index.js');
	assert.equal(url.href, 'https://github.com/microsoft/TypeScript/tree/dev/src/index.js');
	assert.equal(String(url), 'https://github.com/microsoft/TypeScript/tree/dev/src/index.js');
});

test('change filePath', () => {
	const url = new GitHubFileURL('https://github.com/microsoft/TypeScript/tree/master/src/index.js').assign({
		filePath: 'package.json',
	});
	assert.equal(url.user, 'microsoft');
	assert.equal(url.repository, 'TypeScript');
	assert.equal(url.route, 'tree');
	assert.equal(url.branch, 'master');
	assert.equal(url.filePath, 'package.json');
	assert.equal(url.pathname, '/microsoft/TypeScript/tree/master/package.json');
	assert.equal(url.href, 'https://github.com/microsoft/TypeScript/tree/master/package.json');
	assert.equal(String(url), 'https://github.com/microsoft/TypeScript/tree/master/package.json');
});

test('get filePath from search', () => {
	const url = new GitHubFileURL('https://github.com/yakov116/refined-github/commits/f23b687b3b89aa95a76193722cdfeff740646670?after=f23b687b3b89aa95a76193722cdfeff740646670+34&path%5B%5D=source&path%5B%5D=features&path%5B%5D=release-download-count.tsx');
	assert.equal(url.user, 'yakov116');
	assert.equal(url.repository, 'refined-github');
	assert.equal(url.route, 'commits');
	assert.equal(url.branch, 'f23b687b3b89aa95a76193722cdfeff740646670');
	assert.equal(url.filePath, 'source/features/release-download-count.tsx');
	assert.equal(url.pathname, '/yakov116/refined-github/commits/f23b687b3b89aa95a76193722cdfeff740646670/source/features/release-download-count.tsx');
	assert.equal(url.href, 'https://github.com/yakov116/refined-github/commits/f23b687b3b89aa95a76193722cdfeff740646670/source/features/release-download-count.tsx?after=f23b687b3b89aa95a76193722cdfeff740646670+34');
	assert.equal(url.search, '?after=f23b687b3b89aa95a76193722cdfeff740646670+34');
	assert.equal(String(url), 'https://github.com/yakov116/refined-github/commits/f23b687b3b89aa95a76193722cdfeff740646670/source/features/release-download-count.tsx?after=f23b687b3b89aa95a76193722cdfeff740646670+34');
});