|
1 | 1 | import {
|
2 | 2 | deprecate,
|
3 | 3 | checkHtmlElement,
|
| 4 | + checkNode, |
4 | 5 | HtmlElementTypeError,
|
| 6 | + NodeTypeError, |
5 | 7 | toSentence,
|
6 | 8 | } from '../utils'
|
7 | 9 | import document from './helpers/document'
|
@@ -95,6 +97,94 @@ describe('checkHtmlElement', () => {
|
95 | 97 | })
|
96 | 98 | })
|
97 | 99 |
|
| 100 | +describe('checkNode', () => { |
| 101 | + let assertionContext |
| 102 | + beforeAll(() => { |
| 103 | + expect.extend({ |
| 104 | + fakeMatcher() { |
| 105 | + assertionContext = this |
| 106 | + |
| 107 | + return {pass: true} |
| 108 | + }, |
| 109 | + }) |
| 110 | + |
| 111 | + expect(true).fakeMatcher(true) |
| 112 | + }) |
| 113 | + it('does not throw an error for correct html element', () => { |
| 114 | + expect(() => { |
| 115 | + const element = document.createElement('p') |
| 116 | + checkNode(element, () => {}, assertionContext) |
| 117 | + }).not.toThrow() |
| 118 | + }) |
| 119 | + |
| 120 | + it('does not throw an error for correct svg element', () => { |
| 121 | + expect(() => { |
| 122 | + const element = document.createElementNS( |
| 123 | + 'http://www.w3.org/2000/svg', |
| 124 | + 'rect', |
| 125 | + ) |
| 126 | + checkNode(element, () => {}, assertionContext) |
| 127 | + }).not.toThrow() |
| 128 | + }) |
| 129 | + |
| 130 | + it('does not throw an error for Document fragments', () => { |
| 131 | + expect(() => { |
| 132 | + const fragment = document.createDocumentFragment() |
| 133 | + checkNode(fragment, () => {}, assertionContext) |
| 134 | + }).not.toThrow() |
| 135 | + }) |
| 136 | + |
| 137 | + it('does not throw an error for text nodes', () => { |
| 138 | + expect(() => { |
| 139 | + const text = document.createTextNode('foo') |
| 140 | + checkNode(text, () => {}, assertionContext) |
| 141 | + }).not.toThrow() |
| 142 | + }) |
| 143 | + |
| 144 | + it('does not throw for body', () => { |
| 145 | + expect(() => { |
| 146 | + checkNode(document.body, () => {}, assertionContext) |
| 147 | + }).not.toThrow() |
| 148 | + }) |
| 149 | + |
| 150 | + it('throws for undefined', () => { |
| 151 | + expect(() => { |
| 152 | + checkNode(undefined, () => {}, assertionContext) |
| 153 | + }).toThrow(NodeTypeError) |
| 154 | + }) |
| 155 | + |
| 156 | + it('throws for document', () => { |
| 157 | + expect(() => { |
| 158 | + checkNode(document, () => {}, assertionContext) |
| 159 | + }).toThrow(NodeTypeError) |
| 160 | + }) |
| 161 | + |
| 162 | + it('throws for function', () => { |
| 163 | + expect(() => { |
| 164 | + checkNode( |
| 165 | + () => {}, |
| 166 | + () => {}, |
| 167 | + assertionContext, |
| 168 | + ) |
| 169 | + }).toThrow(NodeTypeError) |
| 170 | + }) |
| 171 | + |
| 172 | + it('throws for almost element-like objects', () => { |
| 173 | + class FakeObject {} |
| 174 | + expect(() => { |
| 175 | + checkNode( |
| 176 | + { |
| 177 | + ownerDocument: { |
| 178 | + defaultView: {Node: FakeObject, SVGElement: FakeObject}, |
| 179 | + }, |
| 180 | + }, |
| 181 | + () => {}, |
| 182 | + assertionContext, |
| 183 | + ) |
| 184 | + }).toThrow(NodeTypeError) |
| 185 | + }) |
| 186 | +}) |
| 187 | + |
98 | 188 | describe('toSentence', () => {
|
99 | 189 | it('turns array into string of comma separated list with default last word connector', () => {
|
100 | 190 | expect(toSentence(['one', 'two', 'three'])).toBe('one, two and three')
|
|
0 commit comments