Skip to content

Commit 170b895

Browse files
test($state): non-url 'any' object params, json params, array params
1 parent 3bfd75a commit 170b895

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed

test/stateSpec.js

+75-3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ describe('state', function () {
105105
url: "/bad2/{param:[0-9]{5}}"
106106
})
107107

108+
.state('json', { url: '/jsonstate/{param:json}' })
109+
108110
.state('first', { url: '^/first/subpath' })
109111
.state('second', { url: '^/second' })
110112

@@ -268,13 +270,13 @@ describe('state', function () {
268270
}));
269271

270272
it('can lazy-define a state in $stateNotFound', inject(function ($state, $q, $rootScope) {
271-
initStateTo(DD, { x: 1, y: 2, z: 3 });
273+
initStateTo(DD, { x: "1", y: "2", z: "3" });
272274
var called;
273275
$rootScope.$on('$stateNotFound', function (ev, redirect) {
274276
stateProvider.state(redirect.to, { parent: DD, params: { x: {}, y: {}, z: {}, w: {} }});
275277
called = true;
276278
});
277-
var promise = $state.go('DDD', { w: 4 });
279+
var promise = $state.go('DDD', { w: "4" });
278280
$q.flush();
279281
expect(called).toBeTruthy();
280282
expect($state.current.name).toEqual('DDD');
@@ -289,7 +291,7 @@ describe('state', function () {
289291
ev.retry = deferred.promise;
290292
called = true;
291293
});
292-
var promise = $state.go('AA', { a: 1 });
294+
var promise = $state.go('AA', { a: "1" });
293295
stateProvider.state('AA', { parent: A, params: { a: {} }});
294296
deferred.resolve();
295297
$q.flush();
@@ -780,6 +782,7 @@ describe('state', function () {
780782
'home',
781783
'home.item',
782784
'home.redirect',
785+
'json',
783786
'resolveFail',
784787
'resolveTimeout',
785788
'root',
@@ -819,6 +822,18 @@ describe('state', function () {
819822
expect(stateParams).toEqual({ param: "100" });
820823
}));
821824

825+
it("should allow null default value for non-url params", inject(function($state, $q) {
826+
$state.go("D"); $q.flush();
827+
expect($state.current.name).toBe("D");
828+
expect($state.params).toEqual({ x: null, y: null });
829+
}));
830+
831+
it("should allow arbitrary objects to pass for non-url params", inject(function($state, $q) {
832+
$state.go("D", { x: 100, y: { foo: 'bar' } }); $q.flush();
833+
expect($state.current.name).toBe("D");
834+
expect($state.params).toEqual({ x: 100, y: { foo: 'bar' } });
835+
}));
836+
822837
it("should be populated during primary transition, if unspecified", inject(function($state, $q) {
823838
var count = 0;
824839
$state.get("OPT").onEnter = function($stateParams) { count++; };
@@ -911,6 +926,24 @@ describe('state', function () {
911926
}));
912927

913928
describe("typed parameter handling", function() {
929+
beforeEach(function () {
930+
stateProvider.state({
931+
name: "types",
932+
url: "/types/{p1:string}/{p2:date}",
933+
params: {
934+
p1: { value: [ "defaultValue" ], array: true },
935+
p2: new Date(2014, 10, 15),
936+
nonurl: null
937+
}
938+
});
939+
stateProvider.state({
940+
name: "types.substate",
941+
url: "/sub/{p3[]:int}/{p4:json}?{p5:bool}",
942+
params: {
943+
"p3[]": [ 10 ]
944+
}
945+
});
946+
});
914947

915948
it('should initialize parameters without a hacky empty test', inject(function ($urlMatcherFactory, $state) {
916949
new UrlMatcher("");
@@ -954,6 +987,45 @@ describe('state', function () {
954987
$rootScope.$apply();
955988
expect($state.current.name).toBe("about");
956989
}));
990+
991+
function expectStateUrlMappingFn($state, $rootScope, $q, $location) {
992+
return function (state, url, params, defaults, nonurlparams) {
993+
$state.go(state, extend({}, nonurlparams, params));
994+
$q.flush();
995+
996+
expect($state.current.name).toBe(state.name || state); // allow object
997+
expect($state.params).toEqual(extend({}, defaults, params, nonurlparams));
998+
expect($location.url()).toBe(url);
999+
1000+
initStateTo(A);
1001+
1002+
$location.url(url);
1003+
$rootScope.$broadcast("$locationChangeSuccess");
1004+
$q.flush();
1005+
1006+
expect($state.current.name).toBe(state.name || state); // allow object
1007+
expect($state.params).toEqual(extend({}, defaults, params));
1008+
expect($location.url()).toBe(url);
1009+
}
1010+
}
1011+
1012+
it('should map to/from the $location.url() and $stateParams', inject(function($state, $location, $q, $rootScope) {
1013+
var nov15 = new Date(2014,10,15);
1014+
var defaults = { p1: [ 'defaultValue' ], p2: nov15, nonurl: null };
1015+
var params = { p1: [ "foo" ], p2: nov15 };
1016+
var nonurl = { nonurl: { foo: 'bar' } };
1017+
1018+
var check = expectStateUrlMappingFn($state, $rootScope, $q, $location);
1019+
check('types', '/types/defaultValue/2014-11-15', { }, defaults);
1020+
check('types', "/types/foo/2014-11-15", params, defaults, nonurl);
1021+
1022+
extend(defaults, { "p3[]": [ 10 ] });
1023+
extend(params, { p4: { baz: "qux" }});
1024+
check('types.substate', "/types/foo/2014-11-15/sub/10/%7B%22baz%22:%22qux%22%7D", params, defaults, nonurl);
1025+
1026+
extend(params, { p5: true });
1027+
check('types.substate', "/types/foo/2014-11-15/sub/10/%7B%22baz%22:%22qux%22%7D?p5=1", params, defaults, nonurl);
1028+
}));
9571029
});
9581030

9591031
it('should revert to last known working url on state change failure', inject(function ($state, $rootScope, $location, $q) {

test/urlMatcherFactorySpec.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,26 @@ describe("urlMatcherFactory", function () {
481481
it("should encode/decode dates", function () {
482482
var m = new UrlMatcher("/calendar/{date:date}"),
483483
result = m.exec("/calendar/2014-03-26");
484+
var date = new Date(2014, 2, 26);
484485

485486
expect(result.date instanceof Date).toBe(true);
486-
expect(result.date.toUTCString()).toEqual('Wed, 26 Mar 2014 00:00:00 GMT');
487-
expect(m.format({ date: new Date(2014, 2, 26) })).toBe("/calendar/2014-03-26");
487+
expect(result.date.toUTCString()).toEqual(date.toUTCString());
488+
expect(m.format({ date: date })).toBe("/calendar/2014-03-26");
489+
});
490+
491+
it("should encode/decode arbitrary objects to json", function () {
492+
var m = new UrlMatcher("/state/{param1:json}/{param2:json}");
493+
494+
var params = {
495+
param1: { foo: 'huh', count: 3 },
496+
param2: { foo: 'wha', count: 5 }
497+
};
498+
499+
var json1 = '{"foo":"huh","count":3}';
500+
var json2 = '{"foo":"wha","count":5}';
501+
502+
expect(m.format(params)).toBe("/state/" + encodeURIComponent(json1) + "/" + encodeURIComponent(json2));
503+
expect(m.exec("/state/" + json1 + "/" + json2)).toEqual(params);
488504
});
489505

490506
it("should not match invalid typed parameter values", function() {

0 commit comments

Comments
 (0)