Skip to content

Commit 9690499

Browse files
committed
test: improve CollectionInfoHeader tests
1 parent 7786528 commit 9690499

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type MockAdapter from 'axios-mock-adapter';
2+
import userEvent from '@testing-library/user-event'
23

34
import { mockCollectionHit } from '../../search-manager/data/api.mock';
45
import {
@@ -51,8 +52,8 @@ describe('<CollectionInfoHeader />', () => {
5152

5253
const textBox = screen.getByRole('textbox', { name: /title input/i });
5354

54-
fireEvent.change(textBox, { target: { value: 'New Collection Title' } });
55-
fireEvent.keyDown(textBox, { key: 'Enter', code: 'Enter', charCode: 13 });
55+
userEvent.clear(textBox);
56+
userEvent.type(textBox, 'New Collection Title{enter}');
5657

5758
await waitFor(() => {
5859
expect(axiosMock.history.patch[0].url).toEqual(url);
@@ -63,6 +64,25 @@ describe('<CollectionInfoHeader />', () => {
6364
expect(mockShowToast).toHaveBeenCalledWith('Collection updated successfully.');
6465
});
6566

67+
it('should not update collection title if title is the same', async () => {
68+
const library = await mockContentLibrary(mockContentLibrary.libraryId);
69+
render(<CollectionInfoHeader library={library} collection={mockCollectionHit} />);
70+
const url = api.getLibraryCollectionApiUrl(library.id, mockCollectionHit.blockId);
71+
axiosMock.onPatch(url).reply(200);
72+
73+
fireEvent.click(screen.getByRole('button', { name: /edit collection title/i }));
74+
75+
const textBox = screen.getByRole('textbox', { name: /title input/i });
76+
77+
userEvent.clear(textBox);
78+
userEvent.type(textBox, mockCollectionHit.displayName);
79+
userEvent.type(textBox, '{enter}');
80+
81+
await waitFor(() => expect(axiosMock.history.patch.length).toEqual(0));
82+
83+
expect(textBox).not.toBeInTheDocument();
84+
});
85+
6686
it('should close edit collection title on press Escape', async () => {
6787
const library = await mockContentLibrary(mockContentLibrary.libraryId);
6888
render(<CollectionInfoHeader library={library} collection={mockCollectionHit} />);
@@ -73,8 +93,8 @@ describe('<CollectionInfoHeader />', () => {
7393

7494
const textBox = screen.getByRole('textbox', { name: /title input/i });
7595

76-
fireEvent.change(textBox, { target: { value: 'New Collection Title' } });
77-
fireEvent.keyDown(textBox, { key: 'Escape', code: 'Escape', charCode: 27 });
96+
userEvent.clear(textBox);
97+
userEvent.type(textBox, 'New Collection Title{esc}');
7898

7999
await waitFor(() => expect(axiosMock.history.patch.length).toEqual(0));
80100

@@ -91,8 +111,8 @@ describe('<CollectionInfoHeader />', () => {
91111

92112
const textBox = screen.getByRole('textbox', { name: /title input/i });
93113

94-
fireEvent.change(textBox, { target: { value: 'New Collection Title' } });
95-
fireEvent.keyDown(textBox, { key: 'Enter', code: 'Enter', charCode: 13 });
114+
userEvent.clear(textBox);
115+
userEvent.type(textBox, 'New Collection Title{enter}');
96116

97117
await waitFor(() => {
98118
expect(axiosMock.history.patch[0].url).toEqual(url);

src/library-authoring/collections/CollectionInfoHeader.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface CollectionInfoHeaderProps {
1919
collection: CollectionHit;
2020
}
2121

22-
const CollectionInfoHeader = ({ library, collection } : CollectionInfoHeaderProps) => {
22+
const CollectionInfoHeader = ({ library, collection }: CollectionInfoHeaderProps) => {
2323
const intl = useIntl();
2424
const [inputIsActive, setIsActive] = useState(false);
2525

@@ -29,7 +29,7 @@ const CollectionInfoHeader = ({ library, collection } : CollectionInfoHeaderProp
2929
const handleSaveDisplayName = useCallback(
3030
(event) => {
3131
const newTitle = event.target.value;
32-
if (newTitle && newTitle !== collection?.displayName) {
32+
if (newTitle && newTitle !== collection.displayName) {
3333
updateMutation.mutateAsync({
3434
title: newTitle,
3535
}).then(() => {
@@ -68,15 +68,15 @@ const CollectionInfoHeader = ({ library, collection } : CollectionInfoHeaderProp
6868
id="title"
6969
type="text"
7070
aria-label="Title input"
71-
defaultValue={collection?.displayName}
71+
defaultValue={collection.displayName}
7272
onBlur={handleSaveDisplayName}
7373
onKeyDown={handleOnKeyDown}
7474
/>
7575
)
7676
: (
7777
<>
7878
<span className="font-weight-bold m-1.5">
79-
{collection?.displayName}
79+
{collection.displayName}
8080
</span>
8181
{library.canEditLibrary && (
8282
<IconButton

0 commit comments

Comments
 (0)