Improve testing suite

Signed-off-by: Sebastian Martinez <sebastinez@me.com>
This commit is contained in:
Sebastian Martinez
2022-03-24 15:05:15 +01:00
parent 6f2f8c454f
commit 286cb1cd21
44 changed files with 2550 additions and 482 deletions
+46
View File
@@ -0,0 +1,46 @@
import BlockTimer from "./BlockTimer.svelte";
import { render } from "@testing-library/svelte";
import "@public/index.css";
import type { EventType, Listener } from "@ethersproject/abstract-provider";
describe('BlockTimer', function () {
it("increases correctly the loading bar", () => {
let block = 1;
const props = {
config: {
provider: {
on: (event: EventType, listener: Listener) => {
if (event === "block") {
listener(block);
}
}
}
},
startBlock: 1,
duration: 3
};
const { rerender } = render(BlockTimer, props);
cy.get("div.loader").should("have.attr", "style", "width: 0%;").then(() => {
block += 1;
rerender(props);
});
cy.get("div.loader").last().should("have.attr", "style", "width: 33%;").then(() => {
block += 1;
rerender(props);
});
cy.get("div.loader").last().should("have.attr", "style", "width: 66%;").then(() => {
block += 1;
rerender(props);
});
cy.get("div.loader").last().should("have.attr", "style", "width: 99%;").then(() => {
block += 1;
rerender(props);
});
});
});
+1 -1
View File
@@ -29,5 +29,5 @@
</style>
<div class="parent">
<div class="loader" style="width: {(currentBlock - startBlock) * 10}%" />
<div class="loader" style="width: {(currentBlock - startBlock) * Math.floor(100 / duration)}%" />
</div>
+17 -19
View File
@@ -1,11 +1,11 @@
import Error from "./Error.svelte";
import { mount } from "radicle-svelte-unit-test";
import { render } from "@testing-library/svelte";
import { Failure } from '@app/error';
import { styles } from "@test/support/index";
import "@public/index.css";
describe('Error', function () {
it("Open Error modal with default props", () => {
mount(Error, { props: {
it("should show passed in props", () => {
render(Error, { props: {
subtitle: "Subtitle of Modal",
error: {
type: Failure.InsufficientBalance,
@@ -13,30 +13,28 @@ describe('Error', function () {
message: "Not enough RAD"
}
}
}, styles);
cy.get("[slot=title]").should("have.text", " Error");
cy.get("[slot=subtitle]").should("have.text", "Subtitle of Modal");
cy.get("[slot=body]").should("have.text", "Error: Not enough RAD");
cy.get("button").should("have.text", "Back");
});
cy.findByText("Error");
cy.findByText("Subtitle of Modal");
cy.findByText("Not enough RAD");
cy.findByText("Back");
});
it("Open Error modal with custom message", () => {
mount(Error, { props: {
it("should show custom error message", () => {
render(Error, { props: {
subtitle: "Subtitle of Modal",
message: "Error message to check for",
} }, styles);
cy.get("[slot=subtitle]").should("have.text", "Subtitle of Modal");
cy.get("[slot=body]").should("have.text", "Error: Error message to check for");
cy.get("button").should("have.text", "Back");
} });
cy.findByText("Error message to check for");
});
it("Check floating modal changes button label to Close", () => {
mount(Error, { props: {
it("should change button label to Close when floating", () => {
render(Error, { props: {
title: "Title of Modal",
subtitle: "Subtitle of Modal",
message: "Error message to check for",
floating: true
} }, styles);
cy.get("button").should("have.text", "Close");
} });
cy.findByText("Close");
});
});
+17
View File
@@ -0,0 +1,17 @@
import NotFound from "./NotFound.svelte";
import { render } from "@testing-library/svelte";
import "@public/index.css";
describe('NotFound', function () {
it("shows passed props correctly", () => {
render(NotFound, {
props: {
title: "nakamoto",
subtitle: "Sorry, the requested project was not found."
}
});
cy.findByText("nakamoto");
cy.findByText("Sorry, the requested project was not found.");
cy.findByText("Back");
});
});
+23 -11
View File
@@ -1,18 +1,30 @@
import Search from "./Search.svelte";
import { mount } from "radicle-svelte-unit-test";
import { styles } from "@test/support/index";
import { fireEvent, render } from "@testing-library/svelte";
import "@public/index.css";
describe('Search', function () {
it("Renders correctly", () => {
mount(Search, {}, styles);
cy.get("input").should("exist");
describe('Logic', () => {
it("show a appropiate placeholder", () => {
render(Search);
cy.get("input").should("have.attr", "placeholder", "Search a name or address...");
});
it("Search input displays and navigates correctly", () => {
mount(Search, {}, styles);
cy.get("input").type("cloudhead{enter}");
cy.get("input").should("have.value", "cloudhead");
cy.url().should("contain", "/resolver/query?q=cloudhead");
it("allow input a query and navigates accordingly", () => {
render(Search);
cy.get("input").type("cloudhead.radicle.eth{enter}");
cy.get("input").should("have.value", "cloudhead.radicle.eth");
cy.url().should("contain", "/resolver/query?q=cloudhead.radicle.eth");
});
});
describe("Events", () => {
it("should fire an event when the input changes", () => {
const { component } = render(Search);
const mock = cy.spy();
component.$on("search", mock);
cy.get("input").then(([input]) => {
fireEvent.keyDown(input, { key: "Enter" });
expect(mock).to.have.been.calledOnce;
});
});
});
+25 -6
View File
@@ -1,18 +1,37 @@
import SeedAddress from "./SeedAddress.svelte";
import { mount } from "radicle-svelte-unit-test";
import { styles } from "@test/support/index";
import { render } from "@testing-library/svelte";
import "@public/index.css";
describe('SeedAddress', function () {
it("Renders correctly", () => {
mount(SeedAddress, {
it("shows the seed emoji and seed host", () => {
render(SeedAddress, {
props: {
seed: {
id: "hydkkkf5ksbe5fuszdhpqhytu3q36gwagj874wxwpo5a8ti8coygh1",
emoji: "🐱",
host: "seed.cloudhead.io",
},
port: 8776
}
}, styles);
cy.findByText("seed.cloudhead.io").should("exist").should("have.attr", "href", "/seeds/seed.cloudhead.io");
});
cy.get("span.seed-icon").should("have.text", "🐱");
cy.findByText("seed.cloudhead.io").should("have.attr", "href", "/seeds/seed.cloudhead.io").should("be.visible");
});
it("shows the full seed id", () => {
render(SeedAddress, {
props: {
seed: {
id: "hydkkkf5ksbe5fuszdhpqhytu3q36gwagj874wxwpo5a8ti8coygh1",
emoji: "🐱",
host: "seed.cloudhead.io",
},
port: 8776,
full: true
}
});
cy.get("span.seed-icon").should("have.text", "🐱");
cy.findByText("hydkkk…coygh1@seed.cloudhead.io").should("be.visible");
cy.findByText(":8776").should("be.visible");
});
});
+110 -46
View File
@@ -1,6 +1,6 @@
import BranchSelector from "./BranchSelector.svelte";
import { mount } from "radicle-svelte-unit-test";
import { styles } from "@test/support/index";
import { fireEvent, render } from "@testing-library/svelte";
import "@public/index.css";
const defaultProps = {
project: {
@@ -18,41 +18,63 @@ const defaultProps = {
},
branches: { "master": "e678629cd37c770c640a2cd997fc76303c815772" },
revision: "e678629cd37c770c640a2cd997fc76303c815772",
toggleDropdown: () => "branch"
};
describe('BranchSelector', function () {
it("Render with commit = head and branch listing", () => {
mount(BranchSelector, {
props: defaultProps
}, styles);
cy.findByText("master").should("exist");
cy.get("div.commit > div.hash.desktop").should("have.text", "e678629");
describe('Logic', () => {
it("should show defaultBranch label and head commit if revision == head", () => {
const { rerender } = render(BranchSelector, {
props: defaultProps,
});
cy.get("div.stat.branch").should("be.visible").should("have.text", "master");
cy.get("div.hash.mobile").should("be.visible").should("have.text", "e678629");
// If project.head is null we should get the head from branches.
rerender({
props: {
...defaultProps,
project: {
head: null,
urn: "rad:git:hnrkqdpm9ub19oc8dccx44echy76hzfsezyio",
name: "nakamoto",
description: "Privacy-preserving Bitcoin light-client implementation in Rust",
defaultBranch: "master",
maintainers: [
"rad:git:hnrkqdpm9ub19oc8dccx44echy76hzfsezyio"
],
delegates: [
"hyn9diwfnytahjq8u3iw63h9jte1ydcatxax3saymwdxqu1zo645pe"
]
}
}
});
cy.get("div.stat.branch").should("be.visible").should("have.text", "master");
cy.get("div.hash.mobile").should("be.visible").should("have.text", "e678629");
});
it("Test Branch selection", () => {
// The viewport here is to simulate desktop behaviour
cy.viewport(800, 300);
mount(BranchSelector, {
it("should show the branch dropdown if branches available", () => {
render(BranchSelector, {
props: {
...defaultProps, branches: {
...defaultProps,
branches: {
"master": "e678629cd37c770c640a2cd997fc76303c815772",
"feature-branch": "29e8b7b0f3019b8e8a6d9bfb0964ee78f4ff12f5",
"xyz": "debf82ef3623ec11751a993bda85bac2ff1c6f00",
},
branchesDropdown: true,
}, callbacks: {
branchChanged: cy.stub().as("branchChanged")
branchesDropdown: true
}
}, styles);
cy.get("div.dropdown > div").first().click();
cy.get("@branchChanged")
.should("be.calledOnce")
.its("firstCall.args.0.detail")
.should("equal", "feature-branch");
});
cy.get("div.dropdown div.dropdown-item")
.first()
.should("contain.text", "feature-branch")
.next()
.should("contain.text", "master").should("have.class", "selected")
.next()
.should("contain.text", "xyz");
});
it("Render with commit != head, passing a branch as rev and branch listing", () => {
mount(BranchSelector, {
it("should show feature-branch label and head commit, if branch label is passed as revision", () => {
render(BranchSelector, {
props: {
...defaultProps, branches: {
"master": "e678629cd37c770c640a2cd997fc76303c815772",
@@ -61,42 +83,84 @@ describe('BranchSelector', function () {
},
revision: "feature-branch"
}
}, styles);
cy.findByText("feature-branch").should("exist");
cy.get("div.commit > div.hash.desktop").should("have.text", "29e8b7b");
});
cy.get("div.stat.branch").should("be.visible").should("have.text", "feature-branch");
cy.get("div.hash.mobile").should("be.visible").should("have.text", "29e8b7b");
});
it("Render with commit != head passing a commit as rev and branch listing", () => {
mount(BranchSelector, {
it("should show only commit if no branchLabel nor branches are available", () => {
render(BranchSelector, {
props: {
...defaultProps,
revision: "debf82ef3623ec11751a993bda85bac2ff1c6f00"
revision: "debf82ef3623ec11751a993bda85bac2ff1c6f00",
branches: {}
}
}, styles
);
cy.get("div.commit > div.hash.desktop").should("have.text", "debf82ef3623ec11751a993bda85bac2ff1c6f00");
});
cy.get("div.hash.mobile").should("be.visible").should("have.text", "debf82e");
cy.viewport("macbook-13");
cy.get("div.hash.desktop").should("be.visible").should("have.text", "debf82ef3623ec11751a993bda85bac2ff1c6f00");
});
it("Render with commit = head, without branch listing", () => {
mount(BranchSelector, {
it("should show only commit if branches are available but no branchLabel", () => {
render(BranchSelector, {
props: {
...defaultProps,
revision: "debf82ef3623ec11751a993bda85bac2ff1c6f00",
}
});
cy.get("div.hash.mobile").should("be.visible").should("have.text", "debf82e");
cy.viewport("macbook-13");
cy.get("div.hash.desktop").should("be.visible").should("have.text", "debf82ef3623ec11751a993bda85bac2ff1c6f00");
});
it("should show defaultBranch label if revision == head", () => {
render(BranchSelector, {
props: {
...defaultProps,
revision: "e678629cd37c770c640a2cd997fc76303c815772",
branches: {},
branches: {}
}
}, styles
);
cy.get("div.commit > div.hash.desktop").should("have.text", "e678629cd37c770c640a2cd997fc76303c815772");
});
cy.get("div.stat.branch.not-allowed").should("be.visible").should("have.text", "master");
});
});
it("Render without branch listing, commit != head", () => {
mount(BranchSelector, {
describe("Layout", () => {
it("should show shortened commit when on mobile, and full hash when on desktop", () => {
render(BranchSelector, {
props: {
...defaultProps,
revision: "6b84e519d3c535879eb2b9ee8457bb70ca275a75",
branches: {},
revision: "e678629cd37c770c640a2cd997fc76303c815772"
}
}, styles);
cy.get("div.commit > div.hash.desktop").should("have.text", "6b84e519d3c535879eb2b9ee8457bb70ca275a75");
});
cy.viewport("iphone-x");
cy.get("div.hash.mobile").should("be.visible");
cy.get("div.hash.desktop").should("not.be.visible");
cy.viewport("macbook-15");
cy.get("div.hash.mobile").should("not.be.visible");
cy.get("div.hash.desktop").should("be.visible");
});
});
describe("Events", () => {
it("should dispatch a 'branchChanged' event on click", () => {
const { getByText, component } = render(BranchSelector, {
props: {
...defaultProps,
revision: "feature-branch",
branchesDropdown: true,
branches: {
"feature-branch": "29e8b7b0f3019b8e8a6d9bfb0964ee78f4ff12f5",
"xyz": "debf82ef3623ec11751a993bda85bac2ff1c6f00",
}
}
});
const branchLabel = getByText("xyz");
const mock = cy.spy();
component.$on("branchChanged", mock);
fireEvent.click(branchLabel);
expect(mock).to.have.been.calledOnce;
});
});
+1 -1
View File
@@ -19,7 +19,7 @@
$: branchList = Object.keys(branches).sort().map(b => ({ key: b, value: b, badge: null }));
$: showSelector = branchList.length > 1;
$: head = branches[project.defaultBranch];
$: head = project.head ?? branches[project.defaultBranch];
$: commit = getOid(revision, branches) || head;
$: if (commit == head) {
branchLabel = project.defaultBranch;
+1 -1
View File
@@ -188,7 +188,7 @@
<div class="stat commit-count" class:active={content == ProjectContent.History} on:click={() => toggleContent(ProjectContent.History)}>
<strong>{tree.stats.commits}</strong> commit(s)
</div>
<div class="stat">
<div class="stat contributor-count">
<strong>{tree.stats.contributors}</strong> contributor(s)
</div>
</header>
+1 -1
View File
@@ -57,7 +57,7 @@
<div class="history">
{#each history.headers as group (group.time)}
<div class="commit-group">
<header>
<header class="commit-date">
<p>{group.time}</p>
</header>
<div class="commit-group-headers">
+65 -50
View File
@@ -1,6 +1,6 @@
import PeerSelector from "./PeerSelector.svelte";
import { mount } from "radicle-svelte-unit-test";
import { styles } from "@test/support/index";
import { fireEvent, render } from "@testing-library/svelte";
import "@public/index.css";
const defaultProps = {
peer: "hyyg555wwkkutaysg6yr67qnu5d5ji54iur3n5uzzszndh8dp7ofue",
@@ -14,67 +14,82 @@ const defaultProps = {
toggleDropdown: () => console.log("toggle"),
};
describe('PeerSelector', function () {
it("Render correctly with default props", () => {
mount(PeerSelector, {
describe('Logic', function () {
it("show delegate name and badge", () => {
render(PeerSelector, {
props: defaultProps
}, styles);
cy.get("span.peer-id").should("has.text", "sebastinez");
cy.get("span.badge").should("has.text", "delegate");
});
cy.get("span.peer-id").should("have.text", "sebastinez");
cy.get("span.badge.primary").should("have.text", "delegate");
});
it("Test Peer selection", () => {
mount(PeerSelector, {
props: {
...defaultProps, peers: [
{
"id": "hyyg555wwkkutaysg6yr67qnu5d5ji54iur3n5uzzszndh8dp7ofue",
"name": "sebastinez",
"delegate": false
},
{
"id": "hydkkkf5ksbe5fuszdhpqhytu3q36gwagj874wxwpo5a8ti8coygh1",
"name": "cloudhead",
"delegate": true
},
],
peersDropdown: true
}, callbacks: {
peerChanged: cy.stub().as("peerChanged")
}
}, styles);
cy.get("div.dropdown > div").last().click();
cy.get("@peerChanged")
.should("be.calledOnce")
.its("firstCall.args.0.detail")
.should("equal", "hydkkkf5ksbe5fuszdhpqhytu3q36gwagj874wxwpo5a8ti8coygh1");
});
it("If no peers are provided, no dropdown should be showed", () => {
mount(PeerSelector, {
it("show peer id with badge if no name available", () => {
render(PeerSelector, {
props: {
...defaultProps,
peers: [],
peersDropdown: true
peers: [
{
"id": "hyyg555wwkkutaysg6yr67qnu5d5ji54iur3n5uzzszndh8dp7ofue",
"delegate": true
}
],
}
}, styles);
cy.get("div.dropdown > div.dropdown-item").should("not.exist");
});
cy.get("span.peer-id").should("have.text", "hyyg55…p7ofue");
cy.get("span.badge.primary").should("have.text", "delegate");
});
it("If peer identity is not being resolved, fallback to peer id", () => {
mount(PeerSelector, {
it("show only peer id if no additional data available", () => {
render(PeerSelector, {
props: {
...defaultProps,
peers: [
{
"id": "hyyg555wwkkutaysg6yr67qnu5d5ji54iur3n5uzzszndh8dp7ofue",
}
],
peersDropdown: true
]
}
}, styles);
cy.get("span.peer-id").should("has.text", "hyyg55…p7ofue");
cy.get("span.badge").should("not.exist");
cy.get("div.dropdown > .dropdown-item")
.first()
.should("contain", "hyyg555wwkkutaysg6yr67qnu5d5ji54iur3n5uzzszndh8dp7ofue");
});
cy.get("span.peer-id").should("have.text", "hyyg55…p7ofue");
});
});
describe("Layout", () => {
it("should highlight the current peer", () => {
render(PeerSelector, {
props: { ...defaultProps, peersDropdown: true }
});
cy.get("div.dropdown-item").should("have.class", "selected");
});
});
describe('Events', () => {
it("dispatch peerChanged event if clicking on a peer", () => {
cy.viewport("macbook-13");
const { getByText, component } = render(PeerSelector, {
props: {
...defaultProps,
peersDropdown: true,
peers: [
{
"id": "hyy841u4phudmr8s5rg1jjwd1ct7x7438wmjwtsm464y8uyxyhyi6c",
"name": "cloudhead",
"delegate": true
},
{
"id": "hyyg555wwkkutaysg6yr67qnu5d5ji54iur3n5uzzszndh8dp7ofue",
"name": "sebastinez",
"delegate": true
}
]
}
});
const peer = getByText("cloudhead");
const mock = cy.spy();
component.$on("peerChanged", mock);
fireEvent.click(peer);
expect(mock).to.have.been.calledOnce;
});
});
-48
View File
@@ -1,48 +0,0 @@
import { Seed } from "./Seed";
import { Config } from "@app/config";
describe('Seed', function () {
it("Constructs git host correctly", () => {
const cfg = new Config({ name: "homestead", chainId: 1 }, null as any, null);
const seed = new Seed({
host: "seed.cloudhead.io",
id: "hydzjm73tstmdcwhach3psfgkunbjpih7ztnuswhw9w6h9pi5sffd6",
git: "git.seed.cloudhead.io",
}, cfg);
expect(seed.git.host).to.eq("git.seed.cloudhead.io");
expect(seed.git.port).to.eq(443);
expect(seed.host).to.eq("seed.cloudhead.io");
expect(seed.api.host).to.eq(seed.host);
expect(seed.api.port).to.eq(8777);
});
it("Constructs git host correctly with custom port", () => {
const cfg = new Config({ name: "homestead", chainId: 1 }, null as any, null);
const seed = new Seed({
host: "seed.cloudhead.io",
id: "hydzjm73tstmdcwhach3psfgkunbjpih7ztnuswhw9w6h9pi5sffd6",
git: "https://git.seed.cloudhead.io:8778",
}, cfg);
expect(seed.git.host).to.eq("git.seed.cloudhead.io");
expect(seed.git.port).to.eq(8778);
});
it("Constructs api host correctly", () => {
const cfg = new Config({ name: "homestead", chainId: 1 }, null as any, null);
const seed = new Seed({
host: "seed.cloudhead.io",
id: "hydzjm73tstmdcwhach3psfgkunbjpih7ztnuswhw9w6h9pi5sffd6",
api: "https://api.seed.cloudhead.io:8080",
}, cfg);
expect(seed.api.host).to.eq("api.seed.cloudhead.io");
expect(seed.api.port).to.eq(8080);
expect(seed.host).to.eq("api.seed.cloudhead.io");
expect(seed.git.host).to.eq("seed.cloudhead.io");
expect(seed.git.port).to.eq(443);
});
});
+4 -2
View File
@@ -8,8 +8,11 @@ import WalletConnect from "@walletconnect/client";
import config from "@app/config.json";
import { WalletConnectSigner } from "./WalletConnectSigner";
declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/naming-convention
Cypress: any;
ethereum: any;
registrarState: any;
}
@@ -246,8 +249,7 @@ export async function getConfig(): Promise<Config> {
if (metamask) {
// If Metamask is detected, we use the network configured there.
const ready = await checkMetaMask(metamask);
if (ready === null) throw new Error("Not able to connect to Metamask, check for multiple Web3 providers");
network = ready;
if (ready) network = ready;
}
const networkConfig = (<Record<string, any>> config)[network.name];
+7
View File
@@ -0,0 +1,7 @@
// A simple mock for some HTMLCanvasElement functions to run in a node env.
HTMLCanvasElement.prototype.getContext = () => ({
fillRect: () => null
});
HTMLCanvasElement.prototype.toDataURL = () => null;
+9
View File
@@ -0,0 +1,9 @@
/* eslint-disable no-undef */
import fetch from 'node-fetch';
if (! globalThis.fetch) {
globalThis.fetch = fetch;
globalThis.Headers = fetch.Headers;
globalThis.Request = fetch.Request;
globalThis.Response = fetch.Response;
}
-1
View File
@@ -169,7 +169,6 @@ export function path(opts: PathOptions): string {
}
// We need a SHA1 commit in some places, so we return early if the revision is a SHA and else we look into branches.
// As fallback we use the head commit.
export function getOid(revision: string, branches?: Branches): string | null {
if (isOid(revision)) return revision;
+276
View File
@@ -0,0 +1,276 @@
import { BigNumber } from "ethers";
import { describe, expect, test } from "vitest";
import type { Config } from "./config";
import * as utils from "./utils";
describe("Conversions", () => {
test("toWei", () => {
expect(
utils.toWei("10")
).toEqual(BigNumber.from("10000000000000000000"));
});
});
describe("Format functions", () => {
test.each([
{ cid: "Qm1234567890123456789012345678901234567890", expected: "https://ipfs.io/ipfs/Qm1234567890123456789012345678901234567890" },
{ cid: undefined, expected: undefined }
])("formatIpfsFile $cid => $expected", ({ cid, expected }) => {
expect(
utils.formatIpfsFile(cid)
).toEqual(expected);
});
test.each([
{ amount: "1000", digits: 2, expected: "10.0" },
{ amount: "10000000000000000000", expected: "10.0" },
])("formatBalance", ({ amount, digits, expected }) => {
expect(utils.formatBalance(BigNumber.from(amount), digits)).toEqual(expected);
});
test("formatRadicleId", () => {
expect(
utils.formatRadicleId(
new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 156, 38, 115, 99, 61, 118, 237, 10, 20, 115, 111, 188, 10, 117, 137, 59, 107, 76, 77, 86])
))
.toEqual("rad:git:hnrkjajuucc6zp5eknt3s9xykqsrus44cjimy");
});
test("formatProjectHash", () => {
expect(
utils.formatProjectHash(
new Uint8Array([17, 20, 69, 234, 87, 208, 172, 127, 33, 22, 110, 216, 52, 69, 104, 219, 88, 66, 50, 152, 115, 23])
)
).toEqual("45ea57d0ac7f21166ed8344568db584232987317");
});
test.each([
{ hash: "#L42", expected: 42 },
{ hash: "#ETH", expected: null },
])("formatLocationHash $hash => $expected", ({ hash, expected }) => {
expect(
utils.formatLocationHash(hash))
.toEqual(expected);
});
test.each([
{ id: "hydkkkf5ksbe5fuszdhpqhytu3q36gwagj874wxwpo5a8ti8coygh1", expected: "hydkkk…coygh1" }
])("formatSeedId $id => $expected", ({ id, expected }) => {
expect(
utils.formatSeedId(id))
.toEqual(expected);
});
test("formatRadicleUrn", () => {
expect(
utils.formatRadicleUrn("rad:git:hnrkemobagsicpf9sr95o3g551otspcd84c9o"))
.toEqual("rad:git:hnrkem…d84c9o");
});
test("formatRadicleUrn throw when wrong URN", () => {
expect(() => utils.formatRadicleUrn("hnrkemobagsicpf9sr95o3g551otspcd84c9o"))
.toThrow();
});
test("formatCAIP10Address", () => {
expect(
utils.formatCAIP10Address(
"0x1234567890123456789012345678901234567890",
"eip155",
1
))
.toEqual("0x1234567890123456789012345678901234567890@eip155:1");
});
test("formatAddress", () => {
expect(
utils.formatAddress(
"0xb5d85cbf7cb3ee0d56b3bb207d5fc4b82f43f511",
)
).toEqual("b5d8 – F511");
expect(
() => utils.formatAddress(
"0x8f91813",
)
).toThrowError('invalid address (argument="address", value="0x8f91813", code=INVALID_ARGUMENT, version=address/5.5.0)');
});
test.each([
{ hash: "0x8f918133b56bb85c18ea192549503f0ea59e3beb1f88023f442656c660018e3a", expected: "0x8f91...8e3a" },
{ hash: "0x8f91813", expected: "0x8f91813" }, // If the string length is less than 10 characters the entire string is returned.
])("formatHash $hash => $expected", ({ hash, expected }) => {
expect(
utils.formatHash(hash)
).toEqual(expected);
});
test.each([
{ commit: "a8a6a979a6261a2ec1ea85fc9a65a4a30aa22cc8", expected: "a8a6a97" },
{ commit: "a8a6a97", expected: "a8a6a97" }
])("formatCommit $commit => $expected", ({ commit, expected }) => {
expect(
utils.formatCommit(commit)
).toEqual(expected);
});
});
describe("String Assertions", () => {
test.each([
{ a: "0x1234567890123456789012345678901234567890", b: "0x1234567890123456789012345678901234567890", expected: true },
{ a: "0x1234567890123456789012345678901234567890", b: "0x5E813e48a81977c6Fdd565ed5097eb600C73C4f0", expected: false }
])("isAddressEqual", ({ a, b, expected }) => {
expect(
utils.isAddressEqual(a, b))
.toEqual(expected);
});
test.each([
{ domain: "alt-clients.radicle.xyz", expected: true },
{ domain: "0.0.0.0", expected: true }, // Pass as true since we are not in production
{ domain: "", expected: false },
])("isDomain $domain => $expected", ({ domain, expected }) => {
expect(
utils.isDomain(domain))
.toEqual(expected);
});
test.each([
{ path: "README.md", expected: true },
{ path: "README.mkd", expected: true },
{ path: "README.markdown", expected: true },
{ path: "", expected: false },
])("isMarkdownPath $path => $expected", ({ path, expected }) => {
expect(
utils.isMarkdownPath(path))
.toEqual(expected);
});
test.each([
{ id: "rad:git:hnrkemobagsicpf9sr95o3g551otspcd84c9o", expected: true },
{ id: "0x1234567890123456789012345678901234567890", expected: false },
])("isRadicleId $id => $expected", ({ id, expected }) => {
expect(
utils.isRadicleId(id))
.toEqual(expected);
});
test.each([
{ oid: "a64ae9c6d572e0ad906faa9a4a7a8d43f113278c", expected: true },
{ oid: "a64ae9c", expected: false }
])("isOid $oid => $expected", ({ oid, expected }) => {
expect(
utils.isOid(oid))
.toEqual(expected);
});
test.each([
{ address: "0x5E813e48a81977c6Fdd565ed5097eb600C73C4f0", expected: true },
{ address: "0x5E813e48a81977c6fdd565ed5097eb600c73c4f0", expected: false }, // If address is badly checksummed => false
{ address: "0x5e813e48a81977c6fdd565ed5097eb600c73c4f0", expected: true },
])("isAddress $address => $expected", ({ address, expected }) => {
expect(
utils.isAddress(
address,
))
.toBe(expected);
});
test.each([
{ url: "https://app.radicle.network", expected: true },
{ url: "http://app.radicle.network", expected: true },
{ url: "http://app", expected: true },
{ url: "://app", expected: false },
{ url: "//app", expected: false },
{ url: "app", expected: false },
])("isUrl $url => $expected", ({ url, expected }) => {
expect(
utils.isUrl(
url,
))
.toBe(expected);
});
test.each([
{ did: "did:3:kjzl6cwe1jw1481xu9oyww9bhmueqr8f5uryk4xha9jzhj6vi063e0blpnil383", expected: true },
{ did: "did:kjzl6cwe1jw1481xu9oyww9bhmueqr8f5uryk4xha9jzhj6vi063e0blpnil383", expected: false },
])("isDid $did => $expected", ({ did, expected }) => {
expect(
utils.isDid(did))
.toBe(expected);
});
});
describe("Others", () => {
test.each([
{ viewer: "https://gnosis-safe.io/app/#/safes", name: "", expected: "https://gnosis-safe.io/app/#/safes/0x5E813e48a81977c6Fdd565ed5097eb600C73C4f0" },
{ viewer: null, name: "", expected: "https://etherscan.io/address/0x5E813e48a81977c6Fdd565ed5097eb600C73C4f0" },
])("safeLink $viewer => $expected", ({ name, viewer, expected }) => {
expect(
utils.safeLink(
"0x5E813e48a81977c6Fdd565ed5097eb600C73C4f0",
{
network: {
name
},
safe: {
viewer
}
} as Config
))
.toEqual(expected);
});
test.each([
{ name: "rinkeby", expected: "https://rinkeby.etherscan.io/address/0x5E813e48a81977c6Fdd565ed5097eb600C73C4f0" },
{ name: "", expected: "https://etherscan.io/address/0x5E813e48a81977c6Fdd565ed5097eb600C73C4f0" },
])("explorerLink $name => $expected", ({ name, expected }) => {
expect(
utils.explorerLink(
"0x5E813e48a81977c6Fdd565ed5097eb600C73C4f0",
{
network: {
name,
}
} as Config
))
.toEqual(expected);
});
test.each([
{ id: "rad:git:hnrkjajuucc6zp5eknt3s9xykqsrus44cjimy", expected: new Uint8Array([156, 38, 115, 99, 61, 118, 237, 10, 20, 115, 111, 188, 10, 117, 137, 59, 107, 76, 77, 86]) },
{ id: "hnrkjajuucc6zp5eknt3s9xykqsrus44cjimy", expected: new Uint8Array([156, 38, 115, 99, 61, 118, 237, 10, 20, 115, 111, 188, 10, 117, 137, 59, 107, 76, 77, 86]) }
])("decodeRadicleId", ({ id, expected }) => {
expect(
utils.decodeRadicleId(id))
.toEqual(expected);
});
});
describe("Parse Strings", () => {
test.each([
{ label: "sebastinez.radicle.eth", expected: "sebastinez" },
{ label: "sebastinez", expected: "sebastinez" },
])("parseEnsLabel", ({ label, expected }) => {
expect(
utils.parseEnsLabel(
label,
{
registrar: {
address: "0x1234567890123456789012345678901234567890",
domain: "radicle.eth"
}
} as Config
)
).toEqual(expected);
});
test.each([
{ input: "https://twitter.com/cloudhead", expected: "cloudhead" },
{ input: "sebastinez", expected: "sebastinez" }
])("parseUsername", ({ input, expected }) => {
expect(
utils.parseUsername(input)
).toEqual(expected);
});
});
+4 -2
View File
@@ -98,6 +98,8 @@ export function formatSeedId(id: string): string {
}
export function formatRadicleUrn(id: string): string {
assert(isRadicleId(id));
return id.substring(0, 14)
+ '…'
+ id.substring(id.length - 6, id.length);
@@ -125,6 +127,7 @@ export function formatIpfsFile(ipfs: string | undefined): string | undefined {
return undefined;
}
// If the string is less than 10 characters the entire string is returned.
export function formatHash(hash: string): string {
if (hash.length < 10) return hash;
return hash.substring(0, 6)
@@ -149,8 +152,7 @@ export function capitalize(s: string): string {
return s[0].toUpperCase() + s.substring(1);
}
// Takes a domain name, eg. 'cloudhead.radicle.eth' and
// returns the label, eg. 'cloudhead', otherwise `undefined`.
// Takes a domain name, eg. 'cloudhead.radicle.eth' and returns the label, eg. 'cloudhead'.
export function parseEnsLabel(name: string, config: Config): string {
const domain = config.registrar.domain.replace(".", "\\.");
const label = name.replace(new RegExp(`\\.${domain}$`), "");