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

Commit

Permalink
Add some simple tests for joinUrls behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
msutkowski committed Nov 7, 2020
1 parent 5891dd0 commit fe43058
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/fetchBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export function fetchBaseQuery({ baseUrl }: { baseUrl?: string } = {}) {
body,
...rest,
};

config.headers = new Headers(headers);

if (!config.headers.has('content-type')) {
config.headers.set('content-type', 'application/json');
}
Expand All @@ -42,10 +44,12 @@ export function fetchBaseQuery({ baseUrl }: { baseUrl?: string } = {}) {
}

url = joinUrls(baseUrl, url);

if (params) {
const searchParams = new URLSearchParams(params);
url += `?${searchParams.toString()}`;
}

const response = await fetch(url, config);

let resultData;
Expand Down
28 changes: 28 additions & 0 deletions src/utils/joinsUrls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { joinUrls } from './joinUrls';

test('correctly joins variations relative urls', () => {
expect(joinUrls('/api/', '/banana')).toBe('/api/banana');
expect(joinUrls('/api', '/banana')).toBe('/api/banana');

expect(joinUrls('/api/', 'banana')).toBe('/api/banana');
expect(joinUrls('/api/', '/banana/')).toBe('/api/banana/');

expect(joinUrls('/', '/banana/')).toBe('/banana/');
expect(joinUrls('/', 'banana/')).toBe('/banana/');

expect(joinUrls('/', '/banana')).toBe('/banana');
expect(joinUrls('/', 'banana')).toBe('/banana');

expect(joinUrls('', '/banana')).toBe('/banana');
expect(joinUrls('', 'banana')).toBe('banana');
});

test('correctly joins variations of absolute urls', () => {
expect(joinUrls('https://apple.com', '/api/banana/')).toBe('https://apple.com/api/banana/');
expect(joinUrls('https://apple.com', '/api/banana')).toBe('https://apple.com/api/banana');

expect(joinUrls('https://apple.com/', 'api/banana/')).toBe('https://apple.com/api/banana/');
expect(joinUrls('https://apple.com/', 'api/banana')).toBe('https://apple.com/api/banana');

expect(joinUrls('https://apple.com/', 'api/banana/')).toBe('https://apple.com/api/banana/');
});

0 comments on commit fe43058

Please sign in to comment.