-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(add-touch): begin implementation of touch helpers
- Loading branch information
1 parent
2662dd3
commit ee39595
Showing
2 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import {touchStart, andThen, setupAsync, jQuery as $} from '../../src' | ||
|
||
describe('Touch Events', () => { | ||
describeMouseEventHelper(touchStart, 'touchstart'); | ||
|
||
function describeMouseEventHelper(func, eventName, extraTests = ()=> {}) { | ||
describe(func.name, () => { | ||
setupAsync(); | ||
|
||
let elementToInteractWith; | ||
|
||
function attachElementToBody() { | ||
document.body.appendChild(elementToInteractWith); | ||
return elementToInteractWith; | ||
} | ||
|
||
beforeEach(() => { | ||
elementToInteractWith = document.createElement('div'); | ||
elementToInteractWith.className = 'element-to-interact-with'; | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(elementToInteractWith); | ||
}); | ||
|
||
it(`triggers ${eventName} event on selected element`, () => { | ||
attachElementToBody(); | ||
const spy = sinon.spy(); | ||
$(elementToInteractWith).on(eventName, spy); | ||
func('.element-to-interact-with'); | ||
andThen(() => { | ||
expect(spy).to.have.been.calledOnce(); | ||
}); | ||
}); | ||
|
||
it('waits until element shows up before trying to interact with it', () => { | ||
const spy = sinon.spy(); | ||
$(elementToInteractWith).on(eventName, spy); | ||
func('.element-to-interact-with'); | ||
andThen(() => { | ||
expect(spy).to.have.been.calledOnce(); | ||
}); | ||
|
||
setTimeout(attachElementToBody, 500); | ||
}); | ||
|
||
it('takes extra options as parameters', (done) => { | ||
const element = attachElementToBody(); | ||
$(element).on(eventName, e => { | ||
expect(e.touches[0].clientX).to.equal(1337); | ||
expect(e.touches[0].clientY).to.equal(1338); | ||
done(); | ||
}); | ||
func('.element-to-interact-with', {touches: [{ clientX: 1337, clientY: 1338 }]}); | ||
}); | ||
|
||
it('evaluates options lazily if passed as function', (done) => { | ||
let screenX = 42; | ||
|
||
andThen(() => { | ||
screenX = 1337; | ||
}); | ||
|
||
func('.element-to-interact-with', () => ({ screenX })); | ||
|
||
const element = attachElementToBody(); | ||
$(element).on(eventName, e => { | ||
expect(e.screenX).to.equal(1337); | ||
done(); | ||
}); | ||
}); | ||
|
||
extraTests(attachElementToBody); | ||
}); | ||
} | ||
}) |