Skip to content

Commit 12cd951

Browse files
oBuskwawyed
andauthored
fix(stateService): make sure $state.href (and by extension uiSref) handle inherit: false (#834)
* Add tests for `$state.href()` * Make `stateParams.$inherit()` handle `inherit: false` * Update mergify configuration as per #834 (comment) * Ignore ajv for checking peerdependencies per #834 (comment) * Bump `@uirouter/publish-scripts` to `2.6.0` * Ignore uirouter/core when checking peerdeps * Update package.json * Update package.json * Clarification by naming of variables * Keep parentParams variables within ffor-in closure Co-authored-by: wawyed <[email protected]>
1 parent bfaabc5 commit 12cd951

File tree

5 files changed

+204
-55
lines changed

5 files changed

+204
-55
lines changed

.mergify.yml

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
queue_rules:
2+
- name: default
3+
conditions:
4+
- check-success=ci
5+
16
pull_request_rules:
27
- name: Auto Squash and Merge
38
conditions:
@@ -6,16 +11,16 @@ pull_request_rules:
611
- 'label=ready to squash and merge'
712
actions:
813
delete_head_branch: {}
9-
merge:
14+
queue:
1015
method: squash
11-
strict: smart
16+
name: default
1217
- name: Auto Rebase and Merge
1318
conditions:
1419
- base=master
1520
- status-success=ci
1621
- 'label=ready to rebase and merge'
1722
actions:
1823
delete_head_branch: {}
19-
merge:
24+
queue:
2025
method: rebase
21-
strict: smart
26+
name: default

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@uirouter/core",
33
"description": "UI-Router Core: Framework agnostic, State-based routing for JavaScript Single Page Apps",
4-
"version": "6.0.8",
4+
"version": "6.0.9",
55
"scripts": {
66
"clean": "shx rm -rf lib lib-esm _bundles .cache _doc",
77
"compile": "npm run clean && tsc && tsc -m es6 --outDir lib-esm && shx cp src/*.json lib",
@@ -68,7 +68,7 @@
6868
"devDependencies": {
6969
"@types/jasmine": "^3.3.13",
7070
"@types/jquery": "^3.3.36",
71-
"@uirouter/publish-scripts": "^2.5.5",
71+
"@uirouter/publish-scripts": "^2.6.0",
7272
"bufferutil": "4.0.2",
7373
"dts-downlevel": "^0.4.0",
7474
"fork-ts-checker-webpack-plugin": "^6.0.8",

src/params/stateParams.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Param } from '.';
12
import { extend, ancestors, Obj } from '../common/common';
23
import { StateObject } from '../state/stateObject';
34

@@ -17,20 +18,21 @@ export class StateParams {
1718
* @param {Object} $to Internal definition of object representing state to transition to.
1819
*/
1920
$inherit(newParams: Obj, $current: StateObject, $to: StateObject) {
20-
let parentParams: string[];
2121
const parents = ancestors($current, $to),
2222
inherited: Obj = {},
2323
inheritList: string[] = [];
2424

2525
for (const i in parents) {
2626
if (!parents[i] || !parents[i].params) continue;
27-
parentParams = Object.keys(parents[i].params);
28-
if (!parentParams.length) continue;
27+
const parentParams = parents[i].params;
28+
const parentParamsKeys = Object.keys(parentParams);
29+
if (!parentParamsKeys.length) continue;
2930

30-
for (const j in parentParams) {
31-
if (inheritList.indexOf(parentParams[j]) >= 0) continue;
32-
inheritList.push(parentParams[j]);
33-
inherited[parentParams[j]] = this[parentParams[j]];
31+
for (const j in parentParamsKeys) {
32+
if (parentParams[parentParamsKeys[j]].inherit == false || inheritList.indexOf(parentParamsKeys[j]) >= 0)
33+
continue;
34+
inheritList.push(parentParamsKeys[j]);
35+
inherited[parentParamsKeys[j]] = this[parentParamsKeys[j]];
3436
}
3537
}
3638
return extend({}, inherited, newParams);

test/stateServiceSpec.ts

+65
Original file line numberDiff line numberDiff line change
@@ -1165,5 +1165,70 @@ describe('stateService', function() {
11651165
.then(expectNone, expectNone)
11661166
.then(done);
11671167
});
1168+
1169+
describe('.href()', () => {
1170+
beforeEach(async () => {
1171+
const foo: StateDeclaration = {
1172+
name: 'foo',
1173+
url: '/foo',
1174+
};
1175+
const bar: StateDeclaration = {
1176+
parent: 'foo',
1177+
name: 'bar',
1178+
url: '/bar/:path?query1&query2',
1179+
params: {
1180+
query1: { inherit: false, value: null },
1181+
},
1182+
};
1183+
1184+
$registry.register(foo);
1185+
$registry.register(bar);
1186+
1187+
await initStateTo(bar, {
1188+
path: 'originalPath',
1189+
query1: 'originalQuery1',
1190+
query2: 'originalQuery2',
1191+
});
1192+
});
1193+
1194+
it('should create a href', () => {
1195+
expect(
1196+
$state.href('bar', {
1197+
path: 'example',
1198+
query1: 'aa',
1199+
query2: 'bb',
1200+
})
1201+
).toEqual('#/foo/bar/example?query1=aa&query2=bb');
1202+
});
1203+
1204+
it('shold create a href with relative', () => {
1205+
expect($state.href('^', null, { relative: 'bar' })).toEqual('#/foo');
1206+
});
1207+
1208+
it('should create an absolute url', () => {
1209+
expect(
1210+
$state.href(
1211+
'bar',
1212+
{
1213+
path: 'example',
1214+
query1: 'aa',
1215+
query2: 'bb',
1216+
},
1217+
{ absolute: true }
1218+
)
1219+
).toEqual('http://localhost/#/foo/bar/example?query1=aa&query2=bb');
1220+
});
1221+
1222+
it('should handle inherit: false params', () => {
1223+
expect(
1224+
$state.href('bar', {
1225+
path: 'newpath',
1226+
})
1227+
// Path has new value
1228+
// Query1 should not be inherited
1229+
// Query2 should be inherited
1230+
).toEqual('#/foo/bar/newpath?query2=originalQuery2');
1231+
});
1232+
});
11681233
});
11691234
});

0 commit comments

Comments
 (0)