Skip to content
This repository was archived by the owner on Dec 10, 2021. It is now read-only.

Commit 49eb800

Browse files
committed
Add 100% coverage for callApi
1 parent 88c674b commit 49eb800

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

packages/superset-ui-connection/src/SupersetClientClass.ts

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export default class SupersetClientClass {
5858
}: ClientConfig = {}) {
5959
const url = new URL(
6060
host || protocol ? `${protocol || 'https:'}//${host || 'localhost'}` : baseUrl,
61+
// baseUrl for API could also be relative, so we provide current location.href
62+
// as the base of baseUrl
63+
window.location.href || 'http://localhost',
6164
);
6265
this.baseUrl = url.href.replace(/\/+$/, ''); // always strip trailing slash
6366
this.host = url.host;

packages/superset-ui-connection/src/callApi/callApi.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ function tryParsePayload(payload: Payload) {
1212
}
1313

1414
/**
15-
* Try appending search params to an URL.
15+
* Try appending search params to an URL if needed.
1616
*/
1717
function getFullUrl(partialUrl: string, params: CallApi['searchParams']) {
18-
const url = new URL(partialUrl);
1918
if (params) {
19+
const url = new URL(partialUrl, window.location.href);
2020
const search = params instanceof URLSearchParams ? params : new URLSearchParams(params);
2121
// will completely override any existing search params
2222
url.search = search.toString();
23+
return url.href;
2324
}
24-
return url.href;
25+
return partialUrl;
2526
}
2627

2728
/**
@@ -96,7 +97,7 @@ export default async function callApi({
9697

9798
if (method === 'POST' || method === 'PATCH' || method === 'PUT') {
9899
if (postPayload && jsonPayload) {
99-
throw new Error('Please provide only one of jsonPayload and postPayload');
100+
throw new Error('Please provide only one of jsonPayload or postPayload');
100101
}
101102
if (postPayload instanceof FormData) {
102103
request.body = postPayload;

packages/superset-ui-connection/test/callApi/callApi.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,68 @@ describe('callApi()', () => {
488488
expect(error.message).toEqual('Invalid payload:\n\nhaha');
489489
}
490490
});
491+
492+
it('should accept search params object', async () => {
493+
expect.assertions(3);
494+
window.location.href = 'http://localhost';
495+
fetchMock.get(`glob:*/get-search*`, { yes: 'ok' });
496+
const response = await callApi({
497+
url: '/get-search',
498+
searchParams: {
499+
abc: 1,
500+
},
501+
method: 'GET',
502+
});
503+
const result = await response.json();
504+
expect(response.status).toEqual(200);
505+
expect(result).toEqual({ yes: 'ok' });
506+
expect(fetchMock.lastUrl()).toEqual(`http://localhost/get-search?abc=1`);
507+
});
508+
509+
it('should accept URLSearchParams', async () => {
510+
expect.assertions(2);
511+
window.location.href = 'http://localhost';
512+
fetchMock.post(`glob:*/post-search*`, { yes: 'ok' });
513+
await callApi({
514+
url: '/post-search',
515+
searchParams: new URLSearchParams({
516+
abc: '1',
517+
}),
518+
method: 'POST',
519+
jsonPayload: { request: 'ok' },
520+
});
521+
expect(fetchMock.lastUrl()).toEqual(`http://localhost/post-search?abc=1`);
522+
expect(fetchMock.lastOptions()).toEqual(
523+
expect.objectContaining({
524+
body: JSON.stringify({ request: 'ok' }),
525+
}),
526+
);
527+
});
528+
529+
it('should throw when both payloads provided', async () => {
530+
expect.assertions(1);
531+
fetchMock.post('/post-both-payload', {});
532+
try {
533+
await callApi({
534+
url: '/post-both-payload',
535+
method: 'POST',
536+
postPayload: { a: 1 },
537+
jsonPayload: '{}',
538+
});
539+
} catch (error) {
540+
expect((error as Error).message).toContain('provide only one of jsonPayload or postPayload');
541+
}
542+
});
543+
544+
it('should accept FormData as postPayload', async () => {
545+
expect.assertions(1);
546+
fetchMock.post('/post-formdata', {});
547+
const payload = new FormData();
548+
await callApi({
549+
url: '/post-formdata',
550+
method: 'POST',
551+
postPayload: payload,
552+
});
553+
expect(fetchMock.lastOptions().body).toBe(payload);
554+
});
491555
});

0 commit comments

Comments
 (0)