-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add isInstanceOfElement helper (#552)
- Loading branch information
1 parent
390013e
commit b4330c4
Showing
3 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import {isInstanceOfElement} from '../utils' | ||
import {setup} from './helpers/utils' | ||
|
||
// isInstanceOfElement can be removed once the peerDependency for @testing-library/dom is bumped to a version that includes https://github.com/testing-library/dom-testing-library/pull/885 | ||
describe('check element type per isInstanceOfElement', () => { | ||
let defaultViewDescriptor, spanDescriptor | ||
beforeAll(() => { | ||
defaultViewDescriptor = Object.getOwnPropertyDescriptor( | ||
Object.getPrototypeOf(global.document), | ||
'defaultView', | ||
) | ||
spanDescriptor = Object.getOwnPropertyDescriptor( | ||
global.window, | ||
'HTMLSpanElement', | ||
) | ||
}) | ||
afterEach(() => { | ||
Object.defineProperty( | ||
Object.getPrototypeOf(global.document), | ||
'defaultView', | ||
defaultViewDescriptor, | ||
) | ||
Object.defineProperty(global.window, 'HTMLSpanElement', spanDescriptor) | ||
}) | ||
|
||
test('check in regular jest environment', () => { | ||
const {element} = setup(`<span></span>`) | ||
|
||
expect(element.ownerDocument.defaultView).toEqual( | ||
expect.objectContaining({ | ||
HTMLSpanElement: expect.any(Function), | ||
}), | ||
) | ||
|
||
expect(isInstanceOfElement(element, 'HTMLSpanElement')).toBe(true) | ||
expect(isInstanceOfElement(element, 'HTMLDivElement')).toBe(false) | ||
}) | ||
|
||
test('check in detached document', () => { | ||
const {element} = setup(`<span></span>`) | ||
|
||
Object.defineProperty( | ||
Object.getPrototypeOf(element.ownerDocument), | ||
'defaultView', | ||
{value: null}, | ||
) | ||
|
||
expect(element.ownerDocument.defaultView).toBe(null) | ||
|
||
expect(isInstanceOfElement(element, 'HTMLSpanElement')).toBe(true) | ||
expect(isInstanceOfElement(element, 'HTMLDivElement')).toBe(false) | ||
}) | ||
|
||
test('check in environment not providing constructors on window', () => { | ||
const {element} = setup(`<span></span>`) | ||
|
||
delete global.window.HTMLSpanElement | ||
|
||
expect(element.ownerDocument.defaultView.HTMLSpanElement).toBe(undefined) | ||
|
||
expect(isInstanceOfElement(element, 'HTMLSpanElement')).toBe(true) | ||
expect(isInstanceOfElement(element, 'HTMLDivElement')).toBe(false) | ||
}) | ||
|
||
test('throw error if element is not created by HTML*Element constructor', () => { | ||
const doc = new Document() | ||
|
||
// constructor is global.Element | ||
const element = doc.createElement('span') | ||
|
||
expect(() => isInstanceOfElement(element, 'HTMLSpanElement')).toThrow() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters