closest.js 1.3 KB

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