From 7c05f2c24eda58c4b87ea88e58b3ae1ed1be3073 Mon Sep 17 00:00:00 2001 From: Federico Date: Mon, 26 Aug 2024 20:34:54 +0700 Subject: [PATCH] Cleanup tests, use snapshots --- __snapshots__/index.test.js.snap | 263 ++++++++++++ index.test.js | 700 +++++++------------------------ 2 files changed, 417 insertions(+), 546 deletions(-) create mode 100644 __snapshots__/index.test.js.snap diff --git a/__snapshots__/index.test.js.snap b/__snapshots__/index.test.js.snap new file mode 100644 index 0000000..a18cc02 --- /dev/null +++ b/__snapshots__/index.test.js.snap @@ -0,0 +1,263 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`http://www.google.com/ 1`] = `http://www.google.com`; + +exports[`https://cdn.rawgit.com/fregante/shorten-repo-url/d71718db/.gitignore 1`] = `d71718db/.gitignore (raw)`; + +exports[`https://cdn.rawgit.com/fregante/shorten-repo-url/v0.12/.gitignore 1`] = `v0.12/.gitignore (raw)`; + +exports[`https://cdn.rawgit.com/nodejs/node/d71718db/.gitignore 1`] = `nodejs/node@d71718db/.gitignore (raw)`; + +exports[`https://cdn.rawgit.com/nodejs/node/v0.12/.gitignore 1`] = `nodejs/node@v0.12/.gitignore (raw)`; + +exports[`https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement#parameters 1`] = `developer.mozilla.org/en-US/docs/Web/API/Document/createElement#parameters`; + +exports[`https://example.com/nodejs/node/blob/cc8fc46/.gitignore 1`] = `example.com/nodejs/node/blob/cc8fc46/.gitignore`; + +exports[`https://example.site/한글로-된-URL 1`] = `example.site/한글로-된-URL`; + +exports[`https://github.com/ 1`] = `github.com`; + +exports[`https://github.com/bfred-it/shorten-repo-url/network/dependencies 1`] = `bfred-it/shorten-repo-url (dependencies)`; + +exports[`https://github.com/bfred-it/shorten-repo-url/network/dependents 1`] = `bfred-it/shorten-repo-url (dependents)`; + +exports[`https://github.com/bfred-it/shorten-repo-url/wiki 1`] = `bfred-it/shorten-repo-url/wiki`; + +exports[`https://github.com/features 1`] = `github.com/features`; + +exports[`https://github.com/fregante/shorten-repo-url/ 1`] = `fregante/shorten-repo-url`; + +exports[`https://github.com/fregante/shorten-repo-url/?tab=readme-ov-file 1`] = `fregante/shorten-repo-url`; + +exports[`https://github.com/fregante/shorten-repo-url/archive/6.4.1.zip 1`] = `6.4.1.zip`; + +exports[`https://github.com/fregante/shorten-repo-url/blame/cc8fc46/.gitignore 1`] = `cc8fc46/.gitignore (blame)`; + +exports[`https://github.com/fregante/shorten-repo-url/blame/master/.gitignore 1`] = `master/.gitignore (blame)`; + +exports[`https://github.com/fregante/shorten-repo-url/blame/v0.12/.gitignore 1`] = `v0.12/.gitignore (blame)`; + +exports[`https://github.com/fregante/shorten-repo-url/blob/cc8fc46/.gitignore 1`] = `cc8fc46/.gitignore`; + +exports[`https://github.com/fregante/shorten-repo-url/blob/master/.gitignore 1`] = `master/.gitignore`; + +exports[`https://github.com/fregante/shorten-repo-url/blob/v0.12/.gitignore 1`] = `v0.12/.gitignore`; + +exports[`https://github.com/fregante/shorten-repo-url/commit/cc8fc46.diff 1`] = `cc8fc46.diff`; + +exports[`https://github.com/fregante/shorten-repo-url/commit/cc8fc46.patch 1`] = `cc8fc46.patch`; + +exports[`https://github.com/fregante/shorten-repo-url/commits/cc8fc46/.gitignore 1`] = `cc8fc46/.gitignore (commits)`; + +exports[`https://github.com/fregante/shorten-repo-url/commits/master/.gitignore 1`] = `master/.gitignore (commits)`; + +exports[`https://github.com/fregante/shorten-repo-url/commits/v0.12/.gitignore 1`] = `v0.12/.gitignore (commits)`; + +exports[`https://github.com/fregante/shorten-repo-url/compare 1`] = `fregante/shorten-repo-url/compare`; + +exports[`https://github.com/fregante/shorten-repo-url/compare/d71718db6aa4feb8dc10edbad1134472468e971a 1`] = `d71718d (compare)`; + +exports[`https://github.com/fregante/shorten-repo-url/compare/master 1`] = `master (compare)`; + +exports[`https://github.com/fregante/shorten-repo-url/compare/master...master 1`] = `master...master (compare)`; + +exports[`https://github.com/fregante/shorten-repo-url/contributors 1`] = `fregante/shorten-repo-url/contributors`; + +exports[`https://github.com/fregante/shorten-repo-url/issues 1`] = `fregante/shorten-repo-url/issues`; + +exports[`https://github.com/fregante/shorten-repo-url/issues?q=is%3Aissue++is%3Aopen+sort%3Aupdated-desc+&unrelated=true 1`] = `fregante/shorten-repo-url/issues?unrelated=true (is:open sort:updated-desc)`; + +exports[`https://github.com/fregante/shorten-repo-url/issues?q=wow 1`] = `fregante/shorten-repo-url/issues (wow)`; + +exports[`https://github.com/fregante/shorten-repo-url/labels 1`] = `fregante/shorten-repo-url/labels`; + +exports[`https://github.com/fregante/shorten-repo-url/labels/npm 1`] = `npm (label)`; + +exports[`https://github.com/fregante/shorten-repo-url/milestone/25 1`] = `fregante/shorten-repo-url/milestone/25`; + +exports[`https://github.com/fregante/shorten-repo-url/milestones 1`] = `fregante/shorten-repo-url/milestones`; + +exports[`https://github.com/fregante/shorten-repo-url/network 1`] = `fregante/shorten-repo-url/network`; + +exports[`https://github.com/fregante/shorten-repo-url/projects 1`] = `fregante/shorten-repo-url/projects`; + +exports[`https://github.com/fregante/shorten-repo-url/pull/123/checks 1`] = `#123 (checks)`; + +exports[`https://github.com/fregante/shorten-repo-url/pull/123/commits 1`] = `#123 (commits)`; + +exports[`https://github.com/fregante/shorten-repo-url/pull/123/files 1`] = `#123 (files)`; + +exports[`https://github.com/fregante/shorten-repo-url/pulse 1`] = `fregante/shorten-repo-url/pulse`; + +exports[`https://github.com/fregante/shorten-repo-url/releases 1`] = `fregante/shorten-repo-url/releases`; + +exports[`https://github.com/fregante/shorten-repo-url/releases/download/6.4.1/now-macos 1`] = `6.4.1 now-macos (download)`; + +exports[`https://github.com/fregante/shorten-repo-url/releases/tag/v0.12.0 1`] = `v0.12.0 (release)`; + +exports[`https://github.com/fregante/shorten-repo-url/tree/d71718db6aa4feb8dc10edbad1134472468e971a 1`] = `d71718d`; + +exports[`https://github.com/fregante/shorten-repo-url/tree/d71718db6aa4feb8dc10edbad1134472468e971a/doc 1`] = `d71718d/doc`; + +exports[`https://github.com/fregante/shorten-repo-url/tree/master/doc 1`] = `master/doc`; + +exports[`https://github.com/fregante/shorten-repo-url/tree/v0.12 1`] = `v0.12`; + +exports[`https://github.com/fregante/shorten-repo-url/tree/v0.12/doc 1`] = `v0.12/doc`; + +exports[`https://github.com/fregante/shorten-repo-url/wiki/%22Can-you-add-this-feature%3F%22 1`] = `Wiki: "Can you add this feature?"`; + +exports[`https://github.com/issues 1`] = `github.com/issues`; + +exports[`https://github.com/issues?q=is%3Aissue++is%3Aopen+sort%3Aupdated-desc+&unrelated=true 1`] = `github.com/issues?unrelated=true (is:open sort:updated-desc)`; + +exports[`https://github.com/marketplace 1`] = `github.com/marketplace`; + +exports[`https://github.com/network/dependencies 1`] = `github.com/network/dependencies`; + +exports[`https://github.com/nodejs 1`] = `@nodejs`; + +exports[`https://github.com/nodejs/node/ 1`] = `nodejs/node`; + +exports[`https://github.com/nodejs/node/blame/cc8fc46/.gitignore 1`] = `nodejs/node@cc8fc46/.gitignore (blame)`; + +exports[`https://github.com/nodejs/node/blame/master/.gitignore 1`] = `nodejs/node@master/.gitignore (blame)`; + +exports[`https://github.com/nodejs/node/blame/v0.12/.gitignore 1`] = `nodejs/node@v0.12/.gitignore (blame)`; + +exports[`https://github.com/nodejs/node/blob/cc8fc46/.gitignore 1`] = `nodejs/node@cc8fc46/.gitignore`; + +exports[`https://github.com/nodejs/node/blob/master/.gitignore 1`] = `nodejs/node@master/.gitignore`; + +exports[`https://github.com/nodejs/node/blob/v0.12/.gitignore 1`] = `nodejs/node@v0.12/.gitignore`; + +exports[`https://github.com/nodejs/node/commit/cc8fc46.diff 1`] = `nodejs/node@cc8fc46.diff`; + +exports[`https://github.com/nodejs/node/commit/cc8fc46.patch 1`] = `nodejs/node@cc8fc46.patch`; + +exports[`https://github.com/nodejs/node/commits/cc8fc46/.gitignore 1`] = `nodejs/node@cc8fc46/.gitignore (commits)`; + +exports[`https://github.com/nodejs/node/commits/master/.gitignore 1`] = `nodejs/node@master/.gitignore (commits)`; + +exports[`https://github.com/nodejs/node/commits/v0.12/.gitignore 1`] = `nodejs/node@v0.12/.gitignore (commits)`; + +exports[`https://github.com/nodejs/node/compare 1`] = `nodejs/node/compare`; + +exports[`https://github.com/nodejs/node/compare/d71718db6aa4feb8dc10edbad1134472468e971a 1`] = `nodejs/node@d71718d (compare)`; + +exports[`https://github.com/nodejs/node/compare/master 1`] = `nodejs/node@master (compare)`; + +exports[`https://github.com/nodejs/node/compare/master...master 1`] = `nodejs/node@master...master (compare)`; + +exports[`https://github.com/nodejs/node/contributors 1`] = `nodejs/node/contributors`; + +exports[`https://github.com/nodejs/node/graphs/commit-activity 1`] = `nodejs/node/graphs/commit-activity`; + +exports[`https://github.com/nodejs/node/labels 1`] = `nodejs/node/labels`; + +exports[`https://github.com/nodejs/node/labels/Please%21%20♥ 1`] = `nodejs/node/Please! ♥ (label)`; + +exports[`https://github.com/nodejs/node/labels/npm 1`] = `nodejs/node/npm (label)`; + +exports[`https://github.com/nodejs/node/milestone/25 1`] = `nodejs/node/milestone/25`; + +exports[`https://github.com/nodejs/node/milestones 1`] = `nodejs/node/milestones`; + +exports[`https://github.com/nodejs/node/network 1`] = `nodejs/node/network`; + +exports[`https://github.com/nodejs/node/projects 1`] = `nodejs/node/projects`; + +exports[`https://github.com/nodejs/node/pull/123/files 1`] = `nodejs/node#123 (files)`; + +exports[`https://github.com/nodejs/node/pulse 1`] = `nodejs/node/pulse`; + +exports[`https://github.com/nodejs/node/releases 1`] = `nodejs/node/releases`; + +exports[`https://github.com/nodejs/node/releases/tag/v0.12.0 1`] = `nodejs/node@v0.12.0 (release)`; + +exports[`https://github.com/nodejs/node/tree/d71718db6aa4feb8dc10edbad1134472468e971a 1`] = `nodejs/node@d71718d`; + +exports[`https://github.com/nodejs/node/tree/d71718db6aa4feb8dc10edbad1134472468e971a/doc 1`] = `nodejs/node@d71718d/doc`; + +exports[`https://github.com/nodejs/node/tree/master/doc 1`] = `nodejs/node@master/doc`; + +exports[`https://github.com/nodejs/node/tree/v0.12 1`] = `nodejs/node@v0.12`; + +exports[`https://github.com/nodejs/node/tree/v0.12/doc 1`] = `nodejs/node@v0.12/doc`; + +exports[`https://github.com/nodejs/node/wiki 1`] = `nodejs/node/wiki`; + +exports[`https://github.com/nodejs/shorten-repo-url/ 1`] = `nodejs/shorten-repo-url`; + +exports[`https://github.com/pulls 1`] = `github.com/pulls`; + +exports[`https://github.com/pulls?q=is%3Apr++is%3Aopen+sort%3Aupdated-desc+&unrelated=true 1`] = `github.com/pulls?unrelated=true (is:open sort:updated-desc)`; + +exports[`https://github.com/refined-github/refined-github/labels/Please%21%20♥%EF%B8%8E 1`] = `refined-github/refined-github/Please! ♥︎ (label)`; + +exports[`https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22 1`] = `Wiki: "Can you add this feature?" (refined-github/refined-github)`; + +exports[`https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22# 1`] = `Wiki: "Can you add this feature?" (refined-github/refined-github)`; + +exports[`https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22#3-it-doesnt-require-options 1`] = `Wiki: "Can you add this feature?" (3 it doesnt require options) (refined-github/refined-github)`; + +exports[`https://github.com/settings/profile 1`] = `github.com/settings/profile`; + +exports[`https://github.com/sindresorhus 1`] = `@sindresorhus`; + +exports[`https://github.com/sindresorhus/notifier-for-github/pull/253/files/6b4489d417c9425dc27c5fb8d6b4a8518debd035..60cdcf3c3646164441bf8f037cef620479cdec59 1`] = `6b4489d4..60cdcf3c (#253)`; + +exports[`https://github.com/trending 1`] = `github.com/trending`; + +exports[`https://github.com/trending/developers 1`] = `github.com/trending/developers`; + +exports[`https://github.com/zeit/now-cli/archive/6.4.1.zip 1`] = `zeit/now-cli@6.4.1.zip`; + +exports[`https://github.com/zeit/now-cli/releases/download/6.4.1/now-macos 1`] = `zeit/now-cli@6.4.1 now-macos (download)`; + +exports[`https://raw.githubusercontent.com/fregante/shorten-repo-url/d71718db/.gitignore 1`] = `d71718db/.gitignore (raw)`; + +exports[`https://raw.githubusercontent.com/fregante/shorten-repo-url/master/.gitignore 1`] = `master/.gitignore (raw)`; + +exports[`https://raw.githubusercontent.com/fregante/shorten-repo-url/v0.12/.gitignore 1`] = `v0.12/.gitignore (raw)`; + +exports[`https://raw.githubusercontent.com/nodejs/node/d71718db/.gitignore 1`] = `nodejs/node@d71718db/.gitignore (raw)`; + +exports[`https://raw.githubusercontent.com/nodejs/node/master/.gitignore 1`] = `nodejs/node@master/.gitignore (raw)`; + +exports[`https://raw.githubusercontent.com/nodejs/node/v0.12/.gitignore 1`] = `nodejs/node@v0.12/.gitignore (raw)`; + +exports[`https://rawgit.com/fregante/shorten-repo-url/master/.gitignore 1`] = `master/.gitignore (raw)`; + +exports[`https://rawgit.com/nodejs/node/master/.gitignore 1`] = `nodejs/node@master/.gitignore (raw)`; + +exports[`https://togithub.com/fregante/shorten-repo-url/commit/98c6175b0cbd4caca71d24e68e57b942b0dfb549 1`] = `98c6175`; + +exports[`https://togithub.com/fregante/shorten-repo-url/issues/25 1`] = `#25`; + +exports[`https://togithub.com/fregante/shorten-repo-url/issues/28#issue-850900171 1`] = `#28 (comment)`; + +exports[`https://togithub.com/fregante/shorten-repo-url/pull/32 1`] = `#32`; + +exports[`https://togithub.com/fregante/shorten-repo-url/pull/32/files 1`] = `#32 (files)`; + +exports[`https://togithub.com/fregante/shorten-repo-url/pull/33#discussion_r750069394 1`] = `#33 (review)`; + +exports[`https://togithub.com/fregante/shorten-repo-url/pull/33#pullrequestreview-801229042 1`] = `#33 (review)`; + +exports[`https://togithub.com/nodejs/node/pull/123 1`] = `nodejs/node#123`; + +exports[`https://togithub.com/nodejs/node/pull/123/files 1`] = `nodejs/node#123 (files)`; + +exports[`https://togithub.com/refined-github/refined-github/commit/4f270c4f50e0a2a20085a6e92095117f10340322 1`] = `refined-github/refined-github@4f270c4`; + +exports[`https://togithub.com/refined-github/refined-github/commit/e81a9646b448d90c7e02ab41332cab0507dccbbd#commitcomment-60089354 1`] = `refined-github/refined-github@e81a964 (comment)`; + +exports[`https://www.google.com/ 1`] = `google.com`; + +exports[`https://www.npmjs.com/ 1`] = `npmjs.com`; + +exports[`https://www.npmjs.com/packaasdge/node 1`] = `npmjs.com/packaasdge/node`; + +exports[`https://wwww.google.com/ 1`] = `wwww.google.com`; diff --git a/index.test.js b/index.test.js index 6902104..fd8dac0 100644 --- a/index.test.js +++ b/index.test.js @@ -1,558 +1,166 @@ -import {test, expect, assert} from 'vitest'; +import {test, expect} from 'vitest'; import {Window} from 'happy-dom'; import shortenUrl, {applyToLink} from './index.js'; const currentLocation = 'https://github.com/fregante/shorten-repo-url/issue/1'; globalThis.document = new Window({url: currentLocation}).document; -function urlMatcherMacro(t, shouldMatch = []) { - for (const [originalUrl, expectedShortenedUrl] of shouldMatch) { - expect(shortenUrl(originalUrl, currentLocation)).toBe(expectedShortenedUrl); - } -} +expect.addSnapshotSerializer({ + serialize(value) { + return value; + }, + test(value) { + return value; + }, +}); -test('GitHub.com URLs', urlMatcherMacro, new Map([ - [ - 'https://github.com/fregante/shorten-repo-url/', - 'fregante/shorten-repo-url', - ], - [ - 'https://github.com/fregante/shorten-repo-url/?tab=readme-ov-file', - 'fregante/shorten-repo-url', - ], - [ - 'https://github.com/fregante/shorten-repo-url/tree/v0.12', - 'v0.12', - ], - [ - 'https://github.com/fregante/shorten-repo-url/tree/d71718db6aa4feb8dc10edbad1134472468e971a', - 'd71718d', - ], - [ - 'https://github.com/nodejs/node/', - 'nodejs/node', - ], - [ - 'https://github.com/nodejs/shorten-repo-url/', - 'nodejs/shorten-repo-url', - ], - [ - 'https://github.com/nodejs/node/tree/v0.12', - 'nodejs/node@v0.12', - ], - [ - 'https://github.com/nodejs/node/tree/d71718db6aa4feb8dc10edbad1134472468e971a', - 'nodejs/node@d71718d', - ], - [ - 'https://github.com/fregante/shorten-repo-url/tree/master/doc', - 'master/doc', - ], - [ - 'https://github.com/fregante/shorten-repo-url/tree/v0.12/doc', - 'v0.12/doc', - ], - [ - 'https://github.com/fregante/shorten-repo-url/tree/d71718db6aa4feb8dc10edbad1134472468e971a/doc', - 'd71718d/doc', - ], - [ - 'https://github.com/nodejs/node/tree/master/doc', - 'nodejs/node@master/doc', - ], - [ - 'https://github.com/nodejs/node/tree/v0.12/doc', - 'nodejs/node@v0.12/doc', - ], - [ - 'https://github.com/nodejs/node/tree/d71718db6aa4feb8dc10edbad1134472468e971a/doc', - 'nodejs/node@d71718d/doc', - ], - [ - 'https://github.com/fregante/shorten-repo-url/blob/master/.gitignore', - 'master/.gitignore', - ], - [ - 'https://github.com/fregante/shorten-repo-url/blob/v0.12/.gitignore', - 'v0.12/.gitignore', - ], - [ - 'https://github.com/fregante/shorten-repo-url/blob/cc8fc46/.gitignore', - 'cc8fc46/.gitignore', - ], - [ - 'https://github.com/nodejs/node/blob/master/.gitignore', - 'nodejs/node@master/.gitignore', - ], - [ - 'https://github.com/nodejs/node/blob/v0.12/.gitignore', - 'nodejs/node@v0.12/.gitignore', - ], - [ - 'https://github.com/nodejs/node/blob/cc8fc46/.gitignore', - 'nodejs/node@cc8fc46/.gitignore', - ], - [ - 'https://github.com/fregante/shorten-repo-url/blame/master/.gitignore', - 'master/.gitignore (blame)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/blame/v0.12/.gitignore', - 'v0.12/.gitignore (blame)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/blame/cc8fc46/.gitignore', - 'cc8fc46/.gitignore (blame)', - ], - [ - 'https://github.com/nodejs/node/blame/master/.gitignore', - 'nodejs/node@master/.gitignore (blame)', - ], - [ - 'https://github.com/nodejs/node/blame/v0.12/.gitignore', - 'nodejs/node@v0.12/.gitignore (blame)', - ], - [ - 'https://github.com/nodejs/node/blame/cc8fc46/.gitignore', - 'nodejs/node@cc8fc46/.gitignore (blame)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/commits/master/.gitignore', - 'master/.gitignore (commits)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/commits/v0.12/.gitignore', - 'v0.12/.gitignore (commits)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/commits/cc8fc46/.gitignore', - 'cc8fc46/.gitignore (commits)', - ], - [ - 'https://github.com/nodejs/node/commits/master/.gitignore', - 'nodejs/node@master/.gitignore (commits)', - ], - [ - 'https://github.com/nodejs/node/commits/v0.12/.gitignore', - 'nodejs/node@v0.12/.gitignore (commits)', - ], - [ - 'https://github.com/nodejs/node/commits/cc8fc46/.gitignore', - 'nodejs/node@cc8fc46/.gitignore (commits)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/commit/cc8fc46.diff', - 'cc8fc46.diff', - ], - [ - 'https://github.com/fregante/shorten-repo-url/commit/cc8fc46.patch', - 'cc8fc46.patch', - ], - [ - 'https://github.com/nodejs/node/commit/cc8fc46.diff', - 'nodejs/node@cc8fc46.diff', - ], - [ - 'https://github.com/nodejs/node/commit/cc8fc46.patch', - 'nodejs/node@cc8fc46.patch', - ], - [ - 'https://github.com/fregante/shorten-repo-url/releases/tag/v0.12.0', - 'v0.12.0 (release)', - ], - [ - 'https://github.com/nodejs/node/releases/tag/v0.12.0', - 'nodejs/node@v0.12.0 (release)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/milestone/25', - 'fregante/shorten-repo-url/milestone/25', - ], - [ - 'https://github.com/fregante/shorten-repo-url/compare/d71718db6aa4feb8dc10edbad1134472468e971a', - 'd71718d (compare)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/compare/master', - 'master (compare)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/compare/master...master', - 'master...master (compare)', - ], - [ - 'https://github.com/nodejs/node/compare/d71718db6aa4feb8dc10edbad1134472468e971a', - 'nodejs/node@d71718d (compare)', - ], - [ - 'https://github.com/nodejs/node/compare/master', - 'nodejs/node@master (compare)', - ], - [ - 'https://github.com/nodejs/node/compare/master...master', - 'nodejs/node@master...master (compare)', - ], - [ - 'https://github.com/nodejs/node/milestone/25', - 'nodejs/node/milestone/25', - ], - [ - 'https://github.com/fregante/shorten-repo-url/labels/npm', - 'npm (label)', - ], - [ - 'https://github.com/nodejs/node/labels/npm', - 'nodejs/node/npm (label)', - ], - [ - 'https://github.com/nodejs/node/labels/Please%21%20♥', - 'nodejs/node/Please! ♥ (label)', - ], - [ - 'https://github.com/refined-github/refined-github/labels/Please%21%20♥%EF%B8%8E', - 'refined-github/refined-github/Please! ♥︎ (label)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/archive/6.4.1.zip', - '6.4.1.zip', - ], - [ - 'https://github.com/fregante/shorten-repo-url/releases/download/6.4.1/now-macos', - '6.4.1 now-macos (download)', - ], - [ - 'https://github.com/zeit/now-cli/archive/6.4.1.zip', - 'zeit/now-cli@6.4.1.zip', - ], - [ - 'https://github.com/zeit/now-cli/releases/download/6.4.1/now-macos', - 'zeit/now-cli@6.4.1 now-macos (download)', - ], - [ - 'https://github.com/bfred-it/shorten-repo-url/network/dependents', - 'bfred-it/shorten-repo-url (dependents)', - ], - [ - 'https://github.com/bfred-it/shorten-repo-url/network/dependencies', - 'bfred-it/shorten-repo-url (dependencies)', - ], - [ - 'https://github.com/network/dependencies', - 'github.com/network/dependencies', - ], // Tricky - [ - 'https://github.com/bfred-it/shorten-repo-url/wiki', - 'bfred-it/shorten-repo-url/wiki', - ], - [ - 'https://github.com/fregante/shorten-repo-url/pulse', - 'fregante/shorten-repo-url/pulse', - ], - [ - 'https://github.com/fregante/shorten-repo-url/labels', - 'fregante/shorten-repo-url/labels', - ], - [ - 'https://github.com/fregante/shorten-repo-url/compare', - 'fregante/shorten-repo-url/compare', - ], - [ - 'https://github.com/fregante/shorten-repo-url/network', - 'fregante/shorten-repo-url/network', - ], - [ - 'https://github.com/fregante/shorten-repo-url/projects', - 'fregante/shorten-repo-url/projects', - ], - [ - 'https://github.com/fregante/shorten-repo-url/releases', - 'fregante/shorten-repo-url/releases', - ], - [ - 'https://github.com/fregante/shorten-repo-url/milestones', - 'fregante/shorten-repo-url/milestones', - ], - [ - 'https://github.com/fregante/shorten-repo-url/contributors', - 'fregante/shorten-repo-url/contributors', - ], - [ - 'https://github.com/fregante/shorten-repo-url/pull/123/files', - '#123 (files)', - ], - [ - 'https://github.com/nodejs/node/pull/123/files', - 'nodejs/node#123 (files)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/pull/123/commits', - '#123 (commits)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/pull/123/checks', - '#123 (checks)', - ], - [ - 'https://github.com/nodejs/node/wiki', - 'nodejs/node/wiki', - ], - [ - 'https://github.com/nodejs/node/pulse', - 'nodejs/node/pulse', - ], - [ - 'https://github.com/nodejs/node/labels', - 'nodejs/node/labels', - ], - [ - 'https://github.com/nodejs/node/compare', - 'nodejs/node/compare', - ], - [ - 'https://github.com/nodejs/node/network', - 'nodejs/node/network', - ], - [ - 'https://github.com/nodejs/node/projects', - 'nodejs/node/projects', - ], - [ - 'https://github.com/nodejs/node/releases', - 'nodejs/node/releases', - ], - [ - 'https://github.com/nodejs/node/milestones', - 'nodejs/node/milestones', - ], - [ - 'https://github.com/nodejs/node/contributors', - 'nodejs/node/contributors', - ], - [ - 'https://github.com/nodejs/node/graphs/commit-activity', - 'nodejs/node/graphs/commit-activity', - ], - [ - 'https://rawgit.com/fregante/shorten-repo-url/master/.gitignore', - 'master/.gitignore (raw)', - ], - [ - 'https://cdn.rawgit.com/fregante/shorten-repo-url/v0.12/.gitignore', - 'v0.12/.gitignore (raw)', - ], - [ - 'https://cdn.rawgit.com/fregante/shorten-repo-url/d71718db/.gitignore', - 'd71718db/.gitignore (raw)', - ], - [ - 'https://raw.githubusercontent.com/fregante/shorten-repo-url/master/.gitignore', - 'master/.gitignore (raw)', - ], - [ - 'https://raw.githubusercontent.com/fregante/shorten-repo-url/v0.12/.gitignore', - 'v0.12/.gitignore (raw)', - ], - [ - 'https://raw.githubusercontent.com/fregante/shorten-repo-url/d71718db/.gitignore', - 'd71718db/.gitignore (raw)', - ], - [ - 'https://rawgit.com/nodejs/node/master/.gitignore', - 'nodejs/node@master/.gitignore (raw)', - ], - [ - 'https://cdn.rawgit.com/nodejs/node/v0.12/.gitignore', - 'nodejs/node@v0.12/.gitignore (raw)', - ], - [ - 'https://cdn.rawgit.com/nodejs/node/d71718db/.gitignore', - 'nodejs/node@d71718db/.gitignore (raw)', - ], - [ - 'https://raw.githubusercontent.com/nodejs/node/master/.gitignore', - 'nodejs/node@master/.gitignore (raw)', - ], - [ - 'https://raw.githubusercontent.com/nodejs/node/v0.12/.gitignore', - 'nodejs/node@v0.12/.gitignore (raw)', - ], - [ - 'https://raw.githubusercontent.com/nodejs/node/d71718db/.gitignore', - 'nodejs/node@d71718db/.gitignore (raw)', - ], - [ - 'https://github.com/sindresorhus', - '@sindresorhus', - ], - [ - 'https://github.com/nodejs', - '@nodejs', - ], - [ - 'https://github.com/pulls', - 'github.com/pulls', - ], - [ - 'https://github.com/issues', - 'github.com/issues', - ], - [ - 'https://github.com/trending', - 'github.com/trending', - ], - [ - 'https://github.com/features', - 'github.com/features', - ], - [ - 'https://github.com/marketplace', - 'github.com/marketplace', - ], - [ - 'https://github.com/trending/developers', - 'github.com/trending/developers', - ], - [ - 'https://github.com/settings/profile', - 'github.com/settings/profile', - ], - [ - 'https://github.com/', - 'github.com', - ], - [ - 'https://github.com/fregante/shorten-repo-url/issues', - 'fregante/shorten-repo-url/issues', - ], - [ - 'https://github.com/fregante/shorten-repo-url/issues?q=wow', - 'fregante/shorten-repo-url/issues (wow)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/issues?q=is%3Aissue++is%3Aopen+sort%3Aupdated-desc+&unrelated=true', - 'fregante/shorten-repo-url/issues?unrelated=true (is:open sort:updated-desc)', - ], - [ - 'https://github.com/issues?q=is%3Aissue++is%3Aopen+sort%3Aupdated-desc+&unrelated=true', - 'github.com/issues?unrelated=true (is:open sort:updated-desc)', - ], - [ - 'https://github.com/pulls?q=is%3Apr++is%3Aopen+sort%3Aupdated-desc+&unrelated=true', - 'github.com/pulls?unrelated=true (is:open sort:updated-desc)', - ], - [ - 'https://github.com/sindresorhus/notifier-for-github/pull/253/files/6b4489d417c9425dc27c5fb8d6b4a8518debd035..60cdcf3c3646164441bf8f037cef620479cdec59', - '6b4489d4..60cdcf3c (#253)', - ], - [ - 'https://togithub.com/fregante/shorten-repo-url/issues/25', - '#25', - ], - [ - 'https://togithub.com/fregante/shorten-repo-url/issues/28#issue-850900171', - '#28 (comment)', - ], - [ - 'https://togithub.com/fregante/shorten-repo-url/pull/32', - '#32', - ], - [ - 'https://togithub.com/fregante/shorten-repo-url/pull/32/files', - '#32 (files)', - ], - [ - 'https://togithub.com/fregante/shorten-repo-url/pull/33#pullrequestreview-801229042', - '#33 (review)', - ], - [ - 'https://togithub.com/fregante/shorten-repo-url/pull/33#discussion_r750069394', - '#33 (review)', - ], - [ - 'https://togithub.com/nodejs/node/pull/123', - 'nodejs/node#123', - ], - [ - 'https://togithub.com/nodejs/node/pull/123/files', - 'nodejs/node#123 (files)', - ], - [ - 'https://togithub.com/fregante/shorten-repo-url/commit/98c6175b0cbd4caca71d24e68e57b942b0dfb549', - '98c6175', - ], - [ - 'https://togithub.com/refined-github/refined-github/commit/4f270c4f50e0a2a20085a6e92095117f10340322', - 'refined-github/refined-github@4f270c4', - ], - [ - 'https://togithub.com/refined-github/refined-github/commit/e81a9646b448d90c7e02ab41332cab0507dccbbd#commitcomment-60089354', - 'refined-github/refined-github@e81a964 (comment)', - ], - [ - 'https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22#3-it-doesnt-require-options', - 'Wiki: "Can you add this feature?" (3 it doesnt require options) (refined-github/refined-github)', - ], - [ - 'https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22#', - 'Wiki: "Can you add this feature?" (refined-github/refined-github)', - ], - [ - 'https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22', - 'Wiki: "Can you add this feature?" (refined-github/refined-github)', - ], - [ - 'https://github.com/fregante/shorten-repo-url/wiki/%22Can-you-add-this-feature%3F%22', - 'Wiki: "Can you add this feature?"', - ], -])); +const urls = [ + 'https://github.com/fregante/shorten-repo-url/', + 'https://github.com/fregante/shorten-repo-url/?tab=readme-ov-file', + 'https://github.com/fregante/shorten-repo-url/tree/v0.12', + 'https://github.com/fregante/shorten-repo-url/tree/d71718db6aa4feb8dc10edbad1134472468e971a', + 'https://github.com/nodejs/node/', + 'https://github.com/nodejs/shorten-repo-url/', + 'https://github.com/nodejs/node/tree/v0.12', + 'https://github.com/nodejs/node/tree/d71718db6aa4feb8dc10edbad1134472468e971a', + 'https://github.com/fregante/shorten-repo-url/tree/master/doc', + 'https://github.com/fregante/shorten-repo-url/tree/v0.12/doc', + 'https://github.com/fregante/shorten-repo-url/tree/d71718db6aa4feb8dc10edbad1134472468e971a/doc', + 'https://github.com/nodejs/node/tree/master/doc', + 'https://github.com/nodejs/node/tree/v0.12/doc', + 'https://github.com/nodejs/node/tree/d71718db6aa4feb8dc10edbad1134472468e971a/doc', + 'https://github.com/fregante/shorten-repo-url/blob/master/.gitignore', + 'https://github.com/fregante/shorten-repo-url/blob/v0.12/.gitignore', + 'https://github.com/fregante/shorten-repo-url/blob/cc8fc46/.gitignore', + 'https://github.com/nodejs/node/blob/master/.gitignore', + 'https://github.com/nodejs/node/blob/v0.12/.gitignore', + 'https://github.com/nodejs/node/blob/cc8fc46/.gitignore', + 'https://github.com/fregante/shorten-repo-url/blame/master/.gitignore', + 'https://github.com/fregante/shorten-repo-url/blame/v0.12/.gitignore', + 'https://github.com/fregante/shorten-repo-url/blame/cc8fc46/.gitignore', + 'https://github.com/nodejs/node/blame/master/.gitignore', + 'https://github.com/nodejs/node/blame/v0.12/.gitignore', + 'https://github.com/nodejs/node/blame/cc8fc46/.gitignore', + 'https://github.com/fregante/shorten-repo-url/commits/master/.gitignore', + 'https://github.com/fregante/shorten-repo-url/commits/v0.12/.gitignore', + 'https://github.com/fregante/shorten-repo-url/commits/cc8fc46/.gitignore', + 'https://github.com/nodejs/node/commits/master/.gitignore', + 'https://github.com/nodejs/node/commits/v0.12/.gitignore', + 'https://github.com/nodejs/node/commits/cc8fc46/.gitignore', + 'https://github.com/fregante/shorten-repo-url/commit/cc8fc46.diff', + 'https://github.com/fregante/shorten-repo-url/commit/cc8fc46.patch', + 'https://github.com/nodejs/node/commit/cc8fc46.diff', + 'https://github.com/nodejs/node/commit/cc8fc46.patch', + 'https://github.com/fregante/shorten-repo-url/releases/tag/v0.12.0', + 'https://github.com/nodejs/node/releases/tag/v0.12.0', + 'https://github.com/fregante/shorten-repo-url/milestone/25', + 'https://github.com/fregante/shorten-repo-url/compare/d71718db6aa4feb8dc10edbad1134472468e971a', + 'https://github.com/fregante/shorten-repo-url/compare/master', + 'https://github.com/fregante/shorten-repo-url/compare/master...master', + 'https://github.com/nodejs/node/compare/d71718db6aa4feb8dc10edbad1134472468e971a', + 'https://github.com/nodejs/node/compare/master', + 'https://github.com/nodejs/node/compare/master...master', + 'https://github.com/nodejs/node/milestone/25', + 'https://github.com/fregante/shorten-repo-url/labels/npm', + 'https://github.com/nodejs/node/labels/npm', + 'https://github.com/nodejs/node/labels/Please%21%20♥', + 'https://github.com/refined-github/refined-github/labels/Please%21%20♥%EF%B8%8E', + 'https://github.com/fregante/shorten-repo-url/archive/6.4.1.zip', + 'https://github.com/fregante/shorten-repo-url/releases/download/6.4.1/now-macos', + 'https://github.com/zeit/now-cli/archive/6.4.1.zip', + 'https://github.com/zeit/now-cli/releases/download/6.4.1/now-macos', + 'https://github.com/bfred-it/shorten-repo-url/network/dependents', + 'https://github.com/bfred-it/shorten-repo-url/network/dependencies', + 'https://github.com/network/dependencies', + 'https://github.com/bfred-it/shorten-repo-url/wiki', + 'https://github.com/fregante/shorten-repo-url/pulse', + 'https://github.com/fregante/shorten-repo-url/labels', + 'https://github.com/fregante/shorten-repo-url/compare', + 'https://github.com/fregante/shorten-repo-url/network', + 'https://github.com/fregante/shorten-repo-url/projects', + 'https://github.com/fregante/shorten-repo-url/releases', + 'https://github.com/fregante/shorten-repo-url/milestones', + 'https://github.com/fregante/shorten-repo-url/contributors', + 'https://github.com/fregante/shorten-repo-url/pull/123/files', + 'https://github.com/nodejs/node/pull/123/files', + 'https://github.com/fregante/shorten-repo-url/pull/123/commits', + 'https://github.com/fregante/shorten-repo-url/pull/123/checks', + 'https://github.com/nodejs/node/wiki', + 'https://github.com/nodejs/node/pulse', + 'https://github.com/nodejs/node/labels', + 'https://github.com/nodejs/node/compare', + 'https://github.com/nodejs/node/network', + 'https://github.com/nodejs/node/projects', + 'https://github.com/nodejs/node/releases', + 'https://github.com/nodejs/node/milestones', + 'https://github.com/nodejs/node/contributors', + 'https://github.com/nodejs/node/graphs/commit-activity', + 'https://rawgit.com/fregante/shorten-repo-url/master/.gitignore', + 'https://cdn.rawgit.com/fregante/shorten-repo-url/v0.12/.gitignore', + 'https://cdn.rawgit.com/fregante/shorten-repo-url/d71718db/.gitignore', + 'https://raw.githubusercontent.com/fregante/shorten-repo-url/master/.gitignore', + 'https://raw.githubusercontent.com/fregante/shorten-repo-url/v0.12/.gitignore', + 'https://raw.githubusercontent.com/fregante/shorten-repo-url/d71718db/.gitignore', + 'https://rawgit.com/nodejs/node/master/.gitignore', + 'https://cdn.rawgit.com/nodejs/node/v0.12/.gitignore', + 'https://cdn.rawgit.com/nodejs/node/d71718db/.gitignore', + 'https://raw.githubusercontent.com/nodejs/node/master/.gitignore', + 'https://raw.githubusercontent.com/nodejs/node/v0.12/.gitignore', + 'https://raw.githubusercontent.com/nodejs/node/d71718db/.gitignore', + 'https://github.com/sindresorhus', + 'https://github.com/nodejs', + 'https://github.com/pulls', + 'https://github.com/issues', + 'https://github.com/trending', + 'https://github.com/features', + 'https://github.com/marketplace', + 'https://github.com/trending/developers', + 'https://github.com/settings/profile', + 'https://github.com/', + 'https://github.com/fregante/shorten-repo-url/issues', + 'https://github.com/fregante/shorten-repo-url/issues?q=wow', + 'https://github.com/fregante/shorten-repo-url/issues?q=is%3Aissue++is%3Aopen+sort%3Aupdated-desc+&unrelated=true', + 'https://github.com/issues?q=is%3Aissue++is%3Aopen+sort%3Aupdated-desc+&unrelated=true', + 'https://github.com/pulls?q=is%3Apr++is%3Aopen+sort%3Aupdated-desc+&unrelated=true', + 'https://github.com/sindresorhus/notifier-for-github/pull/253/files/6b4489d417c9425dc27c5fb8d6b4a8518debd035..60cdcf3c3646164441bf8f037cef620479cdec59', + 'https://togithub.com/fregante/shorten-repo-url/issues/25', + 'https://togithub.com/fregante/shorten-repo-url/issues/28#issue-850900171', + 'https://togithub.com/fregante/shorten-repo-url/pull/32', + 'https://togithub.com/fregante/shorten-repo-url/pull/32/files', + 'https://togithub.com/fregante/shorten-repo-url/pull/33#pullrequestreview-801229042', + 'https://togithub.com/fregante/shorten-repo-url/pull/33#discussion_r750069394', + 'https://togithub.com/nodejs/node/pull/123', + 'https://togithub.com/nodejs/node/pull/123/files', + 'https://togithub.com/fregante/shorten-repo-url/commit/98c6175b0cbd4caca71d24e68e57b942b0dfb549', + 'https://togithub.com/refined-github/refined-github/commit/4f270c4f50e0a2a20085a6e92095117f10340322', + 'https://togithub.com/refined-github/refined-github/commit/e81a9646b448d90c7e02ab41332cab0507dccbbd#commitcomment-60089354', + 'https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22#3-it-doesnt-require-options', + 'https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22#', + 'https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22', + 'https://github.com/fregante/shorten-repo-url/wiki/%22Can-you-add-this-feature%3F%22', + 'https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement#parameters', + 'https://www.google.com/', + 'https://wwww.google.com/', + 'http://www.google.com/', + 'https://www.npmjs.com/', + 'https://www.npmjs.com/packaasdge/node', + 'https://example.com/nodejs/node/blob/cc8fc46/.gitignore', + 'https://example.site/한글로-된-URL', +]; -test('External URLs', urlMatcherMacro, new Map([ - [ - 'https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement#parameters', - 'developer.mozilla.org/en-US/docs/Web/API/Document/createElement#parameters', - ], - [ - 'https://www.google.com/', - 'google.com', - ], - [ - 'https://wwww.google.com/', - 'wwww.google.com', - ], - [ - 'http://www.google.com/', - 'http://www.google.com', - ], - [ - 'https://www.npmjs.com/', - 'npmjs.com', - ], - [ - 'https://www.npmjs.com/package/node', - 'npmjs.com/package/node', - ], - [ - 'https://example.com/nodejs/node/blob/cc8fc46/.gitignore', - 'example.com/nodejs/node/blob/cc8fc46/.gitignore', - ], - [ - 'https://example.site/한글로-된-URL', - 'example.site/한글로-된-URL', - ], -])); +test.each(urls)('%s', url => { + const shortened = shortenUrl(url, currentLocation); + expect(shortened).toMatchSnapshot(); -test('applyToLink', () => { const a = document.createElement('a'); - a.href = 'https://github.com'; - a.textContent = 'https://github.com'; - assert(applyToLink(a, currentLocation)); - assert.equal(a.textContent, 'github.com'); -}); + a.href = url; -test('applyToLink with a link that generates a HTML child element', () => { - const a = document.createElement('a'); - a.href = 'https://github.com/fregante/shorten-repo-url/blob/master/.gitignore'; - a.textContent = 'https://github.com/fregante/shorten-repo-url/blob/master/.gitignore'; - assert(applyToLink(a, currentLocation)); - assert.equal(a.innerHTML, 'master/.gitignore'); + // The URL might contain CJK characters, but after it's passed to a.href it's encoded + const correctlyEncodedUrl = a.href; + + a.textContent = url; + applyToLink(a, currentLocation); + expect(a.href, 'applyToLink should not alter the href').toBe(correctlyEncodedUrl); + expect(a.innerHTML).toBe(shortened); }); +