|
| 1 | +import { |
| 2 | + fireEvent, |
| 3 | + initializeMocks, |
| 4 | + render, |
| 5 | + screen, |
| 6 | + waitFor, |
| 7 | +} from '../../testUtils'; |
| 8 | +import { |
| 9 | + mockContentLibrary, |
| 10 | + mockLibraryBlockMetadata, |
| 11 | + mockSetXBlockOLX, |
| 12 | + mockXBlockAssets, |
| 13 | + mockXBlockOLX, |
| 14 | +} from '../data/api.mocks'; |
| 15 | +import { LibraryProvider } from '../common/context'; |
| 16 | +import { ComponentAdvancedInfo } from './ComponentAdvancedInfo'; |
| 17 | + |
| 18 | +mockContentLibrary.applyMock(); |
| 19 | +mockLibraryBlockMetadata.applyMock(); |
| 20 | +mockXBlockAssets.applyMock(); |
| 21 | +mockXBlockOLX.applyMock(); |
| 22 | +const setOLXspy = mockSetXBlockOLX.applyMock(); |
| 23 | + |
| 24 | +const withLibraryId = (libraryId: string = mockContentLibrary.libraryId) => ({ |
| 25 | + extraWrapper: ({ children }: { children: React.ReactNode }) => ( |
| 26 | + <LibraryProvider libraryId={libraryId}>{children}</LibraryProvider> |
| 27 | + ), |
| 28 | +}); |
| 29 | + |
| 30 | +describe('<ComponentAdvancedInfo />', () => { |
| 31 | + it('should display nothing when collapsed', async () => { |
| 32 | + initializeMocks(); |
| 33 | + render(<ComponentAdvancedInfo usageKey={mockLibraryBlockMetadata.usageKeyPublished} />, withLibraryId()); |
| 34 | + const expandButton = await screen.findByRole('button', { name: /Advanced details/ }); |
| 35 | + expect(expandButton).toBeInTheDocument(); |
| 36 | + expect(screen.queryByText(mockLibraryBlockMetadata.usageKeyPublished)).not.toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should display the usage key of the block (when expanded)', async () => { |
| 40 | + initializeMocks(); |
| 41 | + render(<ComponentAdvancedInfo usageKey={mockLibraryBlockMetadata.usageKeyPublished} />, withLibraryId()); |
| 42 | + const expandButton = await screen.findByRole('button', { name: /Advanced details/ }); |
| 43 | + fireEvent.click(expandButton); |
| 44 | + expect(await screen.findByText(mockLibraryBlockMetadata.usageKeyPublished)).toBeInTheDocument(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should display the static assets of the block (when expanded)', async () => { |
| 48 | + initializeMocks(); |
| 49 | + render(<ComponentAdvancedInfo usageKey={mockLibraryBlockMetadata.usageKeyPublished} />, withLibraryId()); |
| 50 | + const expandButton = await screen.findByRole('button', { name: /Advanced details/ }); |
| 51 | + fireEvent.click(expandButton); |
| 52 | + expect(await screen.findByText(/static\/image1\.png/)).toBeInTheDocument(); |
| 53 | + expect(await screen.findByText(/\(12M\)/)).toBeInTheDocument(); // size of the above file |
| 54 | + expect(await screen.findByText(/static\/data\.csv/)).toBeInTheDocument(); |
| 55 | + expect(await screen.findByText(/\(8K\)/)).toBeInTheDocument(); // size of the above file |
| 56 | + }); |
| 57 | + |
| 58 | + it('should display the OLX source of the block (when expanded)', async () => { |
| 59 | + initializeMocks(); |
| 60 | + render(<ComponentAdvancedInfo usageKey={mockXBlockOLX.usageKeyHtml} />, withLibraryId()); |
| 61 | + const expandButton = await screen.findByRole('button', { name: /Advanced details/ }); |
| 62 | + fireEvent.click(expandButton); |
| 63 | + // Because of syntax highlighting, the OLX will be borken up by many different tags so we need to search for |
| 64 | + // just a substring: |
| 65 | + const olxPart = /This is a text component which uses/; |
| 66 | + expect(await screen.findByText(olxPart)).toBeInTheDocument(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('does not display "Edit OLX" button when the library is read-only', async () => { |
| 70 | + initializeMocks(); |
| 71 | + render( |
| 72 | + <ComponentAdvancedInfo usageKey={mockXBlockOLX.usageKeyHtml} />, |
| 73 | + withLibraryId(mockContentLibrary.libraryIdReadOnly), |
| 74 | + ); |
| 75 | + const expandButton = await screen.findByRole('button', { name: /Advanced details/ }); |
| 76 | + fireEvent.click(expandButton); |
| 77 | + expect(screen.queryByRole('button', { name: /Edit OLX/ })).not.toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('can edit the OLX', async () => { |
| 81 | + initializeMocks(); |
| 82 | + render(<ComponentAdvancedInfo usageKey={mockLibraryBlockMetadata.usageKeyPublished} />, withLibraryId()); |
| 83 | + const expandButton = await screen.findByRole('button', { name: /Advanced details/ }); |
| 84 | + fireEvent.click(expandButton); |
| 85 | + const editButton = await screen.findByRole('button', { name: /Edit OLX/ }); |
| 86 | + fireEvent.click(editButton); |
| 87 | + |
| 88 | + expect(setOLXspy).not.toHaveBeenCalled(); |
| 89 | + |
| 90 | + const saveButton = await screen.findByRole('button', { name: /Save/ }); |
| 91 | + fireEvent.click(saveButton); |
| 92 | + |
| 93 | + await waitFor(() => expect(setOLXspy).toHaveBeenCalled()); |
| 94 | + }); |
| 95 | + |
| 96 | + it('displays an error if editing the OLX failed', async () => { |
| 97 | + initializeMocks(); |
| 98 | + |
| 99 | + setOLXspy.mockImplementation(async () => { |
| 100 | + throw new Error('Example error - setting OLX failed'); |
| 101 | + }); |
| 102 | + |
| 103 | + render(<ComponentAdvancedInfo usageKey={mockLibraryBlockMetadata.usageKeyPublished} />, withLibraryId()); |
| 104 | + const expandButton = await screen.findByRole('button', { name: /Advanced details/ }); |
| 105 | + fireEvent.click(expandButton); |
| 106 | + const editButton = await screen.findByRole('button', { name: /Edit OLX/ }); |
| 107 | + fireEvent.click(editButton); |
| 108 | + const saveButton = await screen.findByRole('button', { name: /Save/ }); |
| 109 | + fireEvent.click(saveButton); |
| 110 | + |
| 111 | + expect(await screen.findByText(/An error occurred and the OLX could not be saved./)).toBeInTheDocument(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments