Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add the tests for $URL #197

Merged
merged 2 commits into from
Feb 5, 2024
Merged
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
77 changes: 77 additions & 0 deletions test/url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, expect, test } from "vitest";
import { $URL } from "../src";

describe("$URL", () => {
test("getters", () => {
const inputURL =
"https://john:doe@example.com:1080/path?query=value&v=1&v=2#hash";
const url = new $URL(inputURL);

expect(url.href).toEqual(inputURL);
expect(url.toString()).toEqual(url.href);
expect(url.toJSON()).toEqual(url.href);

expect(url.searchParams.toString()).toEqual("query=value&v=1&v=2");
expect(url.query).toMatchObject({ query: "value", v: ["1", "2"] });

expect(url).toMatchObject({
protocol: "https:",
host: "example.com:1080",
auth: "john:doe",
pathname: "/path",
hash: "#hash",
hostname: "example.com",
port: "1080",
username: "john",
password: "doe",
hasProtocol: 6,
isAbsolute: 6,
search: "?query=value&v=1&v=2",
origin: "https://example.com:1080",
fullpath: "/path?query=value&v=1&v=2#hash",
encodedAuth: "john:doe",
});
});

test("append", () => {
const url = new $URL(
"https://john:doe@example.com:1080/path?query=value#hash"
);
const path = new $URL("/newpath?newquery=newvalue#newhash");

url.append(path);

expect(url.toString()).toEqual(
"https://john:doe@example.com:1080/path/newpath?query=value&newquery=newvalue#newhash"
);
});

test("throws error if appending with another url with protocol", () => {
const url = new $URL("https://example.com/path");
expect(() => url.append(url)).toThrow("Cannot append a URL with protocol");
});

describe("constructor errors", () => {
const tests = [
{
// eslint-disable-next-line unicorn/no-null
input: null,
out: "URL input should be string received object (null)",
},
{
input: 123,
out: "URL input should be string received number (123)",
},
{
input: {},
out: "URL input should be string received object ([object Object])",
},
];

for (const t of tests) {
test(t.input + " throw", () => {
expect(() => new $URL(t.input as any)).toThrow(TypeError(t.out));
});
}
});
});
Loading