Skip to content

Commit

Permalink
test(modal): adds canLeave test
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Oct 12, 2016
1 parent 58d09ed commit 1300cbd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
45 changes: 44 additions & 1 deletion src/components/modal/test/basic/app-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export class E2EPage {
</ion-item>
</ion-list>
<button ion-button full (click)="submit()">Submit</button>
<p>ionViewCanEnter ({{called.ionViewCanEnter}})</p>
<p>ionViewCanLeave ({{called.ionViewCanLeave}})</p>
<p>ionViewDidLoad ({{called.ionViewDidLoad}})</p>
<p>ionViewWillEnter ({{called.ionViewWillEnter}})</p>
<p>ionViewDidEnter ({{called.ionViewDidEnter}})</p>
Expand All @@ -146,6 +148,7 @@ export class ModalPassData {
constructor(
public viewCtrl: ViewController,
public toastCtrl: ToastController,
public alertCtrl: AlertController,
params: NavParams,
someComponentProvider: SomeComponentProvider,
someAppProvider: SomeAppProvider) {
Expand All @@ -156,6 +159,8 @@ export class ModalPassData {
console.log('SomeAppProvider Data', someAppProvider.getData());

this.called = {
ionViewCanEnter: 0,
ionViewCanLeave: 0,
ionViewDidLoad: 0,
ionViewWillEnter: 0,
ionViewDidEnter: 0,
Expand All @@ -166,7 +171,29 @@ export class ModalPassData {

submit() {
console.time('modal');
this.viewCtrl.dismiss(this.data);
this.viewCtrl.dismiss(this.data).catch(() => {
console.log('submit was cancelled');
});
}

ionViewCanEnter() {
console.log('ModalPassData ionViewCanEnter fired');
this.called.ionViewCanEnter++;

return true;
}

ionViewCanLeave() {
console.log('ModalPassData ionViewCanLeave fired');
this.called.ionViewCanLeave++;

return new Promise((resolve: any, reject: any) => {
let alert = this.alertCtrl.create();
alert.setTitle('Do you want to submit?');
alert.addButton({ text: 'Submit', handler: resolve });
alert.addButton({ text: 'Cancel', role: 'cancel', handler: reject });
alert.present();
});
}

ionViewDidLoad() {
Expand Down Expand Up @@ -360,6 +387,8 @@ export class ContactUs {
<p>
<button ion-button (click)="openActionSheet()">Open Action Sheet</button>
</p>
<p>ionViewCanEnter ({{called.ionViewCanEnter}})</p>
<p>ionViewCanLeave ({{called.ionViewCanLeave}})</p>
<p>ionViewDidLoad ({{called.ionViewDidLoad}})</p>
<p>ionViewWillEnter ({{called.ionViewWillEnter}})</p>
<p>ionViewDidEnter ({{called.ionViewDidEnter}})</p>
Expand Down Expand Up @@ -391,6 +420,8 @@ export class ModalFirstPage {
}

this.called = {
ionViewCanEnter: 0,
ionViewCanLeave: 0,
ionViewDidLoad: 0,
ionViewWillEnter: 0,
ionViewDidEnter: 0,
Expand All @@ -410,6 +441,18 @@ export class ModalFirstPage {
this.navCtrl.parent.pop();
}

ionViewCanEnter() {
console.log('ModalFirstPage ionViewCanEnter fired');
this.called.ionViewCanEnter++;
return true;
}

ionViewCanLeave() {
console.log('ModalFirstPage ionViewCanLeave fired');
this.called.ionViewCanLeave++;
return true;
}

ionViewDidLoad() {
console.log('ModalFirstPage ionViewDidLoad fired');
this.called.ionViewDidLoad++;
Expand Down
47 changes: 15 additions & 32 deletions src/navigation/nav-controller-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,19 @@ export class NavControllerBase extends Ion implements NavController {
_viewport: ViewContainerRef;
_views: ViewController[] = [];

viewDidLoad: EventEmitter<any>;
viewWillEnter: EventEmitter<any>;
viewDidEnter: EventEmitter<any>;
viewWillLeave: EventEmitter<any>;
viewDidLeave: EventEmitter<any>;
viewWillUnload: EventEmitter<any>;
viewDidLoad: EventEmitter<any> = new EventEmitter();
viewWillEnter: EventEmitter<any> = new EventEmitter();
viewDidEnter: EventEmitter<any> = new EventEmitter();
viewWillLeave: EventEmitter<any> = new EventEmitter();
viewDidLeave: EventEmitter<any> = new EventEmitter();
viewWillUnload: EventEmitter<any> = new EventEmitter();

id: string;
parent: any;
config: Config;

constructor(
parent: any,
public parent: any,
public _app: App,
config: Config,
public config: Config,
public _keyboard: Keyboard,
elementRef: ElementRef,
public _zone: NgZone,
Expand All @@ -64,20 +62,10 @@ export class NavControllerBase extends Ion implements NavController {
) {
super(config, elementRef, renderer);

this.parent = parent;
this.config = config;

this._sbEnabled = config.getBoolean('swipeBackEnabled');
this._sbThreshold = config.getNumber('swipeBackThreshold', 40);

this.id = 'n' + (++ctrlIds);

this.viewDidLoad = new EventEmitter();
this.viewWillEnter = new EventEmitter();
this.viewDidEnter = new EventEmitter();
this.viewWillLeave = new EventEmitter();
this.viewDidLeave = new EventEmitter();
this.viewWillUnload = new EventEmitter();
}

push(page: any, params?: any, opts?: NavOptions, done?: Function): Promise<any> {
Expand Down Expand Up @@ -280,7 +268,8 @@ export class NavControllerBase extends Ion implements NavController {
return this._viewTest(enteringView, leavingView, ti);

} else {
return this._postViewInit(enteringView, leavingView, ti, ti.resolve);
this._postViewInit(enteringView, leavingView, ti, ti.resolve);
return true;
}
}

Expand Down Expand Up @@ -334,7 +323,7 @@ export class NavControllerBase extends Ion implements NavController {
return null;
}

_postViewInit(enteringView: ViewController, leavingView: ViewController, ti: TransitionInstruction, resolve: TransitionResolveFn): boolean {
_postViewInit(enteringView: ViewController, leavingView: ViewController, ti: TransitionInstruction, resolve: TransitionResolveFn) {
const opts = ti.opts || {};

const insertViews = ti.insertViews;
Expand Down Expand Up @@ -425,8 +414,6 @@ export class NavControllerBase extends Ion implements NavController {
// resolve immediately because there's no animation that's happening
resolve(true, false);
}

return true;
}

/**
Expand All @@ -447,7 +434,7 @@ export class NavControllerBase extends Ion implements NavController {
enteringView._state = ViewState.INITIALIZED;
}

_viewTest(enteringView: ViewController, leavingView: ViewController, ti: TransitionInstruction): boolean {
_viewTest(enteringView: ViewController, leavingView: ViewController, ti: TransitionInstruction) {
const promises: Promise<any>[] = [];
const reject = ti.reject;
const resolve = ti.resolve;
Expand Down Expand Up @@ -490,20 +477,16 @@ export class NavControllerBase extends Ion implements NavController {
// all promises resolved! let's continue
this._postViewInit(enteringView, leavingView, ti, resolve);

}).catch((rejectReason) => {
reject(rejectReason);
});

return false;
}).catch(reject);
return true;
}

// synchronous and all tests passed! let's move on already
this._postViewInit(enteringView, leavingView, ti, resolve);

return true;
}

_transition(enteringView: ViewController, leavingView: ViewController, opts: NavOptions, resolve: TransitionResolveFn): void {
_transition(enteringView: ViewController, leavingView: ViewController, opts: NavOptions, resolve: TransitionResolveFn) {
// figure out if this transition is the root one or a
// child of a parent nav that has the root transition
this._trnsId = this._trnsCtrl.getRootTrnsId(this);
Expand Down

0 comments on commit 1300cbd

Please sign in to comment.