| 1 |
var closest = require('../src/closest'); |
| 2 |
|
| 3 |
describe('closest', function() { |
| 4 |
before(function() { |
| 5 |
var html = '<div id="a">' + |
| 6 |
'<div id="b">' + |
| 7 |
'<div id="c"></div>' + |
| 8 |
'</div>' + |
| 9 |
'</div>'; |
| 10 |
|
| 11 |
document.body.innerHTML += html; |
| 12 |
|
| 13 |
global.a = document.querySelector('#a'); |
| 14 |
global.b = document.querySelector('#b'); |
| 15 |
global.c = document.querySelector('#c'); |
| 16 |
}); |
| 17 |
|
| 18 |
after(function() { |
| 19 |
document.body.innerHTML = ''; |
| 20 |
}); |
| 21 |
|
| 22 |
it('should return the closest parent based on the selector', function() { |
| 23 |
assert.ok(closest(global.c, '#b'), global.b); |
| 24 |
assert.ok(closest(global.c, '#a'), global.a); |
| 25 |
assert.ok(closest(global.b, '#a'), global.a); |
| 26 |
}); |
| 27 |
|
| 28 |
it('should return itself if the same selector is passed', function() { |
| 29 |
assert.ok(closest(document.body, 'body'), document.body); |
| 30 |
}); |
| 31 |
|
| 32 |
it('should not throw on elements without matches()', function() { |
| 33 |
var fakeElement = { |
| 34 |
nodeType: -1, // anything but DOCUMENT_NODE_TYPE |
| 35 |
parentNode: null, |
| 36 |
matches: undefined // undefined to emulate Elements without this function |
| 37 |
}; |
| 38 |
|
| 39 |
try { |
| 40 |
closest(fakeElement, '#a') |
| 41 |
} catch (err) { |
| 42 |
assert.fail(); |
| 43 |
} |
| 44 |
}); |
| 45 |
}); |