Skip to content

Commit 5722f69

Browse files
committed
tests:
add unit tests for a function as example
1 parent b37a9a9 commit 5722f69

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { extractDomainIP } from "../../src/utils/gateway";
4+
5+
describe("extractDomainIP", () => {
6+
it("should extract the domain from a URL with a protocol", () => {
7+
const domain = extractDomainIP("https://example.com:8080");
8+
expect(domain).toBe("example.com");
9+
});
10+
11+
it("should extract the IPv6 address from a URL", () => {
12+
const ipv6 = extractDomainIP("https://[::1]:8080");
13+
expect(ipv6).toBe("::1");
14+
});
15+
16+
it("should throw an error when there is no domain or IP address", () => {
17+
expect(() => extractDomainIP("http://:8080")).toThrow(
18+
'Invalid input "http://:8080": No domain or IP address found.',
19+
);
20+
});
21+
22+
it("should throw an error for invalid IPv6 format", () => {
23+
expect(() => extractDomainIP("https://[]:8080")).toThrow(
24+
'Invalid input "https://[]:8080": Invalid IPv6 address format.',
25+
);
26+
});
27+
28+
it("should extract the domain from a URL without a port", () => {
29+
const domain = extractDomainIP("https://example.com");
30+
expect(domain).toBe("example.com");
31+
});
32+
33+
it("should extract the IPv4 address from a URL", () => {
34+
const ipv4 = extractDomainIP("http://192.168.0.1:3000");
35+
expect(ipv4).toBe("192.168.0.1");
36+
});
37+
38+
it("should handle plain domain strings without protocols", () => {
39+
const domain = extractDomainIP("example.com");
40+
expect(domain).toBe("example.com");
41+
});
42+
});

0 commit comments

Comments
 (0)