-
Notifications
You must be signed in to change notification settings - Fork 2
Update closest() to accept a css selector #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
4826722
94ef0e1
8b44366
7c381d2
e0caed9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import domassist from '../domassist'; | ||
| import test from 'tape-rollup'; | ||
|
|
||
| function addNode(el, num) { | ||
|
||
| const node = document.createElement('div'); | ||
| node.innerText = num; | ||
| node.classList.add(`level-${num}`); | ||
| const children = el.children; | ||
| if (children.length) { | ||
| const child = domassist.findOne(`.level-${num - 1}`); | ||
| child.appendChild(node); | ||
| } else { | ||
| el.appendChild(node); | ||
| } | ||
| } | ||
|
|
||
| const teardown = (el) => { | ||
| while (el.firstChild) { | ||
| el.removeChild(el.firstChild); | ||
| } | ||
| }; | ||
|
|
||
| test('closest - element', assert => { | ||
| const el = domassist.findOne('#domassist'); | ||
| const levels = 4; | ||
| for (let i = 0; i < levels; i += 1) { | ||
| addNode(el, i + 1); | ||
| } | ||
| const startEl = domassist.findOne(`.level-${levels}`); | ||
| let count = levels - 1; | ||
| while (count) { | ||
| assert.ok(domassist.closest(startEl, `.level-${count}`), `Should find element with class of level-${count}`); | ||
| --count; | ||
| } | ||
| teardown(el); | ||
| assert.end(); | ||
| }); | ||
|
|
||
| test('closest - selector', assert => { | ||
| const el = domassist.findOne('#domassist'); | ||
| const levels = 4; | ||
| for (let i = 0; i < levels; i += 1) { | ||
| addNode(el, i + 1); | ||
| } | ||
| let count = levels - 1; | ||
| while (count) { | ||
| assert.ok(domassist.closest(`.level-${levels}`, `.level-${count}`), `Should find element with class of level-${count}`); | ||
| --count; | ||
| } | ||
| teardown(el); | ||
| assert.end(); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import domassist from '../domassist'; | ||
| import test from 'tape-rollup'; | ||
|
|
||
| const page = window.phantom.page; | ||
|
|
||
| const teardown = (el) => { | ||
| while (el.firstChild) { | ||
| el.removeChild(el.firstChild); | ||
| } | ||
| }; | ||
|
|
||
| test('Events - once with Element', assert => { | ||
| const el = domassist.findOne('#domassist'); | ||
|
|
||
| el.innerHTML = ` | ||
| <a href="#">Click</a> | ||
| `; | ||
|
|
||
| const link = domassist.findOne('a', el); | ||
| const pos = link.getBoundingClientRect(); | ||
|
|
||
| let clicks = 0; | ||
|
|
||
| domassist.once(link, 'click', e => { | ||
| clicks++; | ||
| }); | ||
|
|
||
| page.sendEvent('click', pos.left + pos.width / 2, pos.top + pos.height / 2); | ||
| page.sendEvent('click', pos.left + pos.width / 2, pos.top + pos.height / 2); | ||
| page.sendEvent('click', pos.left + pos.width / 2, pos.top + pos.height / 2); | ||
|
|
||
| setTimeout(() => { | ||
| assert.equal(clicks, 1, 'Element only fired once'); | ||
| teardown(el); | ||
| assert.end(); | ||
| }, 500); | ||
| }); | ||
|
|
||
| test('Events - once with selector', assert => { | ||
| const el = domassist.findOne('#domassist'); | ||
|
|
||
| el.innerHTML = ` | ||
| <a href="#">Click</a> | ||
| `; | ||
| const link = domassist.findOne('a', el); | ||
| const pos = link.getBoundingClientRect(); | ||
|
|
||
| let clicks = 0; | ||
|
|
||
| domassist.once('a', 'click', e => { | ||
| clicks++; | ||
| }); | ||
|
|
||
| page.sendEvent('click', pos.left + pos.width / 2, pos.top + pos.height / 2); | ||
| page.sendEvent('click', pos.left + pos.width / 2, pos.top + pos.height / 2); | ||
| page.sendEvent('click', pos.left + pos.width / 2, pos.top + pos.height / 2); | ||
|
|
||
| setTimeout(() => { | ||
| assert.equal(clicks, 1, 'Selector only fired once'); | ||
| teardown(el); | ||
| assert.end(); | ||
| }, 500); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR but as per docs, the capture should be passed on to
offtoo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
off()doesn't receive a capture option sinceremoveEventListener()doesn't have one. The docs have thisoff(selector, event)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does, according to MDN. Is there something I'm missing?