|
1 | 1 | import * as Util from '../../../src/util/index'
|
2 | 2 |
|
3 | 3 | /** Test helpers */
|
4 |
| -import { getFixture, clearFixture } from '../../helpers/fixture' |
| 4 | +import { clearFixture, getFixture } from '../../helpers/fixture' |
5 | 5 |
|
6 | 6 | describe('Util', () => {
|
7 | 7 | let fixtureEl
|
@@ -171,24 +171,58 @@ describe('Util', () => {
|
171 | 171 | })
|
172 | 172 |
|
173 | 173 | describe('isElement', () => {
|
174 |
| - it('should detect if the parameter is an element or not', () => { |
175 |
| - fixtureEl.innerHTML = '<div></div>' |
| 174 | + it('should detect if the parameter is an element or not and return Boolean', () => { |
| 175 | + fixtureEl.innerHTML = |
| 176 | + [ |
| 177 | + '<div id="foo" class="test"></div>', |
| 178 | + '<div id="bar" class="test"></div>' |
| 179 | + ].join('') |
176 | 180 |
|
177 |
| - const el = document.querySelector('div') |
| 181 | + const el = fixtureEl.querySelector('#foo') |
178 | 182 |
|
179 |
| - expect(Util.isElement(el)).toEqual(el.nodeType) |
180 |
| - expect(Util.isElement({})).toEqual(undefined) |
| 183 | + expect(Util.isElement(el)).toEqual(true) |
| 184 | + expect(Util.isElement({})).toEqual(false) |
| 185 | + expect(Util.isElement(fixtureEl.querySelectorAll('.test'))).toEqual(false) |
181 | 186 | })
|
182 | 187 |
|
183 | 188 | it('should detect jQuery element', () => {
|
184 | 189 | fixtureEl.innerHTML = '<div></div>'
|
185 | 190 |
|
186 |
| - const el = document.querySelector('div') |
| 191 | + const el = fixtureEl.querySelector('div') |
187 | 192 | const fakejQuery = {
|
188 |
| - 0: el |
| 193 | + 0: el, |
| 194 | + jquery: 'foo' |
| 195 | + } |
| 196 | + |
| 197 | + expect(Util.isElement(fakejQuery)).toEqual(true) |
| 198 | + }) |
| 199 | + }) |
| 200 | + |
| 201 | + describe('getElement', () => { |
| 202 | + it('should try to parse element', () => { |
| 203 | + fixtureEl.innerHTML = |
| 204 | + [ |
| 205 | + '<div id="foo" class="test"></div>', |
| 206 | + '<div id="bar" class="test"></div>' |
| 207 | + ].join('') |
| 208 | + |
| 209 | + const el = fixtureEl.querySelector('div') |
| 210 | + |
| 211 | + expect(Util.getElement(el)).toEqual(el) |
| 212 | + expect(Util.getElement('#foo')).toEqual(el) |
| 213 | + expect(Util.getElement('#fail')).toBeNull() |
| 214 | + expect(Util.getElement({})).toBeNull() |
| 215 | + expect(Util.getElement([])).toBeNull() |
| 216 | + expect(Util.getElement()).toBeNull() |
| 217 | + expect(Util.getElement(null)).toBeNull() |
| 218 | + expect(Util.getElement(fixtureEl.querySelectorAll('.test'))).toBeNull() |
| 219 | + |
| 220 | + const fakejQueryObject = { |
| 221 | + 0: el, |
| 222 | + jquery: 'foo' |
189 | 223 | }
|
190 | 224 |
|
191 |
| - expect(Util.isElement(fakejQuery)).toEqual(el.nodeType) |
| 225 | + expect(Util.getElement(fakejQueryObject)).toEqual(el) |
192 | 226 | })
|
193 | 227 | })
|
194 | 228 |
|
|
0 commit comments