Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uncaught DOMException error #1752

Merged
merged 3 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/view/use-announcer/use-announcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export default function useAnnouncer(contextId: ContextId): Announce {
// unmounting after a timeout to let any announcements
// during a mount be published
setTimeout(function remove() {
// not clearing the ref as it might have been set by a new effect
getBodyElement().removeChild(el);

// checking if element exists before remove to prevent errors
if (getBodyElement().contains(el)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked - this is all good for ie11

https://developer.mozilla.org/en-US/docs/Web/API/Node/contains

// not clearing the ref as it might have been set by a new effect
getBodyElement().removeChild(el);
}
// if el was the current ref - clear it so that
// we can get a warning if announce is called
if (el === ref.current) {
Expand Down
20 changes: 20 additions & 0 deletions test/unit/view/announcer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ it('should remove the element when unmounting after a timeout', () => {
expect(getElement('5')).not.toBeTruthy();
});

it('should not remove the element when unmounting after a timeout if the element does not exist', () => {
const { unmount } = render(
<WithAnnouncer contextId="5">{getMock()}</WithAnnouncer>,
);

const doc = new DOMParser().parseFromString(
'<!doctype html><title>wat</title>',
'text/html',
);
// $FlowFixMe
document.write(doc);
unmount();
expect(() => jest.runOnlyPendingTimers()).not.toThrow(
// $FlowFixMe
new DOMException(
"Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.",
),
);
});

it('should set the text content of the announcement element', () => {
// arrange
const mock = getMock();
Expand Down