|
| 1 | +import { mockCollectionHit } from '../../search-manager/data/api.mock'; |
| 2 | +import { |
| 3 | + initializeMocks, |
| 4 | + fireEvent, |
| 5 | + render, |
| 6 | + screen, |
| 7 | + waitFor, |
| 8 | +} from '../../testUtils'; |
| 9 | +import { mockContentLibrary } from '../data/api.mocks'; |
| 10 | +import * as api from '../data/api'; |
| 11 | +import CollectionInfoHeader from './CollectionInfoHeader'; |
| 12 | + |
| 13 | +describe('<CollectionInfoHeader />', () => { |
| 14 | + it('should render Collection info Header', async () => { |
| 15 | + initializeMocks(); |
| 16 | + const library = await mockContentLibrary(mockContentLibrary.libraryId); |
| 17 | + render(<CollectionInfoHeader library={library} collection={mockCollectionHit} />); |
| 18 | + |
| 19 | + expect(screen.getByText(mockCollectionHit.displayName)).toBeInTheDocument(); |
| 20 | + expect(screen.getByRole('button', { name: /edit collection title/i })).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should not render edit title button without permission', async () => { |
| 24 | + const readOnlyLibrary = await mockContentLibrary(mockContentLibrary.libraryIdReadOnly); |
| 25 | + render(<CollectionInfoHeader library={readOnlyLibrary} collection={mockCollectionHit} />); |
| 26 | + expect(screen.queryByRole('button', { name: /edit collection title/i })).not.toBeInTheDocument(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should update collection title', async () => { |
| 30 | + const { axiosMock, mockShowToast } = initializeMocks(); |
| 31 | + const library = await mockContentLibrary(mockContentLibrary.libraryId); |
| 32 | + render(<CollectionInfoHeader library={library} collection={mockCollectionHit} />); |
| 33 | + const url = api.getLibraryCollectionApiUrl(library.id, mockCollectionHit.blockId); |
| 34 | + axiosMock.onPatch(url).reply(200); |
| 35 | + |
| 36 | + fireEvent.click(screen.getByRole('button', { name: /edit collection title/i })); |
| 37 | + |
| 38 | + const textBox = screen.getByRole('textbox', { name: /title input/i }); |
| 39 | + |
| 40 | + fireEvent.change(textBox, { target: { value: 'New Collection Title' } }); |
| 41 | + fireEvent.keyDown(textBox, { key: 'Enter', code: 'Enter', charCode: 13 }); |
| 42 | + |
| 43 | + await waitFor(() => { |
| 44 | + expect(axiosMock.history.patch[0].url).toEqual(url); |
| 45 | + expect(axiosMock.history.patch[0].data).toEqual(JSON.stringify({ title: 'New Collection Title' })); |
| 46 | + }); |
| 47 | + |
| 48 | + expect(textBox).not.toBeInTheDocument(); |
| 49 | + expect(mockShowToast).toHaveBeenCalledWith('Collection updated successfully.'); |
| 50 | + }); |
| 51 | +}); |
0 commit comments