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

Commit b9007df

Browse files
committed
refactor(locationSpec): make helper functions take an object
Makes tests more readable
1 parent 0604bb7 commit b9007df

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
@@ -297,9 +297,9 @@ describe('$location', function() {
297297
});
298298

299299
it('should not rewrite when hashbang url is not given', function() {
300-
initService(true, '!', true);
300+
initService({html5Mode:true,hashPrefix: '!',supportHistory: true});
301301
inject(
302-
initBrowser('http://domain.com/base/a/b', '/base'),
302+
initBrowser({url:'http://domain.com/base/a/b',basePath: '/base'}),
303303
function($rootScope, $location, $browser) {
304304
expect($browser.url()).toBe('http://domain.com/base/a/b');
305305
}
@@ -532,24 +532,24 @@ describe('$location', function() {
532532
});
533533

534534

535-
function initService(html5Mode, hashPrefix, supportHistory) {
535+
function initService(options) {
536536
return module(function($provide, $locationProvider){
537-
$locationProvider.html5Mode(html5Mode);
538-
$locationProvider.hashPrefix(hashPrefix);
539-
$provide.value('$sniffer', {history: supportHistory});
537+
$locationProvider.html5Mode(options.html5Mode);
538+
$locationProvider.hashPrefix(options.hashPrefix);
539+
$provide.value('$sniffer', {history: options.supportHistory});
540540
});
541541
}
542-
function initBrowser(url, basePath) {
542+
function initBrowser(options) {
543543
return function($browser){
544-
$browser.url(url);
545-
$browser.$$baseHref = basePath;
544+
$browser.url(options.url);
545+
$browser.$$baseHref = options.basePath;
546546
};
547547
}
548548

549549
describe('wiring', function() {
550550

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

554554

555555
it('should update $location when browser url changes', inject(function($browser, $location) {
@@ -673,9 +673,9 @@ describe('$location', function() {
673673
describe('disabled history', function() {
674674

675675
it('should use hashbang url with hash prefix', function() {
676-
initService(false, '!');
676+
initService({html5Mode:false,hashPrefix: '!'});
677677
inject(
678-
initBrowser('http://domain.com/base/index.html#!/a/b', '/base/index.html'),
678+
initBrowser({url:'http://domain.com/base/index.html#!/a/b',basePath: '/base/index.html'}),
679679
function($rootScope, $location, $browser) {
680680
expect($browser.url()).toBe('http://domain.com/base/index.html#!/a/b');
681681
$location.path('/new');
@@ -688,9 +688,9 @@ describe('$location', function() {
688688

689689

690690
it('should use hashbang url without hash prefix', function() {
691-
initService(false, '');
691+
initService({html5Mode:false,hashPrefix: ''});
692692
inject(
693-
initBrowser('http://domain.com/base/index.html#/a/b', '/base/index.html'),
693+
initBrowser({url:'http://domain.com/base/index.html#/a/b',basePath: '/base/index.html'}),
694694
function($rootScope, $location, $browser) {
695695
expect($browser.url()).toBe('http://domain.com/base/index.html#/a/b');
696696
$location.path('/new');
@@ -711,9 +711,9 @@ describe('$location', function() {
711711
}));
712712

713713
it('should use hashbang url with hash prefix', function() {
714-
initService(true, '!!', false);
714+
initService({html5Mode:true,hashPrefix: '!!',supportHistory: false});
715715
inject(
716-
initBrowser('http://domain.com/base/index.html#!!/a/b', '/base/index.html'),
716+
initBrowser({url:'http://domain.com/base/index.html#!!/a/b',basePath: '/base/index.html'}),
717717
function($rootScope, $location, $browser) {
718718
expect($browser.url()).toBe('http://domain.com/base/index.html#!!/a/b');
719719
$location.path('/new');
@@ -726,19 +726,19 @@ describe('$location', function() {
726726

727727

728728
it('should redirect to hashbang url when new url given', function() {
729-
initService(true, '!');
729+
initService({html5Mode:true,hashPrefix: '!'});
730730
inject(
731-
initBrowser('http://domain.com/base/new-path/index.html', '/base/index.html'),
731+
initBrowser({url:'http://domain.com/base/new-path/index.html',basePath: '/base/index.html'}),
732732
function($browser, $location) {
733733
expect($browser.url()).toBe('http://domain.com/base/index.html#!/new-path/index.html');
734734
}
735735
);
736736
});
737737

738738
it('should correctly convert html5 url with path matching basepath to hashbang url', function () {
739-
initService(true, '!', false);
739+
initService({html5Mode:true,hashPrefix: '!',supportHistory: false});
740740
inject(
741-
initBrowser('http://domain.com/base/index.html', '/base/index.html'),
741+
initBrowser({url:'http://domain.com/base/index.html',basePath: '/base/index.html'}),
742742
function($browser, $location) {
743743
expect($browser.url()).toBe('http://domain.com/base/index.html#!/index.html');
744744
}
@@ -755,9 +755,9 @@ describe('$location', function() {
755755
}));
756756

757757
it('should use new url', function() {
758-
initService(true, '', true);
758+
initService({html5Mode:true,hashPrefix: '',supportHistory: true});
759759
inject(
760-
initBrowser('http://domain.com/base/old/index.html#a', '/base/index.html'),
760+
initBrowser({url:'http://domain.com/base/old/index.html#a',basePath: '/base/index.html'}),
761761
function($rootScope, $location, $browser) {
762762
expect($browser.url()).toBe('http://domain.com/base/old/index.html#a');
763763
$location.path('/new');
@@ -770,9 +770,9 @@ describe('$location', function() {
770770

771771

772772
it('should rewrite when hashbang url given', function() {
773-
initService(true, '!', true);
773+
initService({html5Mode:true,hashPrefix: '!',supportHistory: true});
774774
inject(
775-
initBrowser('http://domain.com/base/index.html#!/a/b', '/base/index.html'),
775+
initBrowser({url:'http://domain.com/base/index.html#!/a/b',basePath: '/base/index.html'}),
776776
function($rootScope, $location, $browser) {
777777
expect($browser.url()).toBe('http://domain.com/base/a/b');
778778
$location.path('/new');
@@ -786,9 +786,9 @@ describe('$location', function() {
786786

787787

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

799799

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

844844
var root, link, originalBrowser, lastEventPreventDefault;
845845

846-
function configureService(linkHref, html5Mode, supportHist, relLink, attrs, content) {
847-
if (typeof relLink !== "boolean") {
848-
content = attrs;
849-
attrs = relLink;
850-
relLink = false;
851-
}
846+
function configureService(options) {
847+
var linkHref = options.linkHref,
848+
html5Mode = options.html5Mode,
849+
supportHist = options.supportHist,
850+
relLink = options.relLink,
851+
attrs = options.attrs,
852+
content = options.content;
853+
852854
module(function($provide, $locationProvider) {
853855
attrs = attrs ? ' ' + attrs + ' ' : '';
854856

@@ -917,7 +919,7 @@ describe('$location', function() {
917919

918920

919921
it('should rewrite rel link to new url when history enabled on new browser', function() {
920-
configureService('link?a#b', true, true);
922+
configureService({linkHref: 'link?a#b', html5Mode: true, supportHist: true});
921923
inject(
922924
initBrowser(),
923925
initLocation(),
@@ -930,7 +932,7 @@ describe('$location', function() {
930932

931933

932934
it('should do nothing if already on the same URL', function() {
933-
configureService('/base/', true, true);
935+
configureService({linkHref: '/base/', html5Mode: true, supportHist: true});
934936
inject(
935937
initBrowser(),
936938
initLocation(),
@@ -957,7 +959,7 @@ describe('$location', function() {
957959

958960

959961
it('should rewrite abs link to new url when history enabled on new browser', function() {
960-
configureService('/base/link?a#b', true, true);
962+
configureService({linkHref: '/base/link?a#b', html5Mode: true, supportHist: true});
961963
inject(
962964
initBrowser(),
963965
initLocation(),
@@ -970,7 +972,7 @@ describe('$location', function() {
970972

971973

972974
it('should rewrite rel link to hashbang url when history enabled on old browser', function() {
973-
configureService('link?a#b', true, false);
975+
configureService({linkHref: 'link?a#b', html5Mode: true, supportHist: false});
974976
inject(
975977
initBrowser(),
976978
initLocation(),
@@ -984,7 +986,7 @@ describe('$location', function() {
984986

985987
// Regression (gh-7721)
986988
it('should not throw when clicking anchor with no href attribute when history enabled on old browser', function() {
987-
configureService(null, true, false);
989+
configureService({linkHref: null, html5Mode: true, supportHist: false});
988990
inject(
989991
initBrowser(),
990992
initLocation(),
@@ -997,7 +999,7 @@ describe('$location', function() {
997999

9981000

9991001
it('should produce relative paths correctly when $location.path() is "/" when history enabled on old browser', function() {
1000-
configureService('partial1', true, false, true);
1002+
configureService({linkHref: 'partial1', html5Mode: true, supportHist: false, relLink: true});
10011003
inject(
10021004
initBrowser(),
10031005
initLocation(),
@@ -1011,7 +1013,7 @@ describe('$location', function() {
10111013

10121014

10131015
it('should rewrite abs link to hashbang url when history enabled on old browser', function() {
1014-
configureService('/base/link?a#b', true, false);
1016+
configureService({linkHref: '/base/link?a#b', html5Mode: true, supportHist: false});
10151017
inject(
10161018
initBrowser(),
10171019
initLocation(),
@@ -1024,7 +1026,7 @@ describe('$location', function() {
10241026

10251027

10261028
it('should not rewrite full url links do different domain', function() {
1027-
configureService('http://www.dot.abc/a?b=c', true);
1029+
configureService({linkHref: 'http://www.dot.abc/a?b=c', html5Mode: true});
10281030
inject(
10291031
initBrowser(),
10301032
initLocation(),
@@ -1037,7 +1039,7 @@ describe('$location', function() {
10371039

10381040

10391041
it('should not rewrite links with target="_blank"', function() {
1040-
configureService('/a?b=c', true, true, 'target="_blank"');
1042+
configureService({linkHref: '/a?b=c', html5Mode: true, supportHist: true, attrs: 'target="_blank"'});
10411043
inject(
10421044
initBrowser(),
10431045
initLocation(),
@@ -1050,7 +1052,7 @@ describe('$location', function() {
10501052

10511053

10521054
it('should not rewrite links with target specified', function() {
1053-
configureService('/a?b=c', true, true, 'target="some-frame"');
1055+
configureService({linkHref: '/a?b=c', html5Mode: true, supportHist: true, attrs: 'target="some-frame"'});
10541056
inject(
10551057
initBrowser(),
10561058
initLocation(),
@@ -1063,7 +1065,7 @@ describe('$location', function() {
10631065

10641066

10651067
it('should not rewrite links with `javascript:` URI', function() {
1066-
configureService(' jAvAsCrIpT:throw new Error("Boom!")', true, true, true);
1068+
configureService({linkHref: ' jAvAsCrIpT:throw new Error("Boom!")', html5Mode: true, supportHist: true, relLink: true});
10671069
inject(
10681070
initBrowser(),
10691071
initLocation(),
@@ -1076,7 +1078,7 @@ describe('$location', function() {
10761078

10771079

10781080
it('should not rewrite links with `mailto:` URI', function() {
1079-
configureService(' mAiLtO:[email protected]', true, true, true);
1081+
configureService({linkHref: ' mAiLtO:[email protected]', html5Mode: true, supportHist: true, relLink: true});
10801082
inject(
10811083
initBrowser(),
10821084
initLocation(),
@@ -1089,7 +1091,7 @@ describe('$location', function() {
10891091

10901092

10911093
it('should rewrite full url links to same domain and base path', function() {
1092-
configureService('http://host.com/base/new', true);
1094+
configureService({linkHref: 'http://host.com/base/new', html5Mode: true});
10931095
inject(
10941096
initBrowser(),
10951097
initLocation(),
@@ -1102,7 +1104,7 @@ describe('$location', function() {
11021104

11031105

11041106
it('should rewrite when clicked span inside link', function() {
1105-
configureService('some/link', true, true, '', '<span>link</span>');
1107+
configureService({linkHref: 'some/link', html5Mode: true, supportHist: true, attrs: '', content: '<span>link</span>'});
11061108
inject(
11071109
initBrowser(),
11081110
initLocation(),
@@ -1118,7 +1120,7 @@ describe('$location', function() {
11181120

11191121
it('should not rewrite when link to different base path when history enabled on new browser',
11201122
function() {
1121-
configureService('/other_base/link', true, true);
1123+
configureService({linkHref: '/other_base/link', html5Mode: true, supportHist: true});
11221124
inject(
11231125
initBrowser(),
11241126
initLocation(),
@@ -1132,7 +1134,7 @@ describe('$location', function() {
11321134

11331135
it('should not rewrite when link to different base path when history enabled on old browser',
11341136
function() {
1135-
configureService('/other_base/link', true, false);
1137+
configureService({linkHref: '/other_base/link', html5Mode: true, supportHist: false});
11361138
inject(
11371139
initBrowser(),
11381140
initLocation(),
@@ -1145,7 +1147,7 @@ describe('$location', function() {
11451147

11461148

11471149
it('should not rewrite when link to different base path when history disabled', function() {
1148-
configureService('/other_base/link', false);
1150+
configureService({linkHref: '/other_base/link', html5Mode: false});
11491151
inject(
11501152
initBrowser(),
11511153
initLocation(),
@@ -1159,7 +1161,7 @@ describe('$location', function() {
11591161

11601162
it('should not rewrite when full link to different base path when history enabled on new browser',
11611163
function() {
1162-
configureService('http://host.com/other_base/link', true, true);
1164+
configureService({linkHref: 'http://host.com/other_base/link', html5Mode: true, supportHist: true});
11631165
inject(
11641166
initBrowser(),
11651167
initLocation(),
@@ -1173,7 +1175,7 @@ describe('$location', function() {
11731175

11741176
it('should not rewrite when full link to different base path when history enabled on old browser',
11751177
function() {
1176-
configureService('http://host.com/other_base/link', true, false);
1178+
configureService({linkHref: 'http://host.com/other_base/link', html5Mode: true, supportHist: false});
11771179
inject(
11781180
initBrowser(),
11791181
initLocation(),
@@ -1186,7 +1188,7 @@ describe('$location', function() {
11861188

11871189

11881190
it('should not rewrite when full link to different base path when history disabled', function() {
1189-
configureService('http://host.com/other_base/link', false);
1191+
configureService({linkHref: 'http://host.com/other_base/link', html5Mode: false});
11901192
inject(
11911193
initBrowser(),
11921194
initLocation(),
@@ -1199,7 +1201,7 @@ describe('$location', function() {
11991201

12001202

12011203
it('should rewrite relative links relative to current path when history disabled', function() {
1202-
configureService('link', true, false, true);
1204+
configureService({linkHref: 'link', html5Mode: true, supportHist: false, relLink: true});
12031205
inject(
12041206
initBrowser(),
12051207
initLocation(),
@@ -1213,7 +1215,7 @@ describe('$location', function() {
12131215

12141216

12151217
it('should replace current path when link begins with "/" and history disabled', function() {
1216-
configureService('/link', true, false, true);
1218+
configureService({linkHref: '/link', html5Mode: true, supportHist: false, relLink: true});
12171219
inject(
12181220
initBrowser(),
12191221
initLocation(),
@@ -1227,7 +1229,7 @@ describe('$location', function() {
12271229

12281230

12291231
it('should replace current hash fragment when link begins with "#" history disabled', function() {
1230-
configureService('#link', true, false, true);
1232+
configureService({linkHref: '#link', html5Mode: true, supportHist: false, relLink: true});
12311233
inject(
12321234
initBrowser(),
12331235
initLocation(),
@@ -1247,7 +1249,7 @@ describe('$location', function() {
12471249
if (!msie || msie >= 9) {
12481250

12491251
it('should not rewrite when clicked with ctrl pressed', function() {
1250-
configureService('/a?b=c', true, true);
1252+
configureService({linkHref: '/a?b=c', html5Mode: true, supportHist: true});
12511253
inject(
12521254
initBrowser(),
12531255
initLocation(),
@@ -1260,7 +1262,7 @@ describe('$location', function() {
12601262

12611263

12621264
it('should not rewrite when clicked with meta pressed', function() {
1263-
configureService('/a?b=c', true, true);
1265+
configureService({linkHref: '/a?b=c', html5Mode: true, supportHist: true});
12641266
inject(
12651267
initBrowser(),
12661268
initLocation(),

0 commit comments

Comments
 (0)