Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
fix: Add better handlers for closing drawer
Browse files Browse the repository at this point in the history
Include tests for new functionality
  • Loading branch information
yusuf-musleh committed Dec 28, 2023
1 parent 39efd6e commit 89bf904
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/library-authoring/author-library/LibraryAuthoringPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -492,17 +492,29 @@ const ContentTagsDrawer = ({ openContentTagsDrawer, setOpenContentTagsDrawer })
}

useEffect(() => {
const handleClose = (event) => {
const handleCloseMessage = (event) => {
if (event.data === 'closeManageTagsDrawer') {
setOpenContentTagsDrawer('');

Check warning on line 497 in src/library-authoring/author-library/LibraryAuthoringPage.jsx

View check run for this annotation

Codecov / codecov/patch

src/library-authoring/author-library/LibraryAuthoringPage.jsx#L497

Added line #L497 was not covered by tests
}
};

// Add event listen to close drawer when close button is clicked from within the Iframe
window.addEventListener('message', handleClose);
const handleCloseEsc = (event) => {
if (event.key === 'Escape' || event.keyCode === 27) {
setOpenContentTagsDrawer('');
}
};

// Add event listener to close drawer when close button is clicked or ESC pressed
// from within the Iframe
window.addEventListener('message', handleCloseMessage);
// Add event listern to close the drawer when ESC pressed and focus outside iframe
// If ESC is pressed while the Iframe is in focus, it will send the close message
// to the parent window and it will be handled with the above event listener
window.addEventListener('keyup', handleCloseEsc);

return () => {
window.removeEventListener('message', handleClose);
window.removeEventListener('message', handleCloseMessage);
window.removeEventListener('keyup', handleCloseEsc);
};
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,33 @@ testSuite('<LibraryAuthoringPageContainer />', () => {
() => expect(updateLibrary.fn).toHaveBeenCalledWith({ data: { title: 'New title', libraryId: library.id } }),
);
});

it('Opens (and closes) block tags drawer', async () => {
const library = libraryFactory();
const block = blockFactory(undefined, { library });
await render(library, genState(library, [block]));
const moreActionsButton = screen.getByLabelText('More actions');
act(() => {
moreActionsButton.click();
});
const manageTagsAction = await screen.getByLabelText('Manage tags');
// Open the tags drawer
act(() => {
manageTagsAction.click();
});

const testExistingManageTagsIFrame = await screen.getByTitle('manage-tags-drawer');
expect(testExistingManageTagsIFrame).not.toBeNull();

// Close the tags drawer
fireEvent.keyUp(testExistingManageTagsIFrame, {
key: 'Escape',
code: 'Escape',
keyCode: 27,
charCode: 27,
});

const testMissingManageTagsIFrame = await screen.queryByTitle('manage-tags-drawer');
expect(testMissingManageTagsIFrame).toBeNull();
});
});

0 comments on commit 89bf904

Please sign in to comment.