Skip to content

Commit

Permalink
Add test for using function to select element
Browse files Browse the repository at this point in the history
Move dockblock

Fix import path for closest

Resolving merge conflict error

Fix linting errors
  • Loading branch information
timrourke committed Nov 3, 2017
1 parent aaa5c2b commit d91e2af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 63 deletions.
9 changes: 9 additions & 0 deletions src/shared/utils/closest/closest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ const matchFunction = Element.prototype.matches ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector;

/**
* Get the closest parent element of a given element that matches the given
* selector string or matching function
*
* @param {Element} element The child element to find a parent of
* @param {String|Function} selector The string or function to use to match
* the parent element
* @return {Element|null}
*/
export default function closest(element, value) {
if (!element) {
return null;
Expand Down
16 changes: 14 additions & 2 deletions src/shared/utils/closest/tests/closest.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {closest} from 'utils';

import {
createSandbox,
} from 'helper';

import closest from './../closest';

const sampleMarkup = `
<div class="tree">
<section class="branch">
Expand Down Expand Up @@ -48,6 +48,18 @@ describe('utils', () => {
expect(closest(element, '.leaf')).toBe(null);
});

test('should match element via callback', () => {
const element = sandbox.querySelector('.leaf');

function callback(currentElement) {
return currentElement
.classList
.contains('leaf');
}

expect(closest(element, callback)).toBe(element);
});

[
'.twig',
'ul',
Expand Down
61 changes: 0 additions & 61 deletions src/utils.js

This file was deleted.

0 comments on commit d91e2af

Please sign in to comment.