Skip to content

Commit

Permalink
refactor: Move api to data to match with 0002 ADR
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Oct 31, 2023
1 parent b09b86a commit df8432d
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 132 deletions.
2 changes: 1 addition & 1 deletion src/taxonomy/TaxonomyListPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Header from '../header';
import SubHeader from '../generic/sub-header/SubHeader';
import messages from './messages';
import TaxonomyCard from './taxonomy-card';
import { useTaxonomyListDataResponse, useIsTaxonomyListDataLoaded } from './api/hooks/selectors';
import { useTaxonomyListDataResponse, useIsTaxonomyListDataLoaded } from './hooks';

const TaxonomyListPage = () => {
const intl = useIntl();
Expand Down
4 changes: 2 additions & 2 deletions src/taxonomy/TaxonomyListPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { act, render } from '@testing-library/react';
import initializeStore from '../store';

import TaxonomyListPage from './TaxonomyListPage';
import { useTaxonomyListDataResponse, useIsTaxonomyListDataLoaded } from './api/hooks/selectors';
import { useTaxonomyListDataResponse, useIsTaxonomyListDataLoaded } from './hooks';

let store;

jest.mock('./api/hooks/selectors', () => ({
jest.mock('./hooks', () => ({
useTaxonomyListDataResponse: jest.fn(),
useIsTaxonomyListDataLoaded: jest.fn(),
}));
Expand Down
2 changes: 2 additions & 0 deletions src/taxonomy/__mocks__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export { default as taxonomyListMock } from './taxonomyListMock';
50 changes: 50 additions & 0 deletions src/taxonomy/__mocks__/taxonomyListMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module.exports = {
next: null,
previous: null,
count: 4,
numPages: 1,
currentPage: 1,
start: 0,
results: [
{
id: -2,
name: 'Content Authors',
description: 'Allows tags for any user ID created on the instance.',
enabled: true,
allowMultiple: false,
allowFreeText: false,
systemDefined: true,
visibleToAuthors: false,
},
{
id: -1,
name: 'Languages',
description: 'lang lang lang lang lang lang lang lang',
enabled: true,
allowMultiple: false,
allowFreeText: false,
systemDefined: true,
visibleToAuthors: true,
},
{
id: 1,
name: 'Taxonomy',
description: 'This is a Description',
enabled: true,
allowMultiple: false,
allowFreeText: false,
systemDefined: false,
visibleToAuthors: true,
},
{
id: 2,
name: 'Taxonomy long long long long long long long long long long long long long long long long long long long',
description: 'This is a Description long lon',
enabled: true,
allowMultiple: false,
allowFreeText: false,
systemDefined: false,
visibleToAuthors: true,
},
],
};
26 changes: 0 additions & 26 deletions src/taxonomy/api/hooks/api.js

This file was deleted.

56 changes: 0 additions & 56 deletions src/taxonomy/api/hooks/api.test.js

This file was deleted.

27 changes: 0 additions & 27 deletions src/taxonomy/api/hooks/selectors.js

This file was deleted.

29 changes: 29 additions & 0 deletions src/taxonomy/data/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @ts-check
import { camelCaseObject, getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';

const getApiBaseUrl = () => getConfig().STUDIO_BASE_URL;
export const getTaxonomyListApiUrl = () => new URL('api/content_tagging/v1/taxonomies/?enabled=true', getApiBaseUrl()).href;
export const getExportTaxonomyApiUrl = (pk, format) => new URL(
`api/content_tagging/v1/taxonomies/${pk}/export/?output_format=${format}&download=1`,
getApiBaseUrl(),
).href;

/**
* Get list of taxonomies.
* @returns {Promise<Object>}
*/
export async function getTaxonomyListData() {
const { data } = await getAuthenticatedHttpClient().get(getTaxonomyListApiUrl());
return camelCaseObject(data);
}

/**
* Downloads the file of the exported taxonomy
* @param {number} pk
* @param {string} format
* @returns {void}
*/
export function getTaxonomyExportFile(pk, format) {
window.location.href = getExportTaxonomyApiUrl(pk, format);
}
61 changes: 61 additions & 0 deletions src/taxonomy/data/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import MockAdapter from 'axios-mock-adapter';
import { initializeMockApp } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';

import { taxonomyListMock } from '../__mocks__';

import {
getTaxonomyListApiUrl,
getExportTaxonomyApiUrl,
getTaxonomyListData,
getTaxonomyExportFile,
} from './api';

let axiosMock;

describe('taxonomy api calls', () => {
const { location } = window;
beforeEach(() => {
initializeMockApp({
authenticatedUser: {
userId: 3,
username: 'abc123',
administrator: true,
roles: [],
},
});
axiosMock = new MockAdapter(getAuthenticatedHttpClient());
});

afterEach(() => {
jest.clearAllMocks();
});

beforeAll(() => {
delete window.location;
window.location = {
href: '',
};
});

afterAll(() => {
window.location = location;
});

it('should get taxonomy list data', async () => {
axiosMock.onGet(getTaxonomyListApiUrl()).reply(200, taxonomyListMock);
const result = await getTaxonomyListData();

expect(axiosMock.history.get[0].url).toEqual(getTaxonomyListApiUrl());
expect(result).toEqual(taxonomyListMock);
});

it('should set window.location.href correctly', () => {
const pk = 1;
const format = 'json';

getTaxonomyExportFile(pk, format);

expect(window.location.href).toEqual(getExportTaxonomyApiUrl(pk, format));
});
});
14 changes: 14 additions & 0 deletions src/taxonomy/data/thunks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @ts-check
import { getTaxonomyExportFile } from './api';

/**
* Downloads the file of the exported taxonomy
* @param {number} pk
* @param {string} format
* @returns {void}
*/
const exportTaxonomy = (pk, format) => (
getTaxonomyExportFile(pk, format)

Check warning on line 11 in src/taxonomy/data/thunks.js

View check run for this annotation

Codecov / codecov/patch

src/taxonomy/data/thunks.js#L11

Added line #L11 was not covered by tests
);

export default exportTaxonomy;
File renamed without changes.
9 changes: 5 additions & 4 deletions src/taxonomy/export-modal/ExportModal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { AppProvider } from '@edx/frontend-platform/react';
import { render, fireEvent } from '@testing-library/react';
import ExportModal from '.';
import initializeStore from '../../store';
import { callExportTaxonomy } from '../api/hooks/selectors';
import exportTaxonomy from '../data/thunks';

const onClose = jest.fn();
let store;
const taxonomyId = 1;

jest.mock('../api/hooks/selectors', () => ({
callExportTaxonomy: jest.fn(),
jest.mock('../data/thunks', () => ({
__esModule: true,
default: jest.fn(),
}));

const ExportModalComponent = () => (
Expand Down Expand Up @@ -48,6 +49,6 @@ describe('<ExportModal />', async () => {
fireEvent.click(getByText('Export'));

expect(onClose).toHaveBeenCalled();
expect(callExportTaxonomy).toHaveBeenCalledWith(taxonomyId, 'json');
expect(exportTaxonomy).toHaveBeenCalledWith(taxonomyId, 'json');
});
});
4 changes: 2 additions & 2 deletions src/taxonomy/export-modal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import PropTypes from 'prop-types';
import { useIntl } from '@edx/frontend-platform/i18n';
import messages from './messages';
import { callExportTaxonomy } from '../api/hooks/selectors';
import exportTaxonomy from '../data/thunks';

const ExportModal = ({
taxonomyId,
Expand All @@ -20,7 +20,7 @@ const ExportModal = ({

const onClickExport = () => {
onClose();
callExportTaxonomy(taxonomyId, outputFormat);
exportTaxonomy(taxonomyId, outputFormat);
};

return (
Expand Down
34 changes: 34 additions & 0 deletions src/taxonomy/hooks.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// @ts-check
import { useQuery } from '@tanstack/react-query';
import { getTaxonomyListData } from './data/api';

/**
* Builds the query yo get the taxonomy list
* @returns {import("./data/types.mjs").UseQueryResult}
*/
const useTaxonomyListData = () => (
useQuery({
queryKey: ['taxonomyList'],
queryFn: getTaxonomyListData,
})
);

/**
* Gets the taxonomy list data
* @returns {import("./data/types.mjs").TaxonomyListData | undefined}
*/
export const useTaxonomyListDataResponse = () => {
const response = useTaxonomyListData();
if (response.status === 'success') {
return response.data;
}
return undefined;
};

/**
* Returns the status of the taxonomy list query
* @returns {boolean}
*/
export const useIsTaxonomyListDataLoaded = () => (
useTaxonomyListData().status === 'success'
);
Loading

0 comments on commit df8432d

Please sign in to comment.