Files

82 lines
2.8 KiB
JavaScript

import { module, test } from 'qunit';
import {
apiBaseUrl,
authorizationHeader,
isGithubDotCom,
nextLink,
normalizeGithubUrl,
notificationsUrl,
} from 'dashtab/config/github';
module('Unit | Config | github', function () {
test('normalizeGithubUrl normalizes input and rejects junk', function (assert) {
assert.strictEqual(normalizeGithubUrl('github.com'), 'https://github.com');
assert.strictEqual(
normalizeGithubUrl('https://github.com/'),
'https://github.com'
);
assert.strictEqual(
normalizeGithubUrl('http://git.example.com'),
'http://git.example.com'
);
assert.strictEqual(normalizeGithubUrl(''), null);
assert.strictEqual(normalizeGithubUrl(' '), null);
assert.strictEqual(normalizeGithubUrl('not a valid url'), null);
});
test('isGithubDotCom recognizes github.com hosts', function (assert) {
assert.true(isGithubDotCom('https://github.com'));
assert.true(isGithubDotCom('https://www.github.com'));
assert.false(isGithubDotCom('https://git.example.com'));
assert.false(isGithubDotCom(''));
});
test('apiBaseUrl uses the dedicated host for github.com and /api/v3 otherwise', function (assert) {
assert.strictEqual(apiBaseUrl(), 'https://api.github.com');
assert.strictEqual(
apiBaseUrl('https://github.com'),
'https://api.github.com'
);
assert.strictEqual(
apiBaseUrl('https://git.example.com'),
'https://git.example.com/api/v3'
);
});
test('notificationsUrl requests the maximum page size', function (assert) {
assert.strictEqual(
notificationsUrl('https://github.com'),
'https://api.github.com/notifications?per_page=50'
);
assert.strictEqual(
notificationsUrl('https://git.example.com'),
'https://git.example.com/api/v3/notifications?per_page=50'
);
});
test('authorizationHeader uses a bearer token', function (assert) {
assert.strictEqual(authorizationHeader('ghp_x'), 'Bearer ghp_x');
});
test('nextLink extracts the next page and tolerates its absence', function (assert) {
assert.strictEqual(
nextLink(
'<https://api.github.com/notifications?per_page=50&page=2>; rel="next", <https://api.github.com/notifications?per_page=50&page=7>; rel="last"'
),
'https://api.github.com/notifications?per_page=50&page=2'
);
assert.strictEqual(
nextLink(
'<https://api.github.com/notifications?per_page=50&after=cursor,1&page=2>; rel="next"'
),
'https://api.github.com/notifications?per_page=50&after=cursor,1&page=2'
);
assert.strictEqual(
nextLink('<https://api.github.com/notifications?page=7>; rel="last"'),
null
);
assert.strictEqual(nextLink(null), null);
assert.strictEqual(nextLink('garbage'), null);
});
});