forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
160 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// @ts-check | ||
import { camelCaseObject, getConfig } from '@edx/frontend-platform'; | ||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
const getApiBaseUrl = () => getConfig().STUDIO_BASE_URL; | ||
const getTaxonomyDetailApiUrl = (taxonomyId) => new URL( | ||
`api/content_tagging/v1/taxonomies/${taxonomyId}/`, | ||
getApiBaseUrl(), | ||
).href; | ||
|
||
/** | ||
* @param {number} taxonomyId | ||
* @returns {import('@tanstack/react-query').UseQueryResult<import('../types.mjs').TaxonomyData>} | ||
*/ // eslint-disable-next-line import/prefer-default-export | ||
export const useTaxonomyDetailData = (taxonomyId) => ( | ||
useQuery({ | ||
queryKey: ['taxonomyDetail', taxonomyId], | ||
queryFn: () => getAuthenticatedHttpClient().get(getTaxonomyDetailApiUrl(taxonomyId)) | ||
.then(camelCaseObject) | ||
.then((response) => response.data), | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { | ||
useTaxonomyDetailData, | ||
} from './api'; | ||
|
||
const mockHttpClient = { | ||
get: jest.fn(), | ||
}; | ||
|
||
jest.mock('@tanstack/react-query', () => ({ | ||
useQuery: jest.fn(), | ||
})); | ||
|
||
jest.mock('@edx/frontend-platform/auth', () => ({ | ||
getAuthenticatedHttpClient: jest.fn(() => mockHttpClient), | ||
})); | ||
|
||
describe('useTaxonomyDetailData', () => { | ||
it('should call useQuery with the correct parameters', () => { | ||
useTaxonomyDetailData('1'); | ||
|
||
expect(useQuery).toHaveBeenCalledWith({ | ||
queryKey: ['taxonomyDetail', '1'], | ||
queryFn: expect.any(Function), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// @ts-check | ||
import { | ||
useTaxonomyDetailData, | ||
} from './api'; | ||
|
||
/** | ||
* @param {number} taxonomyId | ||
* @returns {Pick<import('@tanstack/react-query').UseQueryResult, "error" | "isError" | "isFetched" | "isSuccess">} | ||
*/ | ||
export const useTaxonomyDetailDataStatus = (taxonomyId) => { | ||
const { | ||
isError, | ||
error, | ||
isFetched, | ||
isSuccess, | ||
} = useTaxonomyDetailData(taxonomyId); | ||
return { | ||
isError, | ||
error, | ||
isFetched, | ||
isSuccess, | ||
}; | ||
}; | ||
|
||
/** | ||
* @param {number} taxonomyId | ||
* @returns {import("../types.mjs").TaxonomyData | undefined} | ||
*/ | ||
export const useTaxonomyDetailDataResponse = (taxonomyId) => { | ||
const { isSuccess, data } = useTaxonomyDetailData(taxonomyId); | ||
if (isSuccess) { | ||
return data; | ||
} | ||
|
||
return undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
useTaxonomyDetailData, | ||
} from './api'; | ||
import { | ||
useTaxonomyDetailDataStatus, | ||
useTaxonomyDetailDataResponse, | ||
} from './selectors'; | ||
|
||
jest.mock('./api', () => ({ | ||
__esModule: true, | ||
useTaxonomyDetailData: jest.fn(), | ||
})); | ||
|
||
describe('useTaxonomyDetailDataStatus', () => { | ||
it('should return status values', () => { | ||
const status = { | ||
isError: false, | ||
error: undefined, | ||
isFetched: true, | ||
isSuccess: true, | ||
}; | ||
|
||
useTaxonomyDetailData.mockReturnValueOnce(status); | ||
|
||
const result = useTaxonomyDetailDataStatus(0); | ||
|
||
expect(result).toEqual(status); | ||
}); | ||
}); | ||
|
||
describe('useTaxonomyDetailDataResponse', () => { | ||
it('should return data when status is success', () => { | ||
useTaxonomyDetailData.mockReturnValueOnce({ isSuccess: true, data: 'data' }); | ||
|
||
const result = useTaxonomyDetailDataResponse(); | ||
|
||
expect(result).toEqual('data'); | ||
}); | ||
|
||
it('should return undefined when status is not success', () => { | ||
useTaxonomyDetailData.mockReturnValueOnce({ isSuccess: false }); | ||
|
||
const result = useTaxonomyDetailDataResponse(); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { defineMessages } from '@edx/frontend-platform/i18n'; | ||
|
||
const messages = defineMessages({ | ||
taxonomyDetailsHeader: { | ||
id: 'course-authoring.taxonomy-detail.side-card.header', | ||
defaultMessage: 'Taxonomy details', | ||
}, | ||
taxonomyDetailsName: { | ||
id: 'course-authoring.taxonomy-detail.side-card.name', | ||
defaultMessage: 'Title', | ||
}, | ||
taxonomyDetailsDescription: { | ||
id: 'course-authoring.taxonomy-detail.side-card.description', | ||
defaultMessage: 'Description', | ||
}, | ||
actionsButtonLabel: { | ||
id: 'course-authoring.taxonomy-detail.action.button.label', | ||
defaultMessage: 'Actions', | ||
}, | ||
}); | ||
|
||
export default messages; |