Skip to content

Commit 0251099

Browse files
committed
add getSourceId query
1 parent 0e4f651 commit 0251099

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/lib/AnalysisApps/GWAS/Utils/cohortApi.test.tsx

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { renderHook } from '../../../test/test-utils';
1515
import {
1616
GEN3_COHORT_MIDDLEWARE_API,
1717
useGetCohortDefinitionsQuery,
18-
useGetSourcesQuery
18+
useGetSourcesQuery,
1919
} from './cohortApi';
2020

2121
const server = setupServer();
@@ -42,7 +42,8 @@ const cohortDefinitionAndStatsData = {
4242
cohort_name: 'team2 - test new cohort - small',
4343
size: 80,
4444
},
45-
]};
45+
],
46+
};
4647

4748
describe('cohortApi', () => {
4849
beforeAll(() => {
@@ -100,17 +101,16 @@ describe('cohortApi', () => {
100101
});
101102

102103
it('returns error if project id not accessible ', async () => {
103-
104104
const sourceId = '1234567890';
105105
const selectedTeamProject = 'project2345';
106106

107107
server.use(
108108
http.get(
109109
`${GEN3_COHORT_MIDDLEWARE_API}/cohortdefinition-stats/by-source-id/1234567890/by-team-project`,
110110
() => {
111-
return new HttpResponse(null, {
112-
status: 403,
113-
});
111+
return new HttpResponse(null, {
112+
status: 403,
113+
});
114114
},
115115
),
116116
);
@@ -127,24 +127,21 @@ describe('cohortApi', () => {
127127
isFetching: false,
128128
isSuccess: false,
129129
isLoading: false,
130-
error: { status: 403}
130+
error: { status: 403 },
131131
});
132132
});
133133

134134
it('test for successful useGetSources ', async () => {
135-
const data = {"sources":[{"source_id":123,"source_name":"MVP-batch19000101"}]};
135+
const data = {
136+
sources: [{ source_id: 123, source_name: 'MVP-batch19000101' }],
137+
};
136138
server.use(
137-
http.get(
138-
`${GEN3_COHORT_MIDDLEWARE_API}/sources`,
139-
() => {
140-
return HttpResponse.json(data);
141-
},
142-
),
139+
http.get(`${GEN3_COHORT_MIDDLEWARE_API}/sources`, () => {
140+
return HttpResponse.json(data);
141+
}),
143142
);
144143

145-
const { result } = renderHook(() =>
146-
useGetSourcesQuery(),
147-
);
144+
const { result } = renderHook(() => useGetSourcesQuery());
148145

149146
expect(result.current.isFetching).toBe(true);
150147

@@ -154,7 +151,7 @@ describe('cohortApi', () => {
154151
isFetching: false,
155152
isSuccess: true,
156153
isLoading: false,
157-
data: data
154+
data: data,
158155
});
159156
});
160157
});

src/lib/AnalysisApps/GWAS/Utils/cohortApi.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ interface SourcesResponse {
3232

3333
export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
3434
endpoints: (builder) => ({
35+
36+
3537
getCohortDefinitions: builder.query<GWASCohortDefinitionResponse, CohortDefinitionQueryParams>({
3638
query: ({
3739
sourceId,
@@ -51,11 +53,24 @@ export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
5153
getSources: builder.query< SourcesResponse, void> ({
5254
query: () => `${GEN3_COHORT_MIDDLEWARE_API}/sources`,
5355
}),
56+
getSourceId: builder.query<string, void> ({
57+
query: () => `${GEN3_COHORT_MIDDLEWARE_API}/sources`,
58+
transformResponse: (response: SourcesResponse) => {
59+
if (Array.isArray(response?.sources) && response.sources.length === 1) {
60+
return response.sources[0].source_id;
61+
} else {
62+
const message = `Data source received in an invalid format:
63+
${JSON.stringify(response?.sources)}`;
64+
throw new Error(message);
65+
}
66+
}
67+
})
5468
}),
5569
});
5670

5771

5872
export const {
5973
useGetCohortDefinitionsQuery,
60-
useGetSourcesQuery
74+
useGetSourcesQuery,
75+
useGetSourceIdQuery,
6176
} = gwasCohortApi;

0 commit comments

Comments
 (0)