Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow user provided value if can auto-create orgs [FC-0076] #1582

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 82 additions & 42 deletions src/library-authoring/create-library/CreateLibrary.test.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React from 'react';
import MockAdapter from 'axios-mock-adapter';
import { initializeMockApp } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import { AppProvider } from '@edx/frontend-platform/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { fireEvent, render, waitFor } from '@testing-library/react';
import type MockAdapter from 'axios-mock-adapter';
import userEvent from '@testing-library/user-event';

import initializeStore from '../../store';
import {
fireEvent,
initializeMocks,
render,
waitFor,
} from '../../testUtils';
import { studioHomeMock } from '../../studio-home/__mocks__';
import { getStudioHomeApiUrl } from '../../studio-home/data/api';
import { getApiWaffleFlagsUrl } from '../../data/api';
import { CreateLibrary } from '.';
import { getContentLibraryV2CreateApiUrl } from './data/api';

let store;
const mockNavigate = jest.fn();
let axiosMock: MockAdapter;

Expand All @@ -29,51 +30,26 @@ jest.mock('../../generic/data/apiHooks', () => ({
}),
}));

const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
});

const RootWrapper = () => (
<AppProvider store={store}>
<IntlProvider locale="en" messages={{}}>
<QueryClientProvider client={queryClient}>
<CreateLibrary />
</QueryClientProvider>
</IntlProvider>
</AppProvider>
);

describe('<CreateLibrary />', () => {
beforeEach(() => {
initializeMockApp({
authenticatedUser: {
userId: 3,
username: 'abc123',
administrator: true,
roles: [],
},
});
store = initializeStore();

axiosMock = new MockAdapter(getAuthenticatedHttpClient());
axiosMock = initializeMocks().axiosMock;
axiosMock
.onGet(getApiWaffleFlagsUrl(undefined))
.reply(200, {});
});

afterEach(() => {
jest.clearAllMocks();
axiosMock.restore();
queryClient.clear();
});

test('call api data with correct data', async () => {
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, studioHomeMock);
axiosMock.onPost(getContentLibraryV2CreateApiUrl()).reply(200, {
id: 'library-id',
});

const { getByRole } = render(<RootWrapper />);
const { getByRole } = render(<CreateLibrary />);

const titleInput = getByRole('textbox', { name: /library name/i });
userEvent.click(titleInput);
Expand All @@ -98,11 +74,75 @@ describe('<CreateLibrary />', () => {
});
});

test('cannot create new org unless allowed', async () => {
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, studioHomeMock);
axiosMock.onPost(getContentLibraryV2CreateApiUrl()).reply(200, {
id: 'library-id',
});

const { getByRole, getByText } = render(<CreateLibrary />);

const titleInput = getByRole('textbox', { name: /library name/i });
userEvent.click(titleInput);
userEvent.type(titleInput, 'Test Library Name');

const orgInput = getByRole('combobox', { name: /organization/i });
userEvent.click(orgInput);
userEvent.type(orgInput, 'NewOrg');
userEvent.tab();

const slugInput = getByRole('textbox', { name: /library id/i });
userEvent.click(slugInput);
userEvent.type(slugInput, 'test_library_slug');

fireEvent.click(getByRole('button', { name: /create/i }));
await waitFor(() => {
expect(axiosMock.history.post.length).toBe(0);
});
expect(getByText('Required field.')).toBeInTheDocument();
});

test('can create new org if allowed', async () => {
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, {
...studioHomeMock,
allow_to_create_new_org: true,
allowToCreateNewOrg: true,
pomegranited marked this conversation as resolved.
Show resolved Hide resolved
});
axiosMock.onPost(getContentLibraryV2CreateApiUrl()).reply(200, {
id: 'library-id',
});

const { getByRole } = render(<CreateLibrary />);

const titleInput = getByRole('textbox', { name: /library name/i });
userEvent.click(titleInput);
userEvent.type(titleInput, 'Test Library Name');

const orgInput = getByRole('combobox', { name: /organization/i });
userEvent.click(orgInput);
userEvent.type(orgInput, 'NewOrg');
userEvent.tab();

const slugInput = getByRole('textbox', { name: /library id/i });
userEvent.click(slugInput);
userEvent.type(slugInput, 'test_library_slug');

fireEvent.click(getByRole('button', { name: /create/i }));
await waitFor(() => {
expect(axiosMock.history.post.length).toBe(1);
expect(axiosMock.history.post[0].data).toBe(
'{"description":"","title":"Test Library Name","org":"NewOrg","slug":"test_library_slug"}',
);
expect(mockNavigate).toHaveBeenCalledWith('/library/library-id');
});
});

test('show api error', async () => {
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, studioHomeMock);
axiosMock.onPost(getContentLibraryV2CreateApiUrl()).reply(400, {
field: 'Error message',
});
const { getByRole, getByTestId } = render(<RootWrapper />);
const { getByRole, getByTestId } = render(<CreateLibrary />);

const titleInput = getByRole('textbox', { name: /library name/i });
userEvent.click(titleInput);
Expand Down Expand Up @@ -130,7 +170,7 @@ describe('<CreateLibrary />', () => {
});

test('cancel creating library navigates to libraries page', async () => {
const { getByRole } = render(<RootWrapper />);
const { getByRole } = render(<CreateLibrary />);

fireEvent.click(getByRole('button', { name: /cancel/i }));
await waitFor(() => {
Expand Down
10 changes: 9 additions & 1 deletion src/library-authoring/create-library/CreateLibrary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import FormikErrorFeedback from '../../generic/FormikErrorFeedback';
import AlertError from '../../generic/alert-error';
import { useOrganizationListData } from '../../generic/data/apiHooks';
import SubHeader from '../../generic/sub-header/SubHeader';
import { useStudioHome } from '../../studio-home/hooks';
import { useCreateLibraryV2 } from './data/apiHooks';
import messages from './messages';

Expand All @@ -42,6 +43,8 @@ const CreateLibrary = () => {
isLoading: isOrganizationListLoading,
} = useOrganizationListData();

const { studioHomeData: { allowToCreateNewOrg } } = useStudioHome();

const handleOnClickCancel = () => {
navigate('/libraries');
};
Expand Down Expand Up @@ -100,7 +103,12 @@ const CreateLibrary = () => {
<Form.Autosuggest
name="org"
isLoading={isOrganizationListLoading}
onChange={(event) => formikProps.setFieldValue('org', event.selectionId)}
onChange={(event) => formikProps.setFieldValue(
'org',
allowToCreateNewOrg
? (event.selectionId || event.userProvidedText)
: event.selectionId,
)}
placeholder={intl.formatMessage(messages.orgPlaceholder)}
>
{organizationListData ? organizationListData.map((org) => (
Expand Down
Loading