-
Notifications
You must be signed in to change notification settings - Fork 597
Expand file tree
/
Copy pathindex.test.ts
More file actions
43 lines (36 loc) · 1.43 KB
/
index.test.ts
File metadata and controls
43 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { urlJoin, withHexPrefix, withoutHexPrefix } from './index.js';
describe('string', () => {
describe('urlJoin', () => {
it('joins url fragments', () => {
expect(urlJoin('http://example.com', 'foo', 'bar')).toBe('http://example.com/foo/bar');
});
it.each([
[['http://example.com/', '/foo/', '/bar/'], 'http://example.com/foo/bar'],
[['http://example.com/', '', '//', '///', '////foo//', '//bar////', 'baz'], 'http://example.com/foo/bar/baz'],
])('removes duplicate slashes', (parts, url) => {
expect(urlJoin(...parts)).toBe(url);
});
it.each([
[['http://example.com', 'a'], 'http://example.com/a'],
[['http://example.com', 'a', 'b'], 'http://example.com/a/b'],
[['x', 'y', 'z'], 'x/y/z'],
[['http://example.com', '/a/'], 'http://example.com/a'],
])('preserves single-character path segments %#', (parts, url) => {
expect(urlJoin(...parts)).toBe(url);
});
});
describe('hex prefix helpers', () => {
it('adds 0x prefix when missing', () => {
expect(withHexPrefix('abc')).toBe('0xabc');
});
it('does not duplicate 0x prefix', () => {
expect(withHexPrefix('0xabc')).toBe('0xabc');
});
it('removes 0x prefix when present', () => {
expect(withoutHexPrefix('0xdeadbeef')).toBe('deadbeef');
});
it('leaves string unchanged without prefix', () => {
expect(withoutHexPrefix('deadbeef')).toBe('deadbeef');
});
});
});