Skip to content

Commit

Permalink
Merge pull request #16030 from thoov/ember-routing-part-3
Browse files Browse the repository at this point in the history
[CLEANUP] Convert ember-routing tests to new style (Part 3)
  • Loading branch information
locks authored Dec 25, 2017
2 parents 4ec2098 + aed0088 commit cd61ae4
Show file tree
Hide file tree
Showing 4 changed files with 445 additions and 488 deletions.
209 changes: 98 additions & 111 deletions packages/ember-routing/tests/location/hash_location_test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { get, run } from 'ember-metal';
import HashLocation from '../../location/hash_location';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

let HashTestLocation, location;
let location;

function createLocation(options, assert) {
let HashTestLocation = HashLocation.extend({
_location: {
href: 'http://test.com/',
pathname: '/',
hash: '',
search: '',
replace() {
assert.ok(false, 'location.replace should not be called during testing');
}
}
});

function createLocation(options) {
if (!options) { options = {}; }
location = HashTestLocation.create(options);
}
Expand Down Expand Up @@ -35,153 +48,127 @@ function triggerHashchange() {
window.dispatchEvent(event);
}

QUnit.module('Ember.HashLocation', {
setup() {
HashTestLocation = HashLocation.extend({
_location: {
href: 'http://test.com/',
pathname: '/',
hash: '',
search: '',
replace() {
ok(false, 'location.replace should not be called during testing');
}
}
});
},

moduleFor('Ember.HashLocation', class extends AbstractTestCase {
teardown() {
run(function() {
if (location) { location.destroy(); }
});
}
});

QUnit.test('HashLocation.getURL() returns the current url', function() {
expect(1);

createLocation({
_location: mockBrowserLocation('/#/foo/bar')
});

equal(location.getURL(), '/foo/bar');
});

QUnit.test('HashLocation.getURL() includes extra hashes', function() {
expect(1);

createLocation({
_location: mockBrowserLocation('/#/foo#bar#car')
});

equal(location.getURL(), '/foo#bar#car');
});
['@test HashLocation.getURL() returns the current url'](assert) {
createLocation({
_location: mockBrowserLocation('/#/foo/bar')
}, assert);

QUnit.test('HashLocation.getURL() assumes location.hash without #/ prefix is not a route path', function() {
expect(1);
assert.equal(location.getURL(), '/foo/bar');
}

createLocation({
_location: mockBrowserLocation('/#foo#bar')
});
['@test HashLocation.getURL() includes extra hashes'](assert) {
createLocation({
_location: mockBrowserLocation('/#/foo#bar#car')
}, assert);

equal(location.getURL(), '/#foo#bar');
});
assert.equal(location.getURL(), '/foo#bar#car');
}

QUnit.test('HashLocation.getURL() returns a normal forward slash when there is no location.hash', function() {
expect(1);
['@test HashLocation.getURL() assumes location.hash without #/ prefix is not a route path'](assert) {
createLocation({
_location: mockBrowserLocation('/#foo#bar')
}, assert);

createLocation({
_location: mockBrowserLocation('/')
});
assert.equal(location.getURL(), '/#foo#bar');
}

equal(location.getURL(), '/');
});
['@test HashLocation.getURL() returns a normal forward slash when there is no location.hash'](assert) {
createLocation({
_location: mockBrowserLocation('/')
}, assert);

QUnit.test('HashLocation.setURL() correctly sets the url', function() {
expect(2);
assert.equal(location.getURL(), '/');
}

createLocation();
['@test HashLocation.setURL() correctly sets the url'](assert) {
createLocation({}, assert);

location.setURL('/bar');
location.setURL('/bar');

equal(get(location, 'location.hash'), '/bar');
equal(get(location, 'lastSetURL'), '/bar');
});
assert.equal(get(location, 'location.hash'), '/bar');
assert.equal(get(location, 'lastSetURL'), '/bar');
}

QUnit.test('HashLocation.replaceURL() correctly replaces to the path with a page reload', function() {
expect(2);
['@test HashLocation.replaceURL() correctly replaces to the path with a page reload'](assert) {
expect(2);

createLocation({
_location: {
replace(path) {
equal(path, '#/foo');
createLocation({
_location: {
replace(path) {
assert.equal(path, '#/foo');
}
}
}
});

location.replaceURL('/foo');
}, assert);

equal(get(location, 'lastSetURL'), '/foo');
});
location.replaceURL('/foo');

QUnit.test('HashLocation.onUpdateURL callback executes as expected', function() {
expect(1);
assert.equal(get(location, 'lastSetURL'), '/foo');
}

createLocation({
_location: mockBrowserLocation('/#/foo/bar')
});
['@test HashLocation.onUpdateURL callback executes as expected'](assert) {
expect(1);

let callback = function (param) {
equal(param, '/foo/bar', 'path is passed as param');
};
createLocation({
_location: mockBrowserLocation('/#/foo/bar')
}, assert);

location.onUpdateURL(callback);
let callback = function (param) {
assert.equal(param, '/foo/bar', 'path is passed as param');
};

triggerHashchange();
});
location.onUpdateURL(callback);

QUnit.test('HashLocation.onUpdateURL doesn\'t execute callback if lastSetURL === path', function() {
expect(0);
triggerHashchange();
}

createLocation({
_location: {
href: '/#/foo/bar'
},
lastSetURL: '/foo/bar'
});
['@test HashLocation.onUpdateURL doesn\'t execute callback if lastSetURL === path'](assert) {
expect(0);

let callback = function () {
ok(false, 'callback should not be called');
};
createLocation({
_location: {
href: '/#/foo/bar'
},
lastSetURL: '/foo/bar'
}, assert);

location.onUpdateURL(callback);
let callback = function () {
assert.ok(false, 'callback should not be called');
};

triggerHashchange();
});
location.onUpdateURL(callback);

QUnit.test('HashLocation.formatURL() prepends a # to the provided string', function() {
expect(1);
triggerHashchange();
}

createLocation();
['@test HashLocation.formatURL() prepends a # to the provided string'](assert) {
createLocation({}, assert);

equal(location.formatURL('/foo#bar'), '#/foo#bar');
});
assert.equal(location.formatURL('/foo#bar'), '#/foo#bar');
}

QUnit.test('HashLocation.willDestroy() cleans up hashchange event listener', function() {
expect(1);
['@test HashLocation.willDestroy() cleans up hashchange event listener'](assert) {
expect(1);

createLocation();
createLocation({}, assert);

let callback = function () {
ok(true, 'should invoke callback once');
};
let callback = function () {
assert.ok(true, 'should invoke callback once');
};

location.onUpdateURL(callback);
location.onUpdateURL(callback);

triggerHashchange();
triggerHashchange();

run(location, 'destroy');
location = null;
run(location, 'destroy');
location = null;

triggerHashchange();
triggerHashchange();
}
});
Loading

0 comments on commit cd61ae4

Please sign in to comment.