-
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.
- Loading branch information
Showing
14 changed files
with
768 additions
and
756 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { setupAsync, andThen } from '../../src/async'; | ||
import { | ||
click, | ||
mouseDown, | ||
mouseUp, | ||
mouseMove, | ||
} from '../../src/acceptance'; | ||
import $ from 'jquery'; | ||
|
||
describe('Mouse Events', () => { | ||
describeMouseEventHelper(click, 'click', (attachElementToBody) => { | ||
it('triggers mousedown and mouseup before click', () => { | ||
const $element = $(attachElementToBody()); | ||
const mouseDownListener = sinon.spy(); | ||
const mouseUpListener = sinon.spy(); | ||
const clickListener = sinon.spy(); | ||
|
||
$element.on('mousedown', mouseDownListener); | ||
$element.on('mouseup', mouseUpListener); | ||
$element.on('click', clickListener); | ||
|
||
click($element); | ||
|
||
andThen(() => { | ||
sinon.assert.callOrder(mouseDownListener, mouseUpListener, clickListener); | ||
}); | ||
}); | ||
}); | ||
describeMouseEventHelper(mouseDown, 'mousedown'); | ||
describeMouseEventHelper(mouseUp, 'mouseup'); | ||
describeMouseEventHelper(mouseMove, 'mousemove'); | ||
|
||
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.clientX).to.equal(1337); | ||
expect(e.clientY).to.equal(1338); | ||
done(); | ||
}); | ||
func('.element-to-interact-with', { 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); | ||
}); | ||
} | ||
}); |
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,58 @@ | ||
import $ from 'jquery'; | ||
import { andThen } from '../../src/async'; | ||
import { | ||
setupAndTeardownApp, | ||
scaleWindowWidth | ||
} from '../../src/acceptance'; | ||
|
||
describe('test div dimensions', () => { | ||
const createHistory = () => ({ | ||
push: () => { | ||
} | ||
}); | ||
const renderAppWithHistoryIntoElement = (history, element) => { | ||
}; | ||
setupAndTeardownApp(createHistory, renderAppWithHistoryIntoElement); | ||
|
||
it('starts at 1024 x 1024', () => { | ||
assertTestRootWidthAndHeight(1024, 1024); | ||
}); | ||
|
||
it('width can be scaled down with scaleWindowWidth', () => { | ||
scaleWindowWidth(0.5); | ||
|
||
assertTestRootWidthAndHeight(512, 1024); | ||
}); | ||
|
||
it('width can be scaled up with scaleWindowWidth', () => { | ||
scaleWindowWidth(2); | ||
|
||
assertTestRootWidthAndHeight(2048, 1024); | ||
}); | ||
|
||
it('width can be scaled multiple times with scaleWindowWidth', () => { | ||
scaleWindowWidth(0.5); | ||
scaleWindowWidth(2); | ||
|
||
assertTestRootWidthAndHeight(1024, 1024); | ||
}); | ||
|
||
it('triggers window resize event when calling scaleWindowWidth', () => { | ||
var eventListener = sinon.spy(); | ||
$(window).resize(eventListener); | ||
|
||
scaleWindowWidth(2); | ||
|
||
andThen(() => { | ||
expect(eventListener).to.have.been.calledOnce(); | ||
}) | ||
}); | ||
|
||
function assertTestRootWidthAndHeight(expectedWidth, expectedHeight) { | ||
andThen(() => { | ||
var testRoot = $('#test-root'); | ||
expect(testRoot.width()).to.equal(expectedWidth); | ||
expect(testRoot.height()).to.equal(expectedHeight); | ||
}); | ||
} | ||
}); |
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,28 @@ | ||
import { setupAsync } from '../../src/async'; | ||
import { visit, setupAndTeardownApp } from '../../src/acceptance'; | ||
|
||
describe('visit', () => { | ||
describe('when setupAndTeardownApp has not been called', () => { | ||
setupAsync(); | ||
it('throws', () => { | ||
expect(() => { | ||
visit('/some/path'); | ||
}).to.throw('You cannot use visit() unless you call setupAndTeardownApp() at the root of the appropriate describe()!') | ||
}); | ||
}); | ||
|
||
describe('when setupAndTeardownApp has been called', () => { | ||
const createHistory = () => ({ | ||
push: () => { | ||
} | ||
}); | ||
const renderAppWithHistoryIntoElement = (history, element) => {}; | ||
setupAndTeardownApp(createHistory, renderAppWithHistoryIntoElement); | ||
|
||
it('does not throw', () => { | ||
expect(() => { | ||
visit('/some/path'); | ||
}).to.not.throw(); | ||
}); | ||
}); | ||
}); |
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,38 @@ | ||
import { setupAsync, andThen } from '../../src/async'; | ||
import { waitUntilDisappears } from '../../src/acceptance'; | ||
|
||
describe('waitUntilDisappears', () => { | ||
setupAsync(); | ||
|
||
const label = document.createElement('label'); | ||
label.innerHTML = 'foobar'; | ||
|
||
it('resolve when the object disappears after having showed', (done) => { | ||
let callback = sinon.spy(); | ||
|
||
waitUntilDisappears('label:contains("foobar")'); | ||
|
||
andThen(callback); | ||
|
||
setTimeout(() => { | ||
expect(callback).to.not.have.been.called('Called before selector appeared!'); | ||
}, 150); | ||
|
||
setTimeout(() => { | ||
document.body.appendChild(label); | ||
}, 250); | ||
|
||
setTimeout(() => { | ||
expect(callback).to.not.have.been.called('Called before selector disappeared!'); | ||
}, 350); | ||
|
||
setTimeout(() => { | ||
document.body.removeChild(label); | ||
}, 450); | ||
|
||
setTimeout(() => { | ||
expect(callback).to.have.been.calledOnce('Not called when selector disappeared!'); | ||
done(); | ||
}, 550); | ||
}); | ||
}); |
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,26 @@ | ||
import { setupAsync, andThen } from '../../src/async'; | ||
import { waitUntilExists } from '../../src/acceptance'; | ||
|
||
describe('waitUntilExists', () => { | ||
setupAsync(); | ||
|
||
const label = document.createElement('label'); | ||
label.innerHTML = 'foobar'; | ||
|
||
afterEach(() => { | ||
document.body.removeChild(label); | ||
}); | ||
|
||
it('resolves with a jquery object of the selector when it exists', () => { | ||
setTimeout(() => { | ||
label.innerHTML = 'foobar'; | ||
document.body.appendChild(label); | ||
}, 1000); | ||
|
||
waitUntilExists('label:contains("foobar")'); | ||
|
||
andThen((label) => { | ||
expect(label.text()).to.equal('foobar'); | ||
}); | ||
}); | ||
}); |
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,31 @@ | ||
import { setupAsync, andThen } from '../../src/async'; | ||
|
||
describe('andThen', () => { | ||
describe('without having called setupAsync()', () => { | ||
it('throws informative error', () => { | ||
expect(() => { | ||
andThen(); | ||
}).to.throw('You cannot use andThen() unless you call setupAsync() at the root of the appropriate describe()!'); | ||
}); | ||
}); | ||
|
||
describe('after having called setupAsync', () => { | ||
setupAsync(); | ||
|
||
it('NOTE: can be nested, but the ordering might be unintuitive', (done) => { | ||
let sequence = '0'; | ||
andThen(() => { | ||
sequence += '1'; | ||
andThen(() => { | ||
sequence += '3'; | ||
expect(sequence).to.equal('0123'); | ||
done(); | ||
}); | ||
}); | ||
|
||
andThen(() => { | ||
sequence += '2'; | ||
}); | ||
}); | ||
}); | ||
}); |
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,17 @@ | ||
import { setupAsync, waitMillis, andThen } from '../../src/async'; | ||
|
||
describe('waitMillis', () => { | ||
setupAsync(); | ||
|
||
it('waits the specified amount of milliseconds', () => { | ||
const start = Date.now(); | ||
|
||
waitMillis(1337); | ||
|
||
andThen(() => { | ||
const elapsed = Date.now() - start; | ||
const tolerance = 20; | ||
expect(elapsed).to.be.closeTo(1337, tolerance); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.