-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
next-url.test.ts
212 lines (175 loc) · 6.41 KB
/
next-url.test.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* eslint-env jest */
import { NextURL } from 'next/dist/server/web/next-url'
// TODO Make NextURL extend URL
it.skip('has the right shape and prototype', () => {
const parsed = new NextURL('/about?param1=value1', 'http://127.0.0.1')
expect(parsed).toBeInstanceOf(URL)
})
it('allows to the pathname', async () => {
const parsed = new NextURL('/about?param1=value1', 'http://127.0.0.1:3000')
expect(parsed.basePath).toEqual('')
expect(parsed.hostname).toEqual('localhost')
expect(parsed.host).toEqual('localhost:3000')
expect(parsed.href).toEqual('http://localhost:3000/about?param1=value1')
parsed.pathname = '/hihi'
expect(parsed.href).toEqual('http://localhost:3000/hihi?param1=value1')
})
it('allows to change the host', () => {
const parsed = new NextURL('/about?param1=value1', 'http://127.0.0.1')
expect(parsed.hostname).toEqual('localhost')
expect(parsed.host).toEqual('localhost')
expect(parsed.href).toEqual('http://localhost/about?param1=value1')
parsed.hostname = 'foo.com'
expect(parsed.hostname).toEqual('foo.com')
expect(parsed.host).toEqual('foo.com')
expect(parsed.href).toEqual('http://foo.com/about?param1=value1')
expect(parsed.toString()).toEqual('http://foo.com/about?param1=value1')
})
it('does noop changing to an invalid hostname', () => {
const url = new NextURL('https://foo.com/example')
url.hostname = ''
expect(url.toString()).toEqual('https://foo.com/example')
})
it('allows to change the whole href', () => {
const url = new NextURL('https://localhost.com/foo')
expect(url.hostname).toEqual('localhost.com')
expect(url.protocol).toEqual('https:')
expect(url.host).toEqual('localhost.com')
url.href = 'http://foo.com/bar'
expect(url.hostname).toEqual('foo.com')
expect(url.protocol).toEqual('http:')
expect(url.host).toEqual('foo.com')
})
it('allows to update search params', () => {
const url = new NextURL('/example', 'http://localhost.com')
url.searchParams.set('foo', 'bar')
expect(url.search).toEqual('?foo=bar')
expect(url.toString()).toEqual('http://localhost.com/example?foo=bar')
})
it('parses and formats the basePath', () => {
const url = new NextURL('/root/example', {
base: 'http://127.0.0.1',
basePath: '/root',
})
expect(url.basePath).toEqual('/root')
expect(url.pathname).toEqual('/example')
expect(url.toString()).toEqual('http://localhost/root/example')
const url2 = new NextURL('https://foo.com/root/bar', {
basePath: '/root',
})
expect(url2.basePath).toEqual('/root')
expect(url2.pathname).toEqual('/bar')
expect(url2.toString()).toEqual('https://foo.com/root/bar')
url2.basePath = '/test'
expect(url2.basePath).toEqual('/test')
expect(url2.pathname).toEqual('/bar')
expect(url2.toString()).toEqual('https://foo.com/test/bar')
const url3 = new NextURL('https://foo.com/example', {
basePath: '/root',
})
expect(url3.basePath).toEqual('')
url3.href = 'http://localhost.com/root/example'
expect(url3.basePath).toEqual('/root')
expect(url3.pathname).toEqual('/example')
expect(url3.toString()).toEqual('http://localhost.com/root/example')
})
it('allows to get empty locale when there is no locale', () => {
const url = new NextURL('https://localhost:3000/foo')
expect(url.locale).toEqual('')
})
it('doesnt allow to set an unexisting locale', () => {
const url = new NextURL('https://localhost:3000/foo')
let error: Error | null = null
try {
url.locale = 'foo'
} catch (err) {
error = err
}
expect(error).toBeInstanceOf(TypeError)
expect(error.message).toEqual(
'The NextURL configuration includes no locale "foo"'
)
})
it('always get a default locale', () => {
const url = new NextURL('/bar', {
base: 'http://127.0.0.1',
i18n: {
defaultLocale: 'en',
locales: ['en', 'es', 'fr'],
},
})
expect(url.locale).toEqual('en')
})
it('parses and formats the default locale', () => {
const url = new NextURL('/es/bar', {
base: 'http://127.0.0.1',
basePath: '/root',
i18n: {
defaultLocale: 'en',
locales: ['en', 'es', 'fr'],
},
})
expect(url.locale).toEqual('es')
expect(url.toString()).toEqual('http://localhost/es/bar')
url.basePath = '/root'
expect(url.locale).toEqual('es')
expect(url.toString()).toEqual('http://localhost/root/es/bar')
url.locale = 'en'
expect(url.locale).toEqual('en')
expect(url.toString()).toEqual('http://localhost/root/bar')
url.locale = 'fr'
expect(url.locale).toEqual('fr')
expect(url.toString()).toEqual('http://localhost/root/fr/bar')
})
it('consider 127.0.0.1 and variations as localhost', () => {
const httpUrl = new NextURL('http://localhost:3000/hello')
expect(new NextURL('http://127.0.0.1:3000/hello')).toStrictEqual(httpUrl)
expect(new NextURL('http://127.0.1.0:3000/hello')).toStrictEqual(httpUrl)
expect(new NextURL('http://::1:3000/hello')).toStrictEqual(httpUrl)
const httpsUrl = new NextURL('https://localhost:3000/hello')
expect(new NextURL('https://127.0.0.1:3000/hello')).toStrictEqual(httpsUrl)
expect(new NextURL('https://127.0.1.0:3000/hello')).toStrictEqual(httpsUrl)
expect(new NextURL('https://::1:3000/hello')).toStrictEqual(httpsUrl)
})
it('allows to change the port', () => {
const url = new NextURL('https://localhost:3000/foo')
url.port = '3001'
expect(url.href).toEqual('https://localhost:3001/foo')
url.port = '80'
expect(url.href).toEqual('https://localhost:80/foo')
url.port = ''
expect(url.href).toEqual('https://localhost/foo')
})
it('allows to clone a new copy', () => {
const url = new NextURL('/root/es/bar', {
base: 'http://127.0.0.1',
basePath: '/root',
i18n: {
defaultLocale: 'en',
locales: ['en', 'es', 'fr'],
},
})
const clone = url.clone()
clone.pathname = '/test'
clone.basePath = '/root-test'
expect(url.toString()).toEqual('http://localhost/root/es/bar')
expect(clone.toString()).toEqual('http://localhost/root-test/es/test')
})
it('does not add locale for api route', () => {
const url = new NextURL('http:///localhost:3000/api', {
i18n: {
defaultLocale: 'en',
locales: ['en', 'es', 'fr'],
},
})
url.locale = 'fr'
let expected = 'http://localhost:3000/api'
expect(url.href).toEqual(expected)
expect(url.toString()).toEqual(expected)
expect(url.toJSON()).toEqual(expected)
url.pathname = '/api/hello'
expected = 'http://localhost:3000/api/hello'
expect(url.href).toEqual(expected)
expect(url.toString()).toEqual(expected)
expect(url.toJSON()).toEqual(expected)
})