Skip to content

Commit 65f10e5

Browse files
christopherthielenmergify[bot]
authored andcommitted
fix(useUiSref): Throw if 'to' isnt a string
1 parent ba397a6 commit 65f10e5

File tree

7 files changed

+345
-480
lines changed

7 files changed

+345
-480
lines changed

src/components/UISref.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import * as PropTypes from 'prop-types';
88

99
import * as _classNames from 'classnames';
1010

11-
import { isFunction, TransitionOptions } from '@uirouter/core';
11+
import { isFunction, isString, TransitionOptions } from '@uirouter/core';
1212

13-
import { UIRouterReact, UIRouterContext, StateRegistry } from '../index';
13+
import { StateRegistry } from '../index';
1414
import { useUIRouter } from './hooks';
1515
import { UIViewAddress, UIViewContext } from './UIView';
16-
import { UISrefActiveContext, AddStateInfoFn } from './UISrefActive';
16+
import { UISrefActiveContext } from './UISrefActive';
1717

1818
/** @hidden */
1919
let classNames = _classNames;
@@ -47,6 +47,9 @@ export function useUISref(to: string, params: { [key: string]: any } = {}, optio
4747
const parentUISrefActiveAddStateInfo = useContext(UISrefActiveContext);
4848

4949
const { stateService, stateRegistry } = router;
50+
if (!isString(to)) {
51+
throw new Error(`State name provided to UISref (as 'to') must be a string.`);
52+
}
5053

5154
useEffect(() => parentUISrefActiveAddStateInfo(to, params), []);
5255

src/components/UISrefActive.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ export interface UISrefActiveState {
2121
/** @hidden */
2222
export type AddStateInfoFn = (to: string, params: { [key: string]: any }) => () => void;
2323

24-
/** @hidden */
25-
export const IncorrectStateNameTypeError = new Error('State name provided to <UISref {to}> must be a string.');
26-
2724
/** @internalapi */
2825
const rootAddStateInfoFn: AddStateInfoFn = () => () => undefined;
2926
export const UISrefActiveContext = React.createContext<AddStateInfoFn>(rootAddStateInfoFn);
Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as React from 'react';
22
import * as PropTypes from 'prop-types';
33
import { mount } from 'enzyme';
4-
4+
import { pushStateLocationPlugin } from '@uirouter/core';
55
import {
66
UIRouter,
77
UIRouterContext,
88
UIRouterReact,
99
memoryLocationPlugin,
10+
ReactStateDeclaration,
11+
servicesPlugin,
1012
} from '../../index';
1113

1214
class Child extends React.Component<any, any> {
@@ -20,21 +22,19 @@ class Child extends React.Component<any, any> {
2022

2123
describe('<UIRouter>', () => {
2224
it('throws an error if no plugin or router instance is passed via prop', () => {
23-
expect(() => {
24-
const wrapper = mount(
25+
expect(() =>
26+
mount(
2527
<UIRouter>
2628
<Child />
2729
</UIRouter>
28-
);
29-
}).toThrow();
30+
)
31+
).toThrow();
3032
});
3133

3234
it('creates a router instance', () => {
3335
const wrapper = mount(
3436
<UIRouter plugins={[memoryLocationPlugin]} states={[]}>
35-
<UIRouterContext.Consumer>
36-
{router => <Child router={router} />}
37-
</UIRouterContext.Consumer>
37+
<UIRouterContext.Consumer>{router => <Child router={router} />}</UIRouterContext.Consumer>
3838
</UIRouter>
3939
);
4040
expect(wrapper.find(Child).props().router).toBeDefined();
@@ -45,32 +45,34 @@ describe('<UIRouter>', () => {
4545
router.plugin(memoryLocationPlugin);
4646
const wrapper = mount(
4747
<UIRouter router={router}>
48-
<UIRouterContext.Consumer>
49-
{router => <Child router={router} />}
50-
</UIRouterContext.Consumer>
48+
<UIRouterContext.Consumer>{router => <Child router={router} />}</UIRouterContext.Consumer>
5149
</UIRouter>
5250
);
5351
expect(wrapper.find(Child).props().router).toBe(router);
5452
});
5553

5654
describe('<UIRouterCosumer>', () => {
5755
it('passes down the router instance', () => {
58-
const wrapper = mount(
56+
let router;
57+
58+
mount(
5959
<UIRouter plugins={[memoryLocationPlugin]}>
6060
<UIRouterContext.Consumer>
61-
{router => {
62-
expect(router).toBeInstanceOf(UIRouterReact);
61+
{_router => {
62+
router = _router;
6363
return null;
6464
}}
6565
</UIRouterContext.Consumer>
6666
</UIRouter>
6767
);
68+
69+
expect(router).toBeInstanceOf(UIRouterReact);
6870
});
6971

7072
it('passes down the correct router instance when passed via props', () => {
7173
const router = new UIRouterReact();
7274
router.plugin(memoryLocationPlugin);
73-
const wrapper = mount(
75+
mount(
7476
<UIRouter router={router}>
7577
<UIRouterContext.Consumer>
7678
{_router => {
@@ -83,3 +85,14 @@ describe('<UIRouter>', () => {
8385
});
8486
});
8587
});
88+
89+
export const makeTestRouter = (states: ReactStateDeclaration[]) => {
90+
const router = new UIRouterReact();
91+
router.plugin(servicesPlugin);
92+
router.plugin(pushStateLocationPlugin);
93+
states.forEach(state => router.stateRegistry.register(state));
94+
95+
const mountInRouter: typeof mount = (children, opts) => mount(<UIRouter router={router}>{children}</UIRouter>, opts);
96+
97+
return { router, mountInRouter };
98+
};

0 commit comments

Comments
 (0)