Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 430082e

Browse files
committed
refactor(locationSpec): make helper functions take an object
Makes tests more readable
1 parent fe7d9de commit 430082e

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

test/ng/locationSpec.js

+62-60
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ describe('$location', function() {
301301
});
302302

303303
it('should not rewrite when hashbang url is not given', function() {
304-
initService(true, '!', true);
304+
initService({html5Mode:true,hashPrefix: '!',supportHistory: true});
305305
inject(
306-
initBrowser('http://domain.com/base/a/b', '/base'),
306+
initBrowser({url:'http://domain.com/base/a/b',basePath: '/base'}),
307307
function($rootScope, $location, $browser) {
308308
expect($browser.url()).toBe('http://domain.com/base/a/b');
309309
}
@@ -536,24 +536,24 @@ describe('$location', function() {
536536
});
537537

538538

539-
function initService(html5Mode, hashPrefix, supportHistory) {
539+
function initService(options) {
540540
return module(function($provide, $locationProvider){
541-
$locationProvider.html5Mode(html5Mode);
542-
$locationProvider.hashPrefix(hashPrefix);
543-
$provide.value('$sniffer', {history: supportHistory});
541+
$locationProvider.html5Mode(options.html5Mode);
542+
$locationProvider.hashPrefix(options.hashPrefix);
543+
$provide.value('$sniffer', {history: options.supportHistory});
544544
});
545545
}
546-
function initBrowser(url, basePath) {
546+
function initBrowser(options) {
547547
return function($browser){
548-
$browser.url(url);
549-
$browser.$$baseHref = basePath;
548+
$browser.url(options.url);
549+
$browser.$$baseHref = options.basePath;
550550
};
551551
}
552552

553553
describe('wiring', function() {
554554

555-
beforeEach(initService(false, '!', true));
556-
beforeEach(inject(initBrowser('http://new.com/a/b#!', 'http://new.com/a/b')));
555+
beforeEach(initService({html5Mode:false,hashPrefix: '!',supportHistory: true}));
556+
beforeEach(inject(initBrowser({url:'http://new.com/a/b#!',basePath: 'http://new.com/a/b'})));
557557

558558

559559
it('should update $location when browser url changes', inject(function($browser, $location) {
@@ -677,9 +677,9 @@ describe('$location', function() {
677677
describe('disabled history', function() {
678678

679679
it('should use hashbang url with hash prefix', function() {
680-
initService(false, '!');
680+
initService({html5Mode:false,hashPrefix: '!'});
681681
inject(
682-
initBrowser('http://domain.com/base/index.html#!/a/b', '/base/index.html'),
682+
initBrowser({url:'http://domain.com/base/index.html#!/a/b',basePath: '/base/index.html'}),
683683
function($rootScope, $location, $browser) {
684684
expect($browser.url()).toBe('http://domain.com/base/index.html#!/a/b');
685685
$location.path('/new');
@@ -692,9 +692,9 @@ describe('$location', function() {
692692

693693

694694
it('should use hashbang url without hash prefix', function() {
695-
initService(false, '');
695+
initService({html5Mode:false,hashPrefix: ''});
696696
inject(
697-
initBrowser('http://domain.com/base/index.html#/a/b', '/base/index.html'),
697+
initBrowser({url:'http://domain.com/base/index.html#/a/b',basePath: '/base/index.html'}),
698698
function($rootScope, $location, $browser) {
699699
expect($browser.url()).toBe('http://domain.com/base/index.html#/a/b');
700700
$location.path('/new');
@@ -715,9 +715,9 @@ describe('$location', function() {
715715
}));
716716

717717
it('should use hashbang url with hash prefix', function() {
718-
initService(true, '!!', false);
718+
initService({html5Mode:true,hashPrefix: '!!',supportHistory: false});
719719
inject(
720-
initBrowser('http://domain.com/base/index.html#!!/a/b', '/base/index.html'),
720+
initBrowser({url:'http://domain.com/base/index.html#!!/a/b',basePath: '/base/index.html'}),
721721
function($rootScope, $location, $browser) {
722722
expect($browser.url()).toBe('http://domain.com/base/index.html#!!/a/b');
723723
$location.path('/new');
@@ -730,19 +730,19 @@ describe('$location', function() {
730730

731731

732732
it('should redirect to hashbang url when new url given', function() {
733-
initService(true, '!');
733+
initService({html5Mode:true,hashPrefix: '!'});
734734
inject(
735-
initBrowser('http://domain.com/base/new-path/index.html', '/base/index.html'),
735+
initBrowser({url:'http://domain.com/base/new-path/index.html',basePath: '/base/index.html'}),
736736
function($browser, $location) {
737737
expect($browser.url()).toBe('http://domain.com/base/index.html#!/new-path/index.html');
738738
}
739739
);
740740
});
741741

742742
it('should correctly convert html5 url with path matching basepath to hashbang url', function () {
743-
initService(true, '!', false);
743+
initService({html5Mode:true,hashPrefix: '!',supportHistory: false});
744744
inject(
745-
initBrowser('http://domain.com/base/index.html', '/base/index.html'),
745+
initBrowser({url:'http://domain.com/base/index.html',basePath: '/base/index.html'}),
746746
function($browser, $location) {
747747
expect($browser.url()).toBe('http://domain.com/base/index.html#!/index.html');
748748
}
@@ -759,9 +759,9 @@ describe('$location', function() {
759759
}));
760760

761761
it('should use new url', function() {
762-
initService(true, '', true);
762+
initService({html5Mode:true,hashPrefix: '',supportHistory: true});
763763
inject(
764-
initBrowser('http://domain.com/base/old/index.html#a', '/base/index.html'),
764+
initBrowser({url:'http://domain.com/base/old/index.html#a',basePath: '/base/index.html'}),
765765
function($rootScope, $location, $browser) {
766766
expect($browser.url()).toBe('http://domain.com/base/old/index.html#a');
767767
$location.path('/new');
@@ -774,9 +774,9 @@ describe('$location', function() {
774774

775775

776776
it('should rewrite when hashbang url given', function() {
777-
initService(true, '!', true);
777+
initService({html5Mode:true,hashPrefix: '!',supportHistory: true});
778778
inject(
779-
initBrowser('http://domain.com/base/index.html#!/a/b', '/base/index.html'),
779+
initBrowser({url:'http://domain.com/base/index.html#!/a/b',basePath: '/base/index.html'}),
780780
function($rootScope, $location, $browser) {
781781
expect($browser.url()).toBe('http://domain.com/base/a/b');
782782
$location.path('/new');
@@ -790,9 +790,9 @@ describe('$location', function() {
790790

791791

792792
it('should rewrite when hashbang url given (without hash prefix)', function() {
793-
initService(true, '', true);
793+
initService({html5Mode:true,hashPrefix: '',supportHistory: true});
794794
inject(
795-
initBrowser('http://domain.com/base/index.html#/a/b', '/base/index.html'),
795+
initBrowser({url:'http://domain.com/base/index.html#/a/b',basePath: '/base/index.html'}),
796796
function($rootScope, $location, $browser) {
797797
expect($browser.url()).toBe('http://domain.com/base/a/b');
798798
expect($location.path()).toBe('/a/b');
@@ -802,9 +802,9 @@ describe('$location', function() {
802802

803803

804804
it('should set appBase to serverBase if base[href] is missing', function() {
805-
initService(true, '!', true);
805+
initService({html5Mode:true,hashPrefix: '!',supportHistory: true});
806806
inject(
807-
initBrowser('http://domain.com/my/view1#anchor1', ''),
807+
initBrowser({url:'http://domain.com/my/view1#anchor1',basePath: ''}),
808808
function($rootScope, $location, $browser) {
809809
expect($browser.url()).toBe('http://domain.com/my/view1#anchor1');
810810
expect($location.path()).toBe('/my/view1');
@@ -847,12 +847,14 @@ describe('$location', function() {
847847

848848
var root, link, originalBrowser, lastEventPreventDefault;
849849

850-
function configureService(linkHref, html5Mode, supportHist, relLink, attrs, content) {
851-
if (typeof relLink !== "boolean") {
852-
content = attrs;
853-
attrs = relLink;
854-
relLink = false;
855-
}
850+
function configureService(options) {
851+
var linkHref = options.linkHref,
852+
html5Mode = options.html5Mode,
853+
supportHist = options.supportHist,
854+
relLink = options.relLink,
855+
attrs = options.attrs,
856+
content = options.content;
857+
856858
module(function($provide, $locationProvider) {
857859
attrs = attrs ? ' ' + attrs + ' ' : '';
858860

@@ -921,7 +923,7 @@ describe('$location', function() {
921923

922924

923925
it('should rewrite rel link to new url when history enabled on new browser', function() {
924-
configureService('link?a#b', true, true);
926+
configureService({linkHref: 'link?a#b', html5Mode: true, supportHist: true});
925927
inject(
926928
initBrowser(),
927929
initLocation(),
@@ -934,7 +936,7 @@ describe('$location', function() {
934936

935937

936938
it('should do nothing if already on the same URL', function() {
937-
configureService('/base/', true, true);
939+
configureService({linkHref: '/base/', html5Mode: true, supportHist: true});
938940
inject(
939941
initBrowser(),
940942
initLocation(),
@@ -961,7 +963,7 @@ describe('$location', function() {
961963

962964

963965
it('should rewrite abs link to new url when history enabled on new browser', function() {
964-
configureService('/base/link?a#b', true, true);
966+
configureService({linkHref: '/base/link?a#b', html5Mode: true, supportHist: true});
965967
inject(
966968
initBrowser(),
967969
initLocation(),
@@ -974,7 +976,7 @@ describe('$location', function() {
974976

975977

976978
it('should rewrite rel link to hashbang url when history enabled on old browser', function() {
977-
configureService('link?a#b', true, false);
979+
configureService({linkHref: 'link?a#b', html5Mode: true, supportHist: false});
978980
inject(
979981
initBrowser(),
980982
initLocation(),
@@ -988,7 +990,7 @@ describe('$location', function() {
988990

989991
// Regression (gh-7721)
990992
it('should not throw when clicking anchor with no href attribute when history enabled on old browser', function() {
991-
configureService(null, true, false);
993+
configureService({linkHref: null, html5Mode: true, supportHist: false});
992994
inject(
993995
initBrowser(),
994996
initLocation(),
@@ -1001,7 +1003,7 @@ describe('$location', function() {
10011003

10021004

10031005
it('should produce relative paths correctly when $location.path() is "/" when history enabled on old browser', function() {
1004-
configureService('partial1', true, false, true);
1006+
configureService({linkHref: 'partial1', html5Mode: true, supportHist: false, relLink: true});
10051007
inject(
10061008
initBrowser(),
10071009
initLocation(),
@@ -1015,7 +1017,7 @@ describe('$location', function() {
10151017

10161018

10171019
it('should rewrite abs link to hashbang url when history enabled on old browser', function() {
1018-
configureService('/base/link?a#b', true, false);
1020+
configureService({linkHref: '/base/link?a#b', html5Mode: true, supportHist: false});
10191021
inject(
10201022
initBrowser(),
10211023
initLocation(),
@@ -1028,7 +1030,7 @@ describe('$location', function() {
10281030

10291031

10301032
it('should not rewrite full url links do different domain', function() {
1031-
configureService('http://www.dot.abc/a?b=c', true);
1033+
configureService({linkHref: 'http://www.dot.abc/a?b=c', html5Mode: true});
10321034
inject(
10331035
initBrowser(),
10341036
initLocation(),
@@ -1041,7 +1043,7 @@ describe('$location', function() {
10411043

10421044

10431045
it('should not rewrite links with target="_blank"', function() {
1044-
configureService('/a?b=c', true, true, 'target="_blank"');
1046+
configureService({linkHref: '/a?b=c', html5Mode: true, supportHist: true, attrs: 'target="_blank"'});
10451047
inject(
10461048
initBrowser(),
10471049
initLocation(),
@@ -1054,7 +1056,7 @@ describe('$location', function() {
10541056

10551057

10561058
it('should not rewrite links with target specified', function() {
1057-
configureService('/a?b=c', true, true, 'target="some-frame"');
1059+
configureService({linkHref: '/a?b=c', html5Mode: true, supportHist: true, attrs: 'target="some-frame"'});
10581060
inject(
10591061
initBrowser(),
10601062
initLocation(),
@@ -1067,7 +1069,7 @@ describe('$location', function() {
10671069

10681070

10691071
it('should not rewrite links with `javascript:` URI', function() {
1070-
configureService(' jAvAsCrIpT:throw new Error("Boom!")', true, true, true);
1072+
configureService({linkHref: ' jAvAsCrIpT:throw new Error("Boom!")', html5Mode: true, supportHist: true, relLink: true});
10711073
inject(
10721074
initBrowser(),
10731075
initLocation(),
@@ -1080,7 +1082,7 @@ describe('$location', function() {
10801082

10811083

10821084
it('should not rewrite links with `mailto:` URI', function() {
1083-
configureService(' mAiLtO:foo@bar.com', true, true, true);
1085+
configureService({linkHref: ' mAiLtO:foo@bar.com', html5Mode: true, supportHist: true, relLink: true});
10841086
inject(
10851087
initBrowser(),
10861088
initLocation(),
@@ -1093,7 +1095,7 @@ describe('$location', function() {
10931095

10941096

10951097
it('should rewrite full url links to same domain and base path', function() {
1096-
configureService('http://host.com/base/new', true);
1098+
configureService({linkHref: 'http://host.com/base/new', html5Mode: true});
10971099
inject(
10981100
initBrowser(),
10991101
initLocation(),
@@ -1106,7 +1108,7 @@ describe('$location', function() {
11061108

11071109

11081110
it('should rewrite when clicked span inside link', function() {
1109-
configureService('some/link', true, true, '', '<span>link</span>');
1111+
configureService({linkHref: 'some/link', html5Mode: true, supportHist: true, attrs: '', content: '<span>link</span>'});
11101112
inject(
11111113
initBrowser(),
11121114
initLocation(),
@@ -1122,7 +1124,7 @@ describe('$location', function() {
11221124

11231125
it('should not rewrite when link to different base path when history enabled on new browser',
11241126
function() {
1125-
configureService('/other_base/link', true, true);
1127+
configureService({linkHref: '/other_base/link', html5Mode: true, supportHist: true});
11261128
inject(
11271129
initBrowser(),
11281130
initLocation(),
@@ -1136,7 +1138,7 @@ describe('$location', function() {
11361138

11371139
it('should not rewrite when link to different base path when history enabled on old browser',
11381140
function() {
1139-
configureService('/other_base/link', true, false);
1141+
configureService({linkHref: '/other_base/link', html5Mode: true, supportHist: false});
11401142
inject(
11411143
initBrowser(),
11421144
initLocation(),
@@ -1149,7 +1151,7 @@ describe('$location', function() {
11491151

11501152

11511153
it('should not rewrite when link to different base path when history disabled', function() {
1152-
configureService('/other_base/link', false);
1154+
configureService({linkHref: '/other_base/link', html5Mode: false});
11531155
inject(
11541156
initBrowser(),
11551157
initLocation(),
@@ -1163,7 +1165,7 @@ describe('$location', function() {
11631165

11641166
it('should not rewrite when full link to different base path when history enabled on new browser',
11651167
function() {
1166-
configureService('http://host.com/other_base/link', true, true);
1168+
configureService({linkHref: 'http://host.com/other_base/link', html5Mode: true, supportHist: true});
11671169
inject(
11681170
initBrowser(),
11691171
initLocation(),
@@ -1177,7 +1179,7 @@ describe('$location', function() {
11771179

11781180
it('should not rewrite when full link to different base path when history enabled on old browser',
11791181
function() {
1180-
configureService('http://host.com/other_base/link', true, false);
1182+
configureService({linkHref: 'http://host.com/other_base/link', html5Mode: true, supportHist: false});
11811183
inject(
11821184
initBrowser(),
11831185
initLocation(),
@@ -1190,7 +1192,7 @@ describe('$location', function() {
11901192

11911193

11921194
it('should not rewrite when full link to different base path when history disabled', function() {
1193-
configureService('http://host.com/other_base/link', false);
1195+
configureService({linkHref: 'http://host.com/other_base/link', html5Mode: false});
11941196
inject(
11951197
initBrowser(),
11961198
initLocation(),
@@ -1203,7 +1205,7 @@ describe('$location', function() {
12031205

12041206

12051207
it('should rewrite relative links relative to current path when history disabled', function() {
1206-
configureService('link', true, false, true);
1208+
configureService({linkHref: 'link', html5Mode: true, supportHist: false, relLink: true});
12071209
inject(
12081210
initBrowser(),
12091211
initLocation(),
@@ -1217,7 +1219,7 @@ describe('$location', function() {
12171219

12181220

12191221
it('should replace current path when link begins with "/" and history disabled', function() {
1220-
configureService('/link', true, false, true);
1222+
configureService({linkHref: '/link', html5Mode: true, supportHist: false, relLink: true});
12211223
inject(
12221224
initBrowser(),
12231225
initLocation(),
@@ -1231,7 +1233,7 @@ describe('$location', function() {
12311233

12321234

12331235
it('should replace current hash fragment when link begins with "#" history disabled', function() {
1234-
configureService('#link', true, false, true);
1236+
configureService({linkHref: '#link', html5Mode: true, supportHist: false, relLink: true});
12351237
inject(
12361238
initBrowser(),
12371239
initLocation(),
@@ -1251,7 +1253,7 @@ describe('$location', function() {
12511253
if (!msie || msie >= 9) {
12521254

12531255
it('should not rewrite when clicked with ctrl pressed', function() {
1254-
configureService('/a?b=c', true, true);
1256+
configureService({linkHref: '/a?b=c', html5Mode: true, supportHist: true});
12551257
inject(
12561258
initBrowser(),
12571259
initLocation(),
@@ -1264,7 +1266,7 @@ describe('$location', function() {
12641266

12651267

12661268
it('should not rewrite when clicked with meta pressed', function() {
1267-
configureService('/a?b=c', true, true);
1269+
configureService({linkHref: '/a?b=c', html5Mode: true, supportHist: true});
12681270
inject(
12691271
initBrowser(),
12701272
initLocation(),

0 commit comments

Comments
 (0)