Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Last active June 9, 2020 21:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathansmith/428e2c8c9cbad88f0f17a49bae6206c8 to your computer and use it in GitHub Desktop.
Save nathansmith/428e2c8c9cbad88f0f17a49bae6206c8 to your computer and use it in GitHub Desktop.
Helper to mock DOM methods, for when an element might not exist.
/*
Helper to mock DOM methods, for
when an element might not exist.
*/
const getDomFallback = () => {
return {
// Props.
children: [],
className: '',
classList: {
contains: () => false,
},
id: '',
innerHTML: '',
name: '',
nextSibling: null,
previousSibling: null,
outerHTML: '',
tagName: '',
textContent: '',
// Methods.
addEventListener: () => undefined,
appendChild: () => Object.create(null),
blur: () => undefined,
click: () => undefined,
cloneNode: () => Object.create(null),
closest: () => null,
createElement: () => Object.create(null),
execCommand: () => undefined,
focus: () => undefined,
getAttribute: () => null,
hasAttribute: () => false,
insertAdjacentElement: () => Object.create(null),
insertBefore: () => Object.create(null),
matchMedia: () => ({
matches: false,
addListener: () => undefined,
removeListener: () => undefined,
}),
querySelector: () => null,
querySelectorAll: () => [],
removeAttribute: () => undefined,
removeChild: () => Object.create(null),
removeEventListener: () => undefined,
replaceChild: () => Object.create(null),
requestAnimationFrame: () => undefined,
setAttribute: () => undefined,
};
};
// Export.
export { getDomFallback };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment