Skip to content

Commit

Permalink
fix(stateService): make sure $state.href (and by extension uiSref
Browse files Browse the repository at this point in the history
…) 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 <wawyed@gmail.com>
  • Loading branch information
oBusk and wawyed committed Jan 12, 2023
1 parent bfaabc5 commit 12cd951
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 55 deletions.
13 changes: 9 additions & 4 deletions .mergify.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
queue_rules:
- name: default
conditions:
- check-success=ci

pull_request_rules:
- name: Auto Squash and Merge
conditions:
Expand All @@ -6,16 +11,16 @@ pull_request_rules:
- 'label=ready to squash and merge'
actions:
delete_head_branch: {}
merge:
queue:
method: squash
strict: smart
name: default
- name: Auto Rebase and Merge
conditions:
- base=master
- status-success=ci
- 'label=ready to rebase and merge'
actions:
delete_head_branch: {}
merge:
queue:
method: rebase
strict: smart
name: default
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@uirouter/core",
"description": "UI-Router Core: Framework agnostic, State-based routing for JavaScript Single Page Apps",
"version": "6.0.8",
"version": "6.0.9",
"scripts": {
"clean": "shx rm -rf lib lib-esm _bundles .cache _doc",
"compile": "npm run clean && tsc && tsc -m es6 --outDir lib-esm && shx cp src/*.json lib",
Expand Down Expand Up @@ -68,7 +68,7 @@
"devDependencies": {
"@types/jasmine": "^3.3.13",
"@types/jquery": "^3.3.36",
"@uirouter/publish-scripts": "^2.5.5",
"@uirouter/publish-scripts": "^2.6.0",
"bufferutil": "4.0.2",
"dts-downlevel": "^0.4.0",
"fork-ts-checker-webpack-plugin": "^6.0.8",
Expand Down
16 changes: 9 additions & 7 deletions src/params/stateParams.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Param } from '.';
import { extend, ancestors, Obj } from '../common/common';
import { StateObject } from '../state/stateObject';

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

for (const i in parents) {
if (!parents[i] || !parents[i].params) continue;
parentParams = Object.keys(parents[i].params);
if (!parentParams.length) continue;
const parentParams = parents[i].params;
const parentParamsKeys = Object.keys(parentParams);
if (!parentParamsKeys.length) continue;

for (const j in parentParams) {
if (inheritList.indexOf(parentParams[j]) >= 0) continue;
inheritList.push(parentParams[j]);
inherited[parentParams[j]] = this[parentParams[j]];
for (const j in parentParamsKeys) {
if (parentParams[parentParamsKeys[j]].inherit == false || inheritList.indexOf(parentParamsKeys[j]) >= 0)
continue;
inheritList.push(parentParamsKeys[j]);
inherited[parentParamsKeys[j]] = this[parentParamsKeys[j]];
}
}
return extend({}, inherited, newParams);
Expand Down
65 changes: 65 additions & 0 deletions test/stateServiceSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,5 +1165,70 @@ describe('stateService', function() {
.then(expectNone, expectNone)
.then(done);
});

describe('.href()', () => {
beforeEach(async () => {
const foo: StateDeclaration = {
name: 'foo',
url: '/foo',
};
const bar: StateDeclaration = {
parent: 'foo',
name: 'bar',
url: '/bar/:path?query1&query2',
params: {
query1: { inherit: false, value: null },
},
};

$registry.register(foo);
$registry.register(bar);

await initStateTo(bar, {
path: 'originalPath',
query1: 'originalQuery1',
query2: 'originalQuery2',
});
});

it('should create a href', () => {
expect(
$state.href('bar', {
path: 'example',
query1: 'aa',
query2: 'bb',
})
).toEqual('#/foo/bar/example?query1=aa&query2=bb');
});

it('shold create a href with relative', () => {
expect($state.href('^', null, { relative: 'bar' })).toEqual('#/foo');
});

it('should create an absolute url', () => {
expect(
$state.href(
'bar',
{
path: 'example',
query1: 'aa',
query2: 'bb',
},
{ absolute: true }
)
).toEqual('http://localhost/#/foo/bar/example?query1=aa&query2=bb');
});

it('should handle inherit: false params', () => {
expect(
$state.href('bar', {
path: 'newpath',
})
// Path has new value
// Query1 should not be inherited
// Query2 should be inherited
).toEqual('#/foo/bar/newpath?query2=originalQuery2');
});
});
});
});
Loading

0 comments on commit 12cd951

Please sign in to comment.