Skip to content

Commit

Permalink
fix(app-router): await navigation result
Browse files Browse the repository at this point in the history
Navigation command (eg. redirect) in child route navigation strategy was not working, when parent route was also using a navigation strategy

Fixes #588
  • Loading branch information
balazsmeszegeto committed Apr 19, 2018
1 parent 71f3e4e commit 3d1c416
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/app-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ function processResult(instruction, result, instructionCount, router) {
}

let finalResult = null;
let navigationCommandResult = null;
if (isNavigationCommand(result.output)) {
result.output.navigate(router);
navigationCommandResult = result.output.navigate(router);
} else {
finalResult = result;

Expand All @@ -211,7 +212,8 @@ function processResult(instruction, result, instructionCount, router) {
}
}

return router._dequeueInstruction(instructionCount + 1)
return Promise.resolve(navigationCommandResult)
.then(_ => router._dequeueInstruction(instructionCount + 1))
.then(innerResult => finalResult || innerResult || result);
}

Expand Down
42 changes: 42 additions & 0 deletions test/app-router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class MockLoader extends RouteLoader {
}
}

class MockInstruction {
constructor(title: string) {
this.title = title;
}
resolve(): void {}
}

describe('app-router', () => {
let router;
let history;
Expand Down Expand Up @@ -246,6 +253,41 @@ describe('app-router', () => {
.then(done);
});
});
describe('instruction completes as navigation command', () => {
it('should complete instructions in order before terminating', done => {
const pipeline = new Pipeline()
.addStep({ run(inst, next) { return pipelineStep(inst, next); } });
spyOn(pipeline, 'run').and.callThrough();

const plProvider = {
createPipeline: () => pipeline
};
const router = new AppRouter(container, history, plProvider, ea);
const initialInstruction = new MockInstruction('initial resulting navigation (Promise)');
const instructionAfterNav = new MockInstruction('instruction after navigation');

const navigationCommand = {
navigate: () => new Promise(resolve => {
setTimeout(() => {
router._queue.push(instructionAfterNav);
pipelineStep = (ctx, next) => next.complete({});
resolve();
}, 0);
})
};

router._queue.push(initialInstruction);
pipelineStep = (ctx, next) => next.complete(navigationCommand);

router._dequeueInstruction()
.then(_ => {
expect(pipeline.run).toHaveBeenCalledTimes(2);
expect(pipeline.run.calls.argsFor(0)).toEqual([initialInstruction]);
expect(pipeline.run.calls.argsFor(1)).toEqual([instructionAfterNav]);
done();
});
});
})
});

function expectSuccess(result) {
Expand Down

0 comments on commit 3d1c416

Please sign in to comment.