Skip to content

Commit

Permalink
Split up tests into different files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitchal committed Apr 21, 2017
1 parent 9f1d4ad commit 2f434cb
Show file tree
Hide file tree
Showing 14 changed files with 768 additions and 756 deletions.
104 changes: 104 additions & 0 deletions tests/acceptance/mouse-events.test.js
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);
});
}
});
58 changes: 58 additions & 0 deletions tests/acceptance/test-div-dimensions.test.js
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);
});
}
});
28 changes: 28 additions & 0 deletions tests/acceptance/visit.test.js
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();
});
});
});
38 changes: 38 additions & 0 deletions tests/acceptance/waitUntilDisappears.test.js
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);
});
});
26 changes: 26 additions & 0 deletions tests/acceptance/waitUntilExists.test.js
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');
});
});
});
31 changes: 31 additions & 0 deletions tests/async/andThen.test.js
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';
});
});
});
});
17 changes: 17 additions & 0 deletions tests/async/waitMillis.test.js
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);
});
});
});
Loading

0 comments on commit 2f434cb

Please sign in to comment.