Skip to content

Commit

Permalink
chore(all): sync with master
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed Oct 15, 2018
1 parent 0dcfdf1 commit 3b58778
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/activation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ function findDeactivatable(
let viewPortPlan = plan[viewPortName];
let prevComponent = viewPortPlan.prevComponent;

if ((viewPortPlan.strategy === activationStrategy.invokeLifecycle ||
viewPortPlan.strategy === activationStrategy.replace) &&
prevComponent) {
if ((viewPortPlan.strategy === activationStrategy.invokeLifecycle ||viewPortPlan.strategy === activationStrategy.replace)
&& prevComponent
) {
let viewModel = prevComponent.viewModel;

if (callbackName in viewModel) {
Expand Down
94 changes: 78 additions & 16 deletions test/activation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
CanDeactivatePreviousStep,
CanActivateNextStep,
ActivateNextStep,
NavigationInstructionInit
NavigationInstructionInit,
ViewPortPlan
} from '../src';
import { ValueOf, createPipelineState, MockPipelineState, MockInstruction } from './shared';

Expand All @@ -20,49 +21,72 @@ describe('activation', () => {
let viewPortFactory = (
resultHandler: (val?: any) => any,
strategy: ValueOf<ActivationStrategy> = activationStrategy.invokeLifecycle
): ViewPortInstruction => {
): ViewPortPlan => {
return {
strategy: strategy,
prevComponent: { viewModel: { canDeactivate: resultHandler }, childRouter: null as Router }
} as any;
};

let instructionFactory = (instruction: NavigationInstruction): NavigationInstruction => {
return Object.assign({ router: {} }, instruction);
};

beforeEach(() => {
step = new CanDeactivatePreviousStep();
state = createPipelineState();
});

it('should return true for context that canDeactivate', () => {
let instruction: NavigationInstruction = { plan: { first: viewPortFactory(() => (true)) } } as any;
let instruction: NavigationInstruction = instructionFactory({ plan: { first: viewPortFactory(() => (true)) } } as any);

step.run(instruction, state.next);
expect(state.result).toBe(true);
});

it('should return true for context that canDeactivate with activationStrategy.replace', () => {
let instruction: NavigationInstruction = { plan: { first: viewPortFactory(() => (true), activationStrategy.replace) } } as any;
let instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPortFactory(
() => (true),
activationStrategy.replace
)
}
} as any);

step.run(instruction, state.next);
expect(state.result).toBe(true);
});

it('should cancel for context that cannot Deactivate', () => {
let instruction: NavigationInstruction = { plan: { first: viewPortFactory(() => (false)) } } as any;
let instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPortFactory(() => (false))
}
} as any);

step.run(instruction, state.next);
expect(state.rejection).toBeTruthy();
});

it('should return true for context that cannot Deactivate with unknown strategy', () => {
let instruction: NavigationInstruction = { plan: { first: viewPortFactory(() => (false), 'unknown' as any) } } as any;
let instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPortFactory(() => (false), 'unknown' as any)
}
} as any);

step.run(instruction, state.next);
expect(state.result).toBe(true);
});


it('should return true for context that canDeactivate with a promise', (done) => {
let instruction: NavigationInstruction = { plan: { first: viewPortFactory(() => (Promise.resolve(true))) } } as any;
let instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPortFactory(() => (Promise.resolve(true)))
}
} as any);

step.run(instruction, state.next).then(() => {
expect(state.result).toBe(true);
Expand All @@ -71,7 +95,11 @@ describe('activation', () => {
});

it('should cancel for context that cantDeactivate with a promise', (done) => {
let instruction: NavigationInstruction = { plan: { first: viewPortFactory(() => (Promise.resolve(false))) } } as any;
let instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPortFactory(() => (Promise.resolve(false)))
}
} as any);

step.run(instruction, state.next).then(() => {
expect(state.rejection).toBeTruthy();
Expand All @@ -80,7 +108,11 @@ describe('activation', () => {
});

it('should cancel for context that throws in canDeactivate', (done) => {
let instruction: NavigationInstruction = { plan: { first: viewPortFactory(() => { throw new Error('oops'); }) } } as any;
let instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPortFactory(() => { throw new Error('oops'); })
}
} as any);

step.run(instruction, state.next).then(() => {
expect(state.rejection).toBeTruthy();
Expand All @@ -89,21 +121,35 @@ describe('activation', () => {
});

it('should return true when all plans return true', () => {
let instruction: NavigationInstruction = { plan: { first: viewPortFactory(() => (true)), second: viewPortFactory(() => (true)) } } as any;
let instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPortFactory(() => (true)),
second: viewPortFactory(() => (true))
}
} as any);

step.run(instruction, state.next);
expect(state.result).toBe(true);
});

it('should cancel when some plans return false', () => {
let instruction: NavigationInstruction = { plan: { first: viewPortFactory(() => (true)), second: viewPortFactory(() => (false)) } } as any;
let instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPortFactory(() => (true)),
second: viewPortFactory(() => (false))
}
} as any);

step.run(instruction, state.next);
expect(state.rejection).toBeTruthy();
});

it('should pass a navigationInstruction to the callback function', () => {
const instruction: NavigationInstruction = { plan: { first: viewPortFactory(() => (true)) } } as any;
const instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPortFactory(() => (true))
}
} as any);
const viewModel = instruction.plan.first.prevComponent.viewModel;
spyOn(viewModel, 'canDeactivate').and.callThrough();
step.run(instruction, state.next);
Expand All @@ -124,18 +170,34 @@ describe('activation', () => {

it('should return true when the currentInstruction can deactivate', () => {
let viewPort = viewPortFactory(() => (true), activationStrategy.replace) as any;
let currentInstruction: NavigationInstruction = { viewPortInstructions: { first: viewPortInstructionFactory(() => (true)) } } as any;
let currentInstruction: NavigationInstruction = instructionFactory({
viewPortInstructions: {
first: viewPortInstructionFactory(() => (true))
}
} as any);
viewPort.prevComponent.childRouter = <Router>({ currentInstruction } as any);
let instruction: NavigationInstruction = { plan: { first: viewPort } } as any;
let instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPort
}
} as any);
step.run(instruction, state.next);
expect(state.result).toBe(true);
});

it('should cancel when router instruction cannot deactivate', () => {
let viewPort = viewPortFactory(() => (true), activationStrategy.replace);
let currentInstruction: NavigationInstruction = { viewPortInstructions: { first: viewPortInstructionFactory(() => (false)) } } as any;
let currentInstruction: NavigationInstruction = instructionFactory({
viewPortInstructions: {
first: viewPortInstructionFactory(() => (false))
}
} as any);
viewPort.prevComponent.childRouter = <Router>({ currentInstruction } as any);
let instruction: NavigationInstruction = { plan: { first: viewPort } } as any;
let instruction: NavigationInstruction = instructionFactory({
plan: {
first: viewPort
}
} as any);
step.run(instruction, state.next);
expect(state.rejection).toBeTruthy();
});
Expand Down

0 comments on commit 3b58778

Please sign in to comment.