Skip to content

Commit

Permalink
fix: Fix data API URL handling
Browse files Browse the repository at this point in the history
All configuration calls must handled asynchronously, otherwise they risk
failure in runtime configuration scenarios.
  • Loading branch information
arbrandes committed Nov 21, 2023
1 parent 02cdccc commit a622f8e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('<CreateOrRerunCourseForm />', () => {
userEvent.type(runInput, '1');
userEvent.click(createBtn);
});
await axiosMock.onPost(getCreateOrRerunCourseUrl).reply(200, { url });
await axiosMock.onPost(getCreateOrRerunCourseUrl()).reply(200, { url });
await executeThunk(updateCreateOrRerunCourseQuery({ org: 'testX', run: 'some' }), store.dispatch);

expect(window.location.assign).toHaveBeenCalledWith(`${process.env.STUDIO_BASE_URL}${url}`);
Expand All @@ -168,7 +168,7 @@ describe('<CreateOrRerunCourseForm />', () => {
const numberInput = screen.getByPlaceholderText(messages.courseNumberPlaceholder.defaultMessage);
const runInput = screen.getByPlaceholderText(messages.courseRunPlaceholder.defaultMessage);
const createBtn = screen.getByRole('button', { name: messages.createButton.defaultMessage });
await axiosMock.onPost(getCreateOrRerunCourseUrl).reply(200, { url, destinationCourseKey });
await axiosMock.onPost(getCreateOrRerunCourseUrl()).reply(200, { url, destinationCourseKey });

await act(async () => {
userEvent.type(displayNameInput, 'foo course name');
Expand Down Expand Up @@ -250,7 +250,7 @@ describe('<CreateOrRerunCourseForm />', () => {
it('shows alert error if postErrors presents', async () => {
render(<RootWrapper {...props} />);
await mockStore();
await axiosMock.onPost(getCreateOrRerunCourseUrl).reply(200, { errMsg: 'aaa' });
await axiosMock.onPost(getCreateOrRerunCourseUrl()).reply(200, { errMsg: 'aaa' });
await executeThunk(updateCreateOrRerunCourseQuery({ org: 'testX', run: 'some' }), store.dispatch);

expect(screen.getByText('aaa')).toBeInTheDocument();
Expand Down
8 changes: 4 additions & 4 deletions src/generic/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { convertObjectToSnakeCase } from '../../utils';

export const getApiBaseUrl = () => getConfig().STUDIO_BASE_URL;
export const getCreateOrRerunCourseUrl = new URL('course/', getApiBaseUrl()).href;
export const getCreateOrRerunCourseUrl = () => new URL('course/', getApiBaseUrl()).href;
export const getCourseRerunUrl = (courseId) => new URL(`/api/contentstore/v1/course_rerun/${courseId}`, getApiBaseUrl()).href;
export const getOrganizationsUrl = new URL('organizations', getApiBaseUrl()).href;
export const getOrganizationsUrl = () => new URL('organizations', getApiBaseUrl()).href;

/**
* Get's organizations data.
* @returns {Promise<Object>}
*/
export async function getOrganizations() {
const { data } = await getAuthenticatedHttpClient().get(
getOrganizationsUrl,
getOrganizationsUrl(),
);
return camelCaseObject(data);
}
Expand All @@ -37,7 +37,7 @@ export async function getCourseRerun(courseId) {
*/
export async function createOrRerunCourse(courseData) {
const { data } = await getAuthenticatedHttpClient().post(
getCreateOrRerunCourseUrl,
getCreateOrRerunCourseUrl(),
convertObjectToSnakeCase(courseData, true),
);
return camelCaseObject(data);
Expand Down
4 changes: 2 additions & 2 deletions src/generic/data/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ describe('generic api calls', () => {
org: 'edX',
run: 'Demo_Course',
};
axiosMock.onPost(getCreateOrRerunCourseUrl).reply(200, courseRerunData);
axiosMock.onPost(getCreateOrRerunCourseUrl()).reply(200, courseRerunData);
const result = await createOrRerunCourse(courseRerunData);

expect(axiosMock.history.post[0].url).toEqual(getCreateOrRerunCourseUrl);
expect(axiosMock.history.post[0].url).toEqual(getCreateOrRerunCourseUrl());
expect(result).toEqual(courseRerunData);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('<OrganizationSection />', async () => {
});
store = initializeStore();
axiosMock = new MockAdapter(getAuthenticatedHttpClient());
axiosMock.onDelete(getOrganizationsUrl).reply(200);
axiosMock.onDelete(getOrganizationsUrl()).reply(200);
await executeThunk(fetchOrganizationsQuery(), store.dispatch);
useSelector.mockReturnValue(['edX', 'org']);
});
Expand Down

0 comments on commit a622f8e

Please sign in to comment.