Skip to content

Commit f949480

Browse files
test(vanilla): Mock browser location for browserHistorySpec so URL in karma does not change
fix(vanilla): vanilla locations: do not parse "empty string" query key parameter
1 parent 31a2ff3 commit f949480

File tree

4 files changed

+78
-47
lines changed

4 files changed

+78
-47
lines changed

src/vanilla/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import {isArray} from "../common/index";
66
import { LocationServices, LocationConfig, services } from "../common/coreservices";
77
import { UIRouter } from "../router";
8+
import { identity } from "../common/common";
89

910
const beforeAfterSubstr = (char: string) => (str: string): string[] => {
1011
if (!str) return ["", ""];
@@ -30,7 +31,7 @@ export const keyValsToObjectR = (accum, [key, val]) => {
3031
};
3132

3233
export const getParams = (queryString: string): any =>
33-
queryString.split("&").map(splitEqual).reduce(keyValsToObjectR, {});
34+
queryString.split("&").filter(identity).map(splitEqual).reduce(keyValsToObjectR, {});
3435

3536
export function locationPluginFactory(
3637
name: string,

test/_testUtils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ let stateProps = ["resolve", "resolvePolicy", "data", "template", "templateUrl",
66
export function tree2Array(tree, inheritName) {
77

88
function processState(parent, state, name) {
9-
var substates = omit.apply(null, [state].concat(stateProps));
10-
var thisState = pick.apply(null, [state].concat(stateProps));
9+
let substates = omit.apply(null, [state].concat(stateProps));
10+
let thisState = pick.apply(null, [state].concat(stateProps));
1111
thisState.name = name;
1212
if (!inheritName) thisState.parent = parent;
1313

@@ -27,19 +27,19 @@ export function tree2Array(tree, inheritName) {
2727
}
2828

2929
export function PromiseResult(promise?) {
30-
var self = this, _promise: Promise<any>;
31-
var resolve, reject, complete;
30+
let self = this, _promise: Promise<any>;
31+
let resolve, reject, complete;
3232

3333
this.setPromise = function(promise) {
3434
if (_promise) {
3535
throw new Error("Already have with'd a promise.");
3636
}
3737

38-
var onfulfilled = (data) =>
38+
let onfulfilled = (data) =>
3939
resolve = data || true;
40-
var onrejected = (err) =>
40+
let onrejected = (err) =>
4141
reject = err || true;
42-
var done = () =>
42+
let done = () =>
4343
complete = true;
4444

4545
_promise = promise;

test/vanilla.browserHistorySpec.ts

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
1-
2-
import {services, UrlMatcher} from '../src/index';
1+
import { UrlMatcher } from "../src/index";
32
import { UIRouter } from "../src/router";
4-
import * as vanilla from "../src/vanilla"
3+
import { UrlService } from "../src/url/urlService";
4+
import * as vanilla from "../src/vanilla";
55

66
describe('browserHistory implementation', () => {
77

88
let router: UIRouter;
9-
let locationProvider;
9+
let urlService: UrlService;
1010
let makeMatcher;
1111

12-
// Replace the `history` reference because PhantomJS does not support spying on it.
13-
function mockHistoryObject() {
14-
let plugin: any = router.getPlugin('vanilla.pushStateLocation');
12+
let mockHistory, mockLocation;
13+
14+
// Replace the history and location
15+
function mockPushState(_router) {
16+
let plugin: any = _router.getPlugin('vanilla.pushStateLocation');
17+
18+
mockHistory = {
19+
replaceState: (a, b, url) => mockLocation.href = url,
20+
pushState: (a, b, url) => mockLocation.href = url
21+
};
1522

16-
plugin.service._history = {
17-
replaceState: () => null,
18-
pushState: () => null
23+
mockLocation = {
24+
_href: "/",
25+
pathname: "/",
26+
search: "",
27+
get href() {
28+
return this._href
29+
},
30+
set href(val) {
31+
this._href = val;
32+
var [pathname, search] = val.split("?");
33+
this.pathname = pathname;
34+
this.search = search ? "?" + search : "";
35+
}
1936
};
2037

38+
plugin.service._history = mockHistory;
39+
plugin.service._location = mockLocation;
40+
2141
return plugin.service;
2242
}
2343

@@ -26,7 +46,7 @@ describe('browserHistory implementation', () => {
2646
router.plugin(vanilla.servicesPlugin);
2747
router.plugin(vanilla.pushStateLocationPlugin);
2848
router.stateRegistry.stateQueue.autoFlush(router.stateService);
29-
locationProvider = router.urlService;
49+
urlService = router.urlService;
3050
makeMatcher = (url, config?) => {
3151
return new UrlMatcher(url, router.urlMatcherFactory.paramTypes, config)
3252
};
@@ -38,34 +58,41 @@ describe('browserHistory implementation', () => {
3858
});
3959

4060
it('uses history.pushState when setting a url', () => {
41-
let service = mockHistoryObject();
61+
let service = mockPushState(router);
4262
expect(router.urlService.config.html5Mode()).toBe(true);
4363
let stub = spyOn(service._history, 'pushState');
4464
router.urlRouter.push(makeMatcher('/hello/:name'), { name: 'world' }, {});
4565
expect(stub.calls.first().args[2]).toBe('/hello/world');
4666
});
4767

4868
it('uses history.replaceState when setting a url with replace', () => {
49-
let service = mockHistoryObject();
69+
let service = mockPushState(router);
5070
let stub = spyOn(service._history, 'replaceState');
5171
router.urlRouter.push(makeMatcher('/hello/:name'), { name: 'world' }, { replace: true });
5272
expect(stub.calls.first().args[2]).toBe('/hello/world');
5373
});
5474

55-
it('returns the correct url query', () => {
75+
it('returns the correct url query', async(done) => {
76+
let service = mockPushState(router);
5677
expect(router.urlService.config.html5Mode()).toBe(true);
57-
return router.stateService.go('path', {urlParam: 'bar'}).then(() => {
58-
expect(window.location.toString().includes('/path/bar')).toBe(true);
59-
expect(window.location.toString().includes('/#/path/bar')).toBe(false);
60-
expect(locationProvider.path()).toBe('/path/bar');
61-
expect(locationProvider.search()).toEqual({'':''});
62-
return router.stateService.go('path', {urlParam: 'bar', queryParam: 'query'});
63-
}).then(() => {
64-
expect(window.location.toString().includes('/path/bar?queryParam=query')).toBe(true);
65-
expect(window.location.toString().includes('/#/path/bar?queryParam=query')).toBe(false);
66-
expect(locationProvider.path()).toBe('/path/bar');
67-
expect(locationProvider.search()).toEqual({queryParam:'query'});
68-
});
78+
79+
await router.stateService.go('path', { urlParam: 'bar' });
80+
81+
expect(mockLocation.href.includes('/path/bar')).toBe(true);
82+
expect(mockLocation.href.includes('#')).toBe(false);
83+
84+
expect(urlService.path()).toBe('/path/bar');
85+
expect(urlService.search()).toEqual({});
86+
87+
await router.stateService.go('path', { urlParam: 'bar', queryParam: 'query' });
88+
89+
expect(mockLocation.href.includes('/path/bar?queryParam=query')).toBe(true);
90+
expect(mockLocation.href.includes('#')).toBe(false);
91+
92+
expect(urlService.path()).toBe('/path/bar');
93+
expect(urlService.search()).toEqual({ queryParam: 'query' });
94+
95+
done();
6996
});
7097

7198
});

test/vanilla.hashHistorySpec.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { UIRouter, services, UrlMatcher} from '../src/index';
2-
import * as vanilla from '../src/vanilla';
1+
import { UIRouter, UrlMatcher } from "../src/index";
2+
import * as vanilla from "../src/vanilla";
33

44
describe('hashHistory implementation', () => {
55

@@ -30,17 +30,20 @@ describe('hashHistory implementation', () => {
3030
expect(router.urlService.config.html5Mode()).toBe(false);
3131
});
3232

33-
it('returns the correct url query', (done) => {
34-
return $state.go('path', {urlParam: 'bar'}).then(() => {
35-
expect(window.location.toString().includes('#/path/bar')).toBe(true);
36-
expect(locationProvider.path()).toBe('/path/bar');
37-
expect(locationProvider.search()).toEqual({'':''});
38-
return $state.go('path', {urlParam: 'bar', queryParam: 'query'});
39-
}).then(() => {
40-
expect(window.location.toString().includes('#/path/bar?queryParam=query')).toBe(true);
41-
expect(locationProvider.path()).toBe('/path/bar');
42-
expect(locationProvider.search()).toEqual({queryParam:'query'});
43-
}).then(done);
33+
it('returns the correct url query', async(done) => {
34+
await $state.go('path', { urlParam: 'bar' });
35+
36+
expect(window.location.toString().includes('#/path/bar')).toBe(true);
37+
expect(locationProvider.path()).toBe('/path/bar');
38+
expect(locationProvider.search()).toEqual({});
39+
40+
await $state.go('path', { urlParam: 'bar', queryParam: 'query' });
41+
42+
expect(window.location.toString().includes('#/path/bar?queryParam=query')).toBe(true);
43+
expect(locationProvider.path()).toBe('/path/bar');
44+
expect(locationProvider.search()).toEqual({ queryParam: 'query' });
45+
46+
done();
4447
});
4548

4649
});

0 commit comments

Comments
 (0)