Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bttf committed Apr 1, 2016
1 parent af99ee3 commit d621cd0
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 1 deletion.
102 changes: 102 additions & 0 deletions tests/acceptance/scroll-operator-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import Ember from 'ember';
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | scroll operator');

/**
* This test is seemingly impossible right now. Unable to successfully emulate
* hitting the browser back/forward button due to acceptance tests using
* locationType 'none'. Setting to 'hash' does not seem to help. Need to
* revisit.
*
* test('scroll operator works', function(assert) {
* // Configure location type to simulate browser back button
* this.application.Router.reopen({
* location: 'hash',
* });
* const viewportHeight = Ember.$(window).height();
* const testScrollTop = viewportHeight + 5;
*
* visit('/').then(() => {
* // Modify page to have scrollable content
* const tallDiv = Ember.$('<div></div>').css({ height: viewportHeight * 2 });
* Ember.$(document.body).append(tallDiv);
* // scroll to a position
* Ember.$(window).scrollTop(testScrollTop);
* });
*
* // navigate away from page and change scrollTop
* visit('/route-a').then(() => {
* Ember.$(window).scrollTop(0);
* });
*
* // use browser's native back functionality to navigate to previous route
* //window.history.back();
* visit('/');
*
* // assert that previous position is preserved
* andThen(function() {
* assert.equal(currentURL(), '/');
* assert.equal(Ember.$(window).scrollTop(), testScrollTop);
* });
* });
**/

test('scroll operator does not resume scroll for non-browser triggered transitions', function(assert) {
// get viewport height
const viewportHeight = Ember.$(window).height();

// create arbitrary scrollTo value
const testScrollTop = viewportHeight + 5;

visit('/').then(() => {
// create and append div of 'tall' height
const tallDiv = Ember.$('<div></div>').css({ height: viewportHeight * 2 });
Ember.$(document.body).append(tallDiv);

// scroll to a position
Ember.$(window).scrollTop(testScrollTop);
});

// navigate away from page and ensure scroll
visit('/route-a').then(() => {
Ember.$(window).scrollTop(testScrollTop);
});

// visit index via app
visit('/');

// assert that previous position is reset (equal to 0)
andThen(function() {
assert.equal(currentURL(), '/');
assert.equal(Ember.$(window).scrollTop(), 0);
});
});

test('reset scroll will reset scroll position to 0', function(assert) {
visit('/').then(() => {
// get viewport height
const viewportHeight = Ember.$(window).height();

// create arbitrary scrollTo value
const testScrollTop = viewportHeight + 5;

// create and append div of 'tall' height
const tallDiv = Ember.$('<div></div>').css({ height: viewportHeight * 2 });
Ember.$(document.body).append(tallDiv);

// scroll to a position
Ember.$(window).scrollTop(testScrollTop);
});

visit('/reset-route');

andThen(function() {
assert.equal(currentURL(), '/reset-route');
assert.equal(Ember.$(window).scrollTop(), 0);
});
});
2 changes: 2 additions & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const Router = Ember.Router.extend({
});

Router.map(function() {
this.route('route-a');
this.route('reset-route');
});

export default Router;
4 changes: 4 additions & 0 deletions tests/dummy/app/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Ember from 'ember';
import ScrollOperatorMixin from 'ember-scroll-operator/mixins/scroll-operator';

export default Ember.Route.extend(ScrollOperatorMixin);
4 changes: 4 additions & 0 deletions tests/dummy/app/routes/reset-route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Ember from 'ember';
import ResetScrollMixin from 'ember-scroll-operator/mixins/reset-scroll';

export default Ember.Route.extend(ResetScrollMixin);
3 changes: 3 additions & 0 deletions tests/dummy/app/routes/route-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Ember from 'ember';

export default Ember.Route.extend();
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function(environment) {
modulePrefix: 'dummy',
environment: environment,
baseURL: '/',
locationType: 'auto',
locationType: 'hash',
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
Expand Down

0 comments on commit d621cd0

Please sign in to comment.