|
13 | 13 | const URL = require('../URL').URL; |
14 | 14 |
|
15 | 15 | describe('URL', function () { |
16 | | - it('should pass Mozilla Dev Network examples', () => { |
17 | | - const a = new URL('/', 'https://developer.mozilla.org'); |
18 | | - expect(a.href).toBe('https://developer.mozilla.org/'); |
19 | | - const b = new URL('https://developer.mozilla.org'); |
20 | | - expect(b.href).toBe('https://developer.mozilla.org/'); |
21 | | - const c = new URL('en-US/docs', b); |
22 | | - expect(c.href).toBe('https://developer.mozilla.org/en-US/docs'); |
23 | | - const d = new URL('/en-US/docs', b); |
24 | | - expect(d.href).toBe('https://developer.mozilla.org/en-US/docs'); |
25 | | - const f = new URL('/en-US/docs', d); |
26 | | - expect(f.href).toBe('https://developer.mozilla.org/en-US/docs'); |
27 | | - // from original test suite, but requires complex implementation |
28 | | - // const g = new URL( |
29 | | - // '/en-US/docs', |
30 | | - // 'https://developer.mozilla.org/fr-FR/toto', |
31 | | - // ); |
32 | | - // expect(g.href).toBe('https://developer.mozilla.org/en-US/docs'); |
33 | | - const h = new URL('/en-US/docs', a); |
34 | | - expect(h.href).toBe('https://developer.mozilla.org/en-US/docs'); |
35 | | - const i = new URL('http://github.com', 'http://google.com'); |
36 | | - expect(i.href).toBe('http://github.com/'); |
37 | | - // Support Bare Hosts |
38 | | - const j = new URL('home', 'http://localhost'); |
39 | | - expect(j.href).toBe('http://localhost/home'); |
40 | | - // Insert / between Base and Path if missing |
41 | | - const k = new URL('en-US/docs', 'https://developer.mozilla.org'); |
42 | | - expect(k.href).toBe('https://developer.mozilla.org/en-US/docs'); |
| 16 | + it('should correctly parse all URL components', () => { |
| 17 | + const url = new URL('https://username:[email protected]:8080/docs/path?query=testQuery&key=value#fragment'); |
| 18 | + |
| 19 | + expect(url.hash).toBe('#fragment'); |
| 20 | + expect(url.host).toBe('reactnative.dev:8080'); |
| 21 | + expect(url.hostname).toBe('reactnative.dev'); |
| 22 | + expect(url.href).toBe('https://username:[email protected]:8080/docs/path?query=testQuery&key=value#fragment'); |
| 23 | + expect(url.origin).toBe('https://reactnative.dev:8080'); |
| 24 | + expect(url.password).toBe('password'); |
| 25 | + expect(url.username).toBe('username'); |
| 26 | + expect(url.pathname).toBe('/docs/path'); |
| 27 | + expect(url.port).toBe('8080'); |
| 28 | + expect(url.protocol).toBe('https:'); |
| 29 | + expect(url.search).toBe('?query=testQuery&key=value'); |
| 30 | + |
| 31 | + // Test searchParams parsing |
| 32 | + expect(url.searchParams.get('query')).toBe('testQuery'); |
| 33 | + expect(url.searchParams.get('key')).toBe('value'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should handle URLs without authentication correctly', () => { |
| 37 | + const url = new URL('https://reactnative.dev/docs'); |
| 38 | + |
| 39 | + expect(url.username).toBe(''); |
| 40 | + expect(url.password).toBe(''); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle URLs without query parameters', () => { |
| 44 | + const url = new URL('https://reactnative.dev/docs'); |
| 45 | + |
| 46 | + expect(url.search).toBe(''); |
| 47 | + expect(url.searchParams.toString()).toBe(''); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should handle URLs without a port', () => { |
| 51 | + const url = new URL('https://reactnative.dev/docs'); |
| 52 | + |
| 53 | + expect(url.port).toBe(''); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle URLs without a hash', () => { |
| 57 | + const url = new URL('https://reactnative.dev/docs'); |
| 58 | + |
| 59 | + expect(url.hash).toBe(''); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle URLs with relative paths correctly', () => { |
| 63 | + const base = new URL('https://developer.mozilla.org'); |
| 64 | + |
| 65 | + const url1 = new URL('/en-US/docs', base); |
| 66 | + expect(url1.href).toBe('https://developer.mozilla.org/en-US/docs'); |
| 67 | + |
| 68 | + const url2 = new URL('en-US/docs', base); |
| 69 | + expect(url2.href).toBe('https://developer.mozilla.org/en-US/docs'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should support bare hosts and relative paths', () => { |
| 73 | + const url = new URL('home', 'http://localhost'); |
| 74 | + expect(url.href).toBe('http://localhost/home'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should correctly resolve full URLs when given a base URL', () => { |
| 78 | + const url = new URL('http://github.com', 'http://google.com'); |
| 79 | + expect(url.href).toBe('http://github.com/'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should insert / between base and path if missing', () => { |
| 83 | + const url = new URL('en-US/docs', 'https://developer.mozilla.org'); |
| 84 | + expect(url.href).toBe('https://developer.mozilla.org/en-US/docs'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should default pathname to "/" if no path is provided', () => { |
| 88 | + const url = new URL('https://example.com'); |
| 89 | + expect(url.pathname).toBe('/'); |
43 | 90 | }); |
44 | 91 | }); |
0 commit comments