Skip to content

Commit

Permalink
feat(UIView): warn user when using transition as resolve token
Browse files Browse the repository at this point in the history
  • Loading branch information
elboman committed Dec 17, 2017
1 parent a6c3d76 commit 10b247b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/components/UIView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export interface UIViewState {
props?: any;
}

export const TransitionPropCollisionError = new Error(
'`transition` cannot be used as resolve token. ' +
'Please rename your resolve to avoid conflicts with the router transition.',
);

export class UIView extends Component<UIViewProps, UIViewState> {
// This object contains all the metadata for this UIView
uiViewData: ActiveUIView;
Expand Down Expand Up @@ -237,12 +242,16 @@ export class UIView extends Component<UIViewProps, UIViewState> {

let ctx = new ResolveContext(newConfig.path);
trans = ctx.getResolvable(Transition).data;
let stringTokens = trans
let stringTokens: string[] = trans
.getResolveTokens()
.filter(x => typeof x === 'string');
resolves = stringTokens
.map(token => [token, trans.injector().get(token)])
.reduce(applyPairs, {});

if (stringTokens.indexOf('transition') !== -1) {
throw TransitionPropCollisionError;
}
}

this.uiViewData.config = newConfig;
Expand Down
20 changes: 20 additions & 0 deletions src/components/__tests__/UIView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ReactStateDeclaration,
memoryLocationPlugin,
servicesPlugin,
TransitionPropCollisionError,
} from '../../index';

const states = [
Expand Down Expand Up @@ -156,6 +157,25 @@ describe('<UIView>', () => {
});
});

it('throws if a resolve uses the token `transition`', async () => {
const Comp = () => <span>component</span>;
router.stateRegistry.register({
name: '__state',
component: Comp,
resolve: [{ token: 'transition', resolveFn: () => null }],
});

await router.stateService.go('__state');

expect(() => {
const wrapper = mount(
<UIRouter router={router}>
<UIView />
</UIRouter>,
);
}).toThrow(TransitionPropCollisionError);
});

it('renders nested State Components', () => {
return router.stateService.go('parent.child').then(() => {
const wrapper = mount(
Expand Down

0 comments on commit 10b247b

Please sign in to comment.