|
3 | 3 | getWindowFromNode, |
4 | 4 | checkContainerType, |
5 | 5 | runWithRealTimers, |
| 6 | + isInstanceOfElement, |
6 | 7 | } from '../helpers' |
| 8 | +import {render} from './helpers/test-utils' |
7 | 9 |
|
8 | 10 | const globalObj = typeof window === 'undefined' ? global : window |
9 | 11 |
|
@@ -53,6 +55,90 @@ describe('query container validation throws when validation fails', () => { |
53 | 55 | }) |
54 | 56 | }) |
55 | 57 |
|
| 58 | +describe('check element type per isInstanceOfElement', () => { |
| 59 | + let defaultViewDescriptor, spanDescriptor |
| 60 | + beforeAll(() => { |
| 61 | + defaultViewDescriptor = Object.getOwnPropertyDescriptor( |
| 62 | + Object.getPrototypeOf(global.document), |
| 63 | + 'defaultView', |
| 64 | + ) |
| 65 | + spanDescriptor = Object.getOwnPropertyDescriptor( |
| 66 | + global.window, |
| 67 | + 'HTMLSpanElement', |
| 68 | + ) |
| 69 | + }) |
| 70 | + afterEach(() => { |
| 71 | + Object.defineProperty( |
| 72 | + Object.getPrototypeOf(global.document), |
| 73 | + 'defaultView', |
| 74 | + defaultViewDescriptor, |
| 75 | + ) |
| 76 | + Object.defineProperty(global.window, 'HTMLSpanElement', spanDescriptor) |
| 77 | + }) |
| 78 | + |
| 79 | + test('check in regular jest environment', () => { |
| 80 | + const {container} = render(`<span></span>`) |
| 81 | + |
| 82 | + expect(container.firstChild.ownerDocument.defaultView).toEqual( |
| 83 | + expect.objectContaining({ |
| 84 | + HTMLSpanElement: expect.any(Function), |
| 85 | + }), |
| 86 | + ) |
| 87 | + |
| 88 | + expect(isInstanceOfElement(container.firstChild, 'HTMLSpanElement')).toBe( |
| 89 | + true, |
| 90 | + ) |
| 91 | + expect(isInstanceOfElement(container.firstChild, 'HTMLDivElement')).toBe( |
| 92 | + false, |
| 93 | + ) |
| 94 | + }) |
| 95 | + |
| 96 | + test('check in detached document', () => { |
| 97 | + const {container} = render(`<span></span>`) |
| 98 | + |
| 99 | + Object.defineProperty( |
| 100 | + Object.getPrototypeOf(container.ownerDocument), |
| 101 | + 'defaultView', |
| 102 | + {value: null}, |
| 103 | + ) |
| 104 | + |
| 105 | + expect(container.firstChild.ownerDocument.defaultView).toBe(null) |
| 106 | + |
| 107 | + expect(isInstanceOfElement(container.firstChild, 'HTMLSpanElement')).toBe( |
| 108 | + true, |
| 109 | + ) |
| 110 | + expect(isInstanceOfElement(container.firstChild, 'HTMLDivElement')).toBe( |
| 111 | + false, |
| 112 | + ) |
| 113 | + }) |
| 114 | + |
| 115 | + test('check in environment not providing constructors on window', () => { |
| 116 | + const {container} = render(`<span></span>`) |
| 117 | + |
| 118 | + delete global.window.HTMLSpanElement |
| 119 | + |
| 120 | + expect(container.firstChild.ownerDocument.defaultView.HTMLSpanElement).toBe( |
| 121 | + undefined, |
| 122 | + ) |
| 123 | + |
| 124 | + expect(isInstanceOfElement(container.firstChild, 'HTMLSpanElement')).toBe( |
| 125 | + true, |
| 126 | + ) |
| 127 | + expect(isInstanceOfElement(container.firstChild, 'HTMLDivElement')).toBe( |
| 128 | + false, |
| 129 | + ) |
| 130 | + }) |
| 131 | + |
| 132 | + test('throw error if element is not created by HTML*Element constructor', () => { |
| 133 | + const doc = new Document() |
| 134 | + |
| 135 | + // constructor is global.Element |
| 136 | + const element = doc.createElement('span') |
| 137 | + |
| 138 | + expect(() => isInstanceOfElement(element, 'HTMLSpanElement')).toThrow() |
| 139 | + }) |
| 140 | +}) |
| 141 | + |
56 | 142 | test('should always use realTimers before using callback when timers are faked with useFakeTimers', () => { |
57 | 143 | const originalSetTimeout = globalObj.setTimeout |
58 | 144 |
|
|
0 commit comments