Skip to content
This repository was archived by the owner on Nov 23, 2024. It is now read-only.

Commit 93cfcc6

Browse files
committed
Add some simple tests for joinUrls behavior
1 parent 5891dd0 commit 93cfcc6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/fetchBaseQuery.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ export function fetchBaseQuery({ baseUrl }: { baseUrl?: string } = {}) {
3232
body,
3333
...rest,
3434
};
35+
3536
config.headers = new Headers(headers);
37+
3638
if (!config.headers.has('content-type')) {
3739
config.headers.set('content-type', 'application/json');
3840
}
@@ -42,10 +44,12 @@ export function fetchBaseQuery({ baseUrl }: { baseUrl?: string } = {}) {
4244
}
4345

4446
url = joinUrls(baseUrl, url);
47+
4548
if (params) {
4649
const searchParams = new URLSearchParams(params);
4750
url += `?${searchParams.toString()}`;
4851
}
52+
4953
const response = await fetch(url, config);
5054

5155
let resultData;

src/utils/joinsUrls.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { joinUrls } from './joinUrls';
2+
3+
test('correctly joins variations relative urls', () => {
4+
expect(joinUrls('/api/', '/banana')).toBe('/api/banana');
5+
expect(joinUrls('/api', '/banana')).toBe('/api/banana');
6+
expect(joinUrls('/api/', 'banana')).toBe('/api/banana');
7+
expect(joinUrls('/api/', '/banana/')).toBe('/api/banana/');
8+
expect(joinUrls('/', '/banana/')).toBe('/banana/');
9+
expect(joinUrls('', '/banana')).toBe('/banana');
10+
expect(joinUrls('', 'banana')).toBe('banana');
11+
});
12+
13+
test('correctly joins variations of absolute urls', () => {
14+
expect(joinUrls('https://apple.com', '/api/banana')).toBe('https://apple.com/api/banana');
15+
expect(joinUrls('https://apple.com', '/api/banana')).toBe('https://apple.com/api/banana');
16+
expect(joinUrls('https://apple.com', 'api/banana')).toBe('https://apple.com/api/banana');
17+
expect(joinUrls('https://apple.com/', 'api/banana/')).toBe('https://apple.com/api/banana/');
18+
});

0 commit comments

Comments
 (0)