Skip to content

Commit bedbf4e

Browse files
authored
Merge pull request #277 from tildeio/fix-substates
Fix transition being undefined through substates
2 parents 4b09c3c + fad4cc2 commit bedbf4e

File tree

2 files changed

+106
-9
lines changed

2 files changed

+106
-9
lines changed

lib/router/router.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,10 @@ export default abstract class Router<T extends Route> {
227227
}
228228

229229
if (isIntermediate) {
230+
let transition = new InternalTransition(this, undefined, undefined);
231+
this.toReadOnlyInfos(transition, newState);
230232
this.setupContexts(newState);
231-
let transition = this.activeTransition!;
232-
if (transition !== undefined && !transition.isCausedByAbortingTransition) {
233-
transition = new InternalTransition(this, undefined, undefined);
234-
transition.from = transition.from;
235-
}
236233

237-
this.toInfos(transition, newState.routeInfos);
238234
this.routeWillChange(transition);
239235
return this.activeTransition!;
240236
}
@@ -773,7 +769,7 @@ export default abstract class Router<T extends Route> {
773769
}
774770
}
775771

776-
private toInfos(
772+
public toInfos(
777773
newTransition: OpaqueTransition,
778774
newRouteInfos: InternalRouteInfo<T>[],
779775
includeAttributes = false

tests/router_test.ts

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ scenarios.forEach(function(scenario) {
14141414
});
14151415

14161416
test('abort route events', function(assert) {
1417-
assert.expect(20);
1417+
assert.expect(19);
14181418
map(assert, function(match) {
14191419
match('/').to('index');
14201420
match('/posts', function(match) {
@@ -1465,7 +1465,6 @@ scenarios.forEach(function(scenario) {
14651465
} else if (aborted) {
14661466
assert.equal(transition.isAborted, true);
14671467
assert.equal(transition.to, transition.from);
1468-
assert.equal(transition.to, transition.from);
14691468
assert.equal(transition.to!.localName, 'index');
14701469
} else {
14711470
assert.equal(transition.to!.localName, 'post');
@@ -1501,6 +1500,107 @@ scenarios.forEach(function(scenario) {
15011500
});
15021501
});
15031502

1503+
test('always has a transition through the substates', function(assert) {
1504+
map(assert, function(match) {
1505+
match('/').to('index');
1506+
match('/posts', function(match) {
1507+
match('/:id').to('post');
1508+
match('/details').to('postDetails');
1509+
});
1510+
match('/foo', function(match) {
1511+
match('/').to('foo', function(match) {
1512+
match('/bar').to('bar');
1513+
});
1514+
});
1515+
match('/err').to('fooError');
1516+
});
1517+
1518+
let enterSubstate = false;
1519+
let initial = true;
1520+
let isAborted = false;
1521+
let errorHandled = false;
1522+
let enteredWillChange = 0;
1523+
let enteredDidChange = 0;
1524+
1525+
routes = {
1526+
post: createHandler('post', {
1527+
beforeModel(transition: Transition) {
1528+
isAborted = true;
1529+
transition.abort();
1530+
enterSubstate = true;
1531+
router.intermediateTransitionTo('fooError');
1532+
},
1533+
}),
1534+
foo: createHandler('foo'),
1535+
};
1536+
1537+
router.transitionDidError = (error: TransitionError, transition: Transition) => {
1538+
if (error.wasAborted || transition.isAborted) {
1539+
return logAbort(transition);
1540+
} else {
1541+
transition.trigger(false, 'error', error.error, transition, error.route);
1542+
if (errorHandled) {
1543+
transition.rollback();
1544+
router.routeDidChange(transition);
1545+
return transition;
1546+
} else {
1547+
transition.abort();
1548+
return error.error;
1549+
}
1550+
}
1551+
};
1552+
1553+
router.routeWillChange = (transition: Transition) => {
1554+
enteredWillChange++;
1555+
if (initial) {
1556+
assert.equal(transition.to!.localName, 'index', 'initial');
1557+
assert.equal(transition.from!, null, 'initial');
1558+
assert.equal(transition.to!.parent, null, 'initial');
1559+
} else if (isAborted) {
1560+
assert.equal(transition.to!.localName, 'index', 'aborted');
1561+
assert.equal(isPresent(transition.from) && transition.from!.localName, 'index', 'aborted');
1562+
} else if (enterSubstate) {
1563+
assert.equal(transition.to!.localName, 'fooError', 'substate');
1564+
assert.equal(isPresent(transition.from) && transition.from!.localName, 'index', 'substate');
1565+
assert.equal(transition.to!.parent!.localName, 'foo', 'substate');
1566+
} else {
1567+
assert.equal(transition.to!.localName, 'post', 'to post');
1568+
assert.equal(isPresent(transition.from) && transition.from!.localName, 'index', 'to post');
1569+
assert.equal(transition.to!.parent, null, 'to post');
1570+
}
1571+
};
1572+
1573+
router.routeDidChange = (transition: Transition) => {
1574+
enteredDidChange++;
1575+
if (initial) {
1576+
assert.equal(transition.to!.localName, 'index', 'initial');
1577+
assert.equal(transition.from!, null, 'initial');
1578+
initial = false;
1579+
} else if (isAborted) {
1580+
assert.equal(transition.to!.localName, 'index', 'aborted');
1581+
assert.equal(isPresent(transition.from) && transition.from!.localName, 'index', 'aborted');
1582+
isAborted = false;
1583+
} else {
1584+
assert.equal(transition.to!.localName, 'bar');
1585+
assert.equal(isPresent(transition.from) && transition.from!.localName, 'index');
1586+
}
1587+
};
1588+
1589+
router
1590+
.transitionTo('/')
1591+
.then(() => {
1592+
return router.transitionTo('/posts/1');
1593+
})
1594+
.catch((err: any) => {
1595+
assert.equal(err.name, 'TransitionAborted');
1596+
return router.activeTransition as any;
1597+
})
1598+
.finally(() => {
1599+
assert.equal(enteredWillChange, 4);
1600+
assert.equal(enteredDidChange, 2);
1601+
});
1602+
});
1603+
15041604
test('error route events', function(assert) {
15051605
map(assert, function(match) {
15061606
match('/').to('index');
@@ -1555,6 +1655,7 @@ scenarios.forEach(function(scenario) {
15551655
transition.trigger(false, 'error', error.error, transition, error.route);
15561656
if (errorHandled) {
15571657
transition.rollback();
1658+
router.toInfos(transition, router.state!.routeInfos, true);
15581659
router.routeDidChange(transition);
15591660
return transition;
15601661
} else {

0 commit comments

Comments
 (0)