-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add tests for remaining actions #18
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,95 @@ | ||
import { lazyload } from './lazyload'; | ||
import { Action } from './types'; | ||
import * as assert from 'assert'; | ||
import * as sinon from 'sinon'; | ||
|
||
describe('lazyload', function() { | ||
let element: HTMLElement; | ||
let action: ReturnType<Action>; | ||
let intersectionObserverConstructorSpy: sinon.SinonSpy; | ||
const observeFake = sinon.fake(); | ||
const unobserveFake = sinon.fake(); | ||
|
||
before(function() { | ||
setupIntersectionObserverMock({ | ||
observe: observeFake, | ||
unobserve: unobserveFake | ||
}); | ||
intersectionObserverConstructorSpy = sinon.spy(global, 'IntersectionObserver'); | ||
}); | ||
|
||
beforeEach(function() { | ||
element = document.createElement('div'); | ||
document.body.appendChild(element); | ||
}); | ||
|
||
afterEach(function() { | ||
action.destroy!(); | ||
element.remove(); | ||
observeFake.resetHistory(); | ||
unobserveFake.resetHistory(); | ||
}); | ||
|
||
it('observes node', function() { | ||
action = lazyload(element, {}); | ||
assert.ok(intersectionObserverConstructorSpy.calledOnce); | ||
assert.ok(observeFake.calledOnce); | ||
}); | ||
|
||
it('sets attribute on intersection', function() { | ||
action = lazyload(element, { className: 'test'}); | ||
const intersectionCallback = intersectionObserverConstructorSpy.firstCall.firstArg; | ||
intersectionCallback([{ | ||
isIntersecting: true, | ||
target: element | ||
}]); | ||
|
||
assert.ok(unobserveFake.calledOnce); | ||
assert.strictEqual(element.className, 'test'); | ||
}); | ||
|
||
it('does not set attribute when no intersection', function() { | ||
action = lazyload(element, { className: 'test'}); | ||
const intersectionCallback = intersectionObserverConstructorSpy.firstCall.firstArg; | ||
intersectionCallback([{ | ||
isIntersecting: false, | ||
target: element | ||
}]); | ||
|
||
assert.ok(unobserveFake.notCalled); | ||
assert.strictEqual(element.className, ''); | ||
}); | ||
}); | ||
|
||
// from https://stackoverflow.com/a/58651649 | ||
function setupIntersectionObserverMock({ | ||
root = null, | ||
rootMargin = '', | ||
thresholds = [], | ||
disconnect = () => null, | ||
observe = () => null, | ||
takeRecords = () => [], | ||
unobserve = () => null | ||
} = {}): void { | ||
class MockIntersectionObserver implements IntersectionObserver { | ||
readonly root: Element | null = root; | ||
readonly rootMargin: string = rootMargin; | ||
readonly thresholds: ReadonlyArray<number> = thresholds; | ||
disconnect: () => void = disconnect; | ||
observe: (target: Element) => void = observe; | ||
takeRecords: () => IntersectionObserverEntry[] = takeRecords; | ||
unobserve: (target: Element) => void = unobserve; | ||
} | ||
|
||
Object.defineProperty(window, 'IntersectionObserver', { | ||
writable: true, | ||
configurable: true, | ||
value: MockIntersectionObserver | ||
}); | ||
|
||
Object.defineProperty(global, 'IntersectionObserver', { | ||
writable: true, | ||
configurable: true, | ||
value: MockIntersectionObserver | ||
}); | ||
} |
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,56 @@ | ||
import { longpress } from './longpress'; | ||
import { Action } from './types'; | ||
import * as assert from 'assert'; | ||
import * as sinon from 'sinon'; | ||
|
||
describe('longpress', function() { | ||
let element: HTMLElement; | ||
let cb = sinon.fake(); | ||
let action: ReturnType<Action>; | ||
let clock: sinon.SinonFakeTimers; | ||
|
||
before(function() { | ||
element = document.createElement('div'); | ||
element.addEventListener('longpress', cb); | ||
document.body.appendChild(element); | ||
clock = sinon.useFakeTimers(); | ||
}); | ||
|
||
after(function() { | ||
element.remove(); | ||
clock.restore(); | ||
}); | ||
|
||
afterEach(function() { | ||
action.destroy!(); | ||
cb.resetHistory(); | ||
}); | ||
|
||
it('dispatches longpress event when mousedown more than duration', function() { | ||
const duration = 10; | ||
action = longpress(element, duration); | ||
element.dispatchEvent(new window.MouseEvent('mousedown')); | ||
clock.tick(duration); | ||
element.dispatchEvent(new window.MouseEvent('mouseup')); | ||
assert.ok(cb.calledOnce); | ||
}); | ||
|
||
it('does not dispatch longpress event when mousedown less than duration', function() { | ||
action = longpress(element, 100); | ||
element.dispatchEvent(new window.MouseEvent('mousedown')); | ||
clock.tick(10); | ||
element.dispatchEvent(new window.MouseEvent('mouseup')); | ||
assert.ok(cb.notCalled); | ||
}); | ||
|
||
it('updates duration', function() { | ||
const newDuration = 10; | ||
action = longpress(element, 500); | ||
action.update!(newDuration); | ||
|
||
element.dispatchEvent(new window.MouseEvent('mousedown')); | ||
clock.tick(newDuration); | ||
element.dispatchEvent(new window.MouseEvent('mouseup')); | ||
assert.ok(cb.calledOnce); | ||
}); | ||
}); |
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,44 @@ | ||
import { pannable } from './pannable'; | ||
import { Action } from './types'; | ||
import * as assert from 'assert'; | ||
import * as sinon from 'sinon'; | ||
|
||
describe('pannable', function() { | ||
let element: HTMLElement; | ||
let action: ReturnType<Action>; | ||
|
||
before(function() { | ||
element = document.createElement('div'); | ||
document.body.appendChild(element); | ||
}); | ||
|
||
after(function() { | ||
element.remove(); | ||
}); | ||
|
||
afterEach(function() { | ||
action.destroy!(); | ||
}); | ||
|
||
it('dispatches pan events', function() { | ||
action = pannable(element); | ||
const panstartCb = sinon.spy(); | ||
const panmoveCb = sinon.spy(); | ||
const panendCb = sinon.spy(); | ||
element.addEventListener('panstart', panstartCb); | ||
element.addEventListener('panmove', panmoveCb); | ||
element.addEventListener('panend', panendCb); | ||
|
||
element.dispatchEvent(new window.MouseEvent('mousedown', { clientX: 20, clientY: 30})); | ||
const panstartDetail = panstartCb.firstCall.firstArg.detail; | ||
assert.deepStrictEqual(panstartDetail, { x: 20, y: 30 }); | ||
|
||
window.dispatchEvent(new window.MouseEvent('mousemove', { clientX: 30, clientY: 50 })); | ||
const panmoveDetail = panmoveCb.firstCall.firstArg.detail; | ||
assert.deepStrictEqual(panmoveDetail, { x: 30, y: 50, dx: 10, dy: 20 }); | ||
|
||
window.dispatchEvent(new window.MouseEvent('mouseup', { clientX: 35, clientY: 55 })); | ||
const panendDetail = panendCb.firstCall.firstArg.detail; | ||
assert.deepStrictEqual(panendDetail, { x: 35, y: 55 }); | ||
}); | ||
}); |
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,56 @@ | ||
import { preventTabClose } from './preventTabClose'; | ||
import { Action } from './types'; | ||
import * as assert from 'assert'; | ||
import * as sinon from 'sinon'; | ||
|
||
describe('preventTabClose', function() { | ||
let element: HTMLElement; | ||
let action: ReturnType<Action>; | ||
|
||
before(function() { | ||
element = document.createElement('div'); | ||
document.body.appendChild(element); | ||
}); | ||
|
||
after(function() { | ||
element.remove(); | ||
}); | ||
|
||
afterEach(function() { | ||
action.destroy!(); | ||
}); | ||
|
||
it('cancels beforeunload event when enabled', function() { | ||
action = preventTabClose(element, true); | ||
const event = new window.Event('beforeunload'); | ||
const preventDefaultSpy = sinon.spy(event, 'preventDefault'); | ||
const returnValSpy = sinon.spy(event, 'returnValue', ['set']); | ||
window.dispatchEvent(event); | ||
|
||
assert.ok(preventDefaultSpy.calledOnce); | ||
assert.ok(returnValSpy.set.calledWith('')); | ||
}); | ||
|
||
it('does not cancel beforeunload event when disabled', function() { | ||
action = preventTabClose(element, false); | ||
const event = new window.Event('beforeunload'); | ||
const preventDefaultSpy = sinon.spy(event, 'preventDefault'); | ||
window.dispatchEvent(event); | ||
|
||
assert.ok(preventDefaultSpy.notCalled); | ||
}); | ||
|
||
it('updates enabled parameter', function() { | ||
action = preventTabClose(element, false); | ||
const event = new window.Event('beforeunload'); | ||
const preventDefaultSpy = sinon.spy(event, 'preventDefault'); | ||
window.dispatchEvent(event); | ||
|
||
assert.ok(preventDefaultSpy.notCalled); | ||
|
||
action.update!(true); | ||
window.dispatchEvent(event); | ||
|
||
assert.ok(preventDefaultSpy.called); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
type error
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.
Fixed in #20