Skip to content

Commit 72f5694

Browse files
committed
fixup! feat: improve collection sidebar
1 parent 57e5657 commit 72f5694

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

src/library-authoring/collections/CollectionDetails.test.tsx

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,30 @@ import { mockContentSearchConfig, mockSearchResult } from '../../search-manager/
77
import { type CollectionHit, formatSearchHit } from '../../search-manager/data/api';
88
import {
99
initializeMocks,
10+
fireEvent,
1011
render,
1112
screen,
1213
waitFor,
1314
within,
1415
} from '../../testUtils';
1516
import mockResult from '../__mocks__/collection-search.json';
17+
import * as api from '../data/api';
1618
import { mockContentLibrary } from '../data/api.mocks';
1719
import CollectionDetails from './CollectionDetails';
1820

1921
const searchEndpoint = 'http://mock.meilisearch.local/multi-search';
2022

2123
let axiosMock: MockAdapter;
22-
// let mockShowToast: (message: string) => void;
24+
let mockShowToast: (message: string) => void;
2325

2426
mockContentSearchConfig.applyMock();
27+
const library = mockContentLibrary.libraryData;
2528

2629
describe('<CollectionDetails />', () => {
2730
beforeEach(() => {
2831
const mocks = initializeMocks();
2932
axiosMock = mocks.axiosMock;
30-
// mockShowToast = mocks.mockShowToast;
33+
mockShowToast = mocks.mockShowToast;
3134
});
3235

3336
afterEach(() => {
@@ -38,7 +41,6 @@ describe('<CollectionDetails />', () => {
3841

3942
const renderCollectionDetails = async () => {
4043
const collectionData: CollectionHit = formatSearchHit(mockResult.results[2].hits[0]) as CollectionHit;
41-
const library = mockContentLibrary.libraryData;
4244

4345
render((
4446
<SearchContextProvider>
@@ -49,7 +51,7 @@ describe('<CollectionDetails />', () => {
4951
await waitFor(() => { expect(fetchMock).toHaveFetchedTimes(1, searchEndpoint, 'post'); });
5052
};
5153

52-
it('should render Collection Detail', async () => {
54+
it('should render Collection Details', async () => {
5355
mockSearchResult(mockResult);
5456
await renderCollectionDetails();
5557

@@ -66,6 +68,76 @@ describe('<CollectionDetails />', () => {
6668
expect(screen.getByText('September 19, 2024')).toBeInTheDocument();
6769
});
6870

71+
it('should allow modifying the description', async () => {
72+
mockSearchResult(mockResult);
73+
await renderCollectionDetails();
74+
75+
const {
76+
description: originalDescription,
77+
block_id: blockId,
78+
context_key: contextKey,
79+
} = mockResult.results[2].hits[0];
80+
81+
expect(screen.getByText(originalDescription)).toBeInTheDocument();
82+
83+
const url = api.getLibraryCollectionApiUrl(contextKey, blockId);
84+
axiosMock.onPatch(url).reply(200);
85+
86+
const textArea = screen.getByRole('textbox');
87+
88+
// Change the description to the same value
89+
fireEvent.focus(textArea);
90+
fireEvent.change(textArea, { target: { value: originalDescription } });
91+
fireEvent.blur(textArea);
92+
93+
await waitFor(() => {
94+
expect(axiosMock.history.patch).toHaveLength(0);
95+
expect(mockShowToast).not.toHaveBeenCalled();
96+
});
97+
98+
// Change the description to a new value
99+
fireEvent.focus(textArea);
100+
fireEvent.change(textArea, { target: { value: 'New description' } });
101+
fireEvent.blur(textArea);
102+
103+
await waitFor(() => {
104+
expect(axiosMock.history.patch).toHaveLength(1);
105+
expect(axiosMock.history.patch[0].url).toEqual(url);
106+
expect(axiosMock.history.patch[0].data).toEqual(JSON.stringify({ description: 'New description' }));
107+
expect(mockShowToast).toHaveBeenCalledWith('Collection updated successfully.');
108+
});
109+
});
110+
111+
it('should show error while modifing the description', async () => {
112+
mockSearchResult(mockResult);
113+
await renderCollectionDetails();
114+
115+
const {
116+
description: originalDescription,
117+
block_id: blockId,
118+
context_key: contextKey,
119+
} = mockResult.results[2].hits[0];
120+
121+
expect(screen.getByText(originalDescription)).toBeInTheDocument();
122+
123+
const url = api.getLibraryCollectionApiUrl(contextKey, blockId);
124+
axiosMock.onPatch(url).reply(500);
125+
126+
const textArea = screen.getByRole('textbox');
127+
128+
// Change the description to a new value
129+
fireEvent.focus(textArea);
130+
fireEvent.change(textArea, { target: { value: 'New description' } });
131+
fireEvent.blur(textArea);
132+
133+
await waitFor(() => {
134+
expect(axiosMock.history.patch).toHaveLength(1);
135+
expect(axiosMock.history.patch[0].url).toEqual(url);
136+
expect(axiosMock.history.patch[0].data).toEqual(JSON.stringify({ description: 'New description' }));
137+
expect(mockShowToast).toHaveBeenCalledWith('Failed to update collection.');
138+
});
139+
});
140+
69141
it('should render Collection stats', async () => {
70142
mockSearchResult(mockResult);
71143
await renderCollectionDetails();

src/library-authoring/collections/CollectionDetails.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const CollectionDetails = ({ library, collection }: CollectionDetailsProps) => {
9393

9494
const [description, setDescription] = useState(collection.description);
9595

96-
const updateMutation = useUpdateCollection(library.id, collection.blockId);
96+
const updateMutation = useUpdateCollection(collection.contextKey, collection.blockId);
9797

9898
// istanbul ignore if: this should never happen
9999
if (!collection) {

0 commit comments

Comments
 (0)