Skip to content

Commit 8234484

Browse files
author
Harald Wilhelmi
committed
Fixing export link
1 parent 28e205e commit 8234484

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { test, expect } from 'vitest'
2+
import { getModificationExportLink } from '@/services/modification'
3+
import { type SearchBy } from '@/utils/search'
4+
5+
test.each<{ searchBy: SearchBy; apiBaseUrl: string; expected: string }>([
6+
{
7+
searchBy: 'Gene/Chrom',
8+
apiBaseUrl: 'http://localhost:1234/',
9+
expected: 'http://localhost:1234/modification/csv/gene?rnaType=WTS&taxaId=123'
10+
},
11+
{
12+
searchBy: 'Modification',
13+
apiBaseUrl: 'http://localhost:1234/',
14+
expected: 'http://localhost:1234/modification/csv?rnaType=WTS&taxaId=123'
15+
},
16+
{
17+
searchBy: 'Modification',
18+
apiBaseUrl: '/',
19+
expected: '/modification/csv?rnaType=WTS&taxaId=123'
20+
}
21+
])('getModificationExportLink(%s)', ({ searchBy, apiBaseUrl, expected }) => {
22+
function getApiUrlCb(endpoint: string): string {
23+
return `${apiBaseUrl}${endpoint}`
24+
}
25+
26+
expect(
27+
getModificationExportLink(
28+
{
29+
rna_type: 'WTS',
30+
selections: [],
31+
state: 'complete',
32+
taxa: { taxa_id: 123, taxa_name: 'xxx', taxa_sname: 'x', domain: 'd', kingdom: 'k' },
33+
searchBy
34+
},
35+
[],
36+
getApiUrlCb
37+
)
38+
).toBe(expected)
39+
})

client/src/services/modification.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,23 @@ function getGeneFilters(searchParameters: SearchParameters): string[] {
133133

134134
function getModificationExportLink(
135135
searchParameters: SearchParameters,
136-
sortMetas?: DataTableSortMeta[]
136+
sortMetas?: DataTableSortMeta[],
137+
getApiUrlCb: (uri: string) => string = getApiUrl
137138
) {
138139
const uri =
139140
searchParameters.searchBy === 'Gene/Chrom' ? 'modification/csv/gene' : 'modification/csv'
140141
const rawParams = getQueryParametersFromSearchParameters(searchParameters, sortMetas)
141-
const url = new URL(getApiUrl(uri))
142+
const params = new URLSearchParams()
142143
for (const [k, v] of Object.entries(rawParams)) {
143144
if (v) {
144145
if (Array.isArray(v)) {
145-
v.forEach((x) => url.searchParams.append(k, x))
146+
v.forEach((x) => params.append(k, x))
146147
} else {
147-
url.searchParams.append(k, v)
148+
params.append(k, v)
148149
}
149150
}
150151
}
151-
return url.toString()
152+
return getApiUrlCb(uri) + '?' + params.toString()
152153
}
153154

154155
function getSiteParams(modification: Modification): SiteParams {

0 commit comments

Comments
 (0)