Skip to content

Commit

Permalink
fix(modal): innerView.closeModal(...) not passing back context (#5833)
Browse files Browse the repository at this point in the history
  • Loading branch information
manoldonev authored May 18, 2018
1 parent 3f8af4c commit 1365f13
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
79 changes: 79 additions & 0 deletions tests/app/ui/page/page-tests-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Label } from "tns-core-modules/ui/label";
import { Color } from "tns-core-modules/color";
import { TabView, TabViewItem } from "tns-core-modules/ui/tab-view/tab-view";
import { _resetRootView, getRootView } from "tns-core-modules/application";
import { Button } from "tns-core-modules/ui/button/button";

export function addLabelToPage(page: Page, text?: string) {
const label = new Label();
Expand Down Expand Up @@ -421,6 +422,84 @@ export function test_WhenPageIsNavigatedToFrameCurrentPageIsNowTheSameAsThePage(
page.off(Label.loadedEvent, navigatedEventHandler);
}

export function test_WhenInnerViewCallsCloseModal_WithArguments_ShouldPassResult() {
_test_WhenInnerViewCallsCloseModal((args: ShownModallyData) =>
{
const page = <Page>args.object;
const button = <Button>page.content;
return button.closeModal.bind(button);
}, "return value");
}

export function test_WhenInnerViewCallsCloseModal_WithoutArguments_ShouldWork() {
_test_WhenInnerViewCallsCloseModal((args: ShownModallyData) =>
{
const page = <Page>args.object;
const button = <Button>page.content;
return button.closeModal.bind(button);
});
}

export function test_WhenInnerViewCallsCloseCallback_WithArguments_ShouldPassResult() {
_test_WhenInnerViewCallsCloseModal((args: ShownModallyData) =>
{
return args.closeCallback;
}, "return value");
}

export function test_WhenInnerViewCallsCloseCallback_WithoutArguments_ShouldWork() {
_test_WhenInnerViewCallsCloseModal((args: ShownModallyData) =>
{
return args.closeCallback;
});
}

function _test_WhenInnerViewCallsCloseModal(closeModalGetter: (ShownModallyData) => Function, result?: any) {
let modalClosedWithResult = false;

const modalCloseCallback = function (returnValue: any) {
modalClosedWithResult = returnValue === result;
}

const modalPageShownModallyEventHandler = function(args: ShownModallyData) {
const page = <Page>args.object;
page.off(View.shownModallyEvent, modalPageShownModallyEventHandler);

closeModalGetter(args)(result);
}

const hostNavigatedToEventHandler = function(args: NavigatedData) {
const page = <Page>args.object;
page.off(Page.navigatedToEvent, hostNavigatedToEventHandler);

const modalPage = new Page();
modalPage.id = "modalPage_test_WhenInnerViewCallsCloseModal_WithArguments_ShouldPassResult";
modalPage.on(View.shownModallyEvent, modalPageShownModallyEventHandler);

const button = new Button();
button.text = "CLOSE MODAL";
modalPage.content = button;

(<Button>page.content).showModal(modalPage, {}, modalCloseCallback);
}

const masterPageFactory = function(): Page {
const masterPage = new Page();
masterPage.id = "masterPage_test_WhenInnerViewCallsCloseModal_WithArguments_ShouldPassResult";
masterPage.on(Page.navigatedToEvent, hostNavigatedToEventHandler)

const button = new Button();
button.text = "TAP";
masterPage.content = button;

return masterPage;
};

helper.navigate(masterPageFactory);

TKUnit.waitUntilReady(() => modalClosedWithResult);
}

export function test_WhenViewBaseCallsShowModal_WithArguments_ShouldOpenModal() {
let modalClosed = false;

Expand Down
3 changes: 2 additions & 1 deletion tns-core-modules/ui/core/view-base/view-base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ export abstract class ViewBase extends Observable {

/**
* Closes the current modal view that this page is showing.
* @param context - Any context you want to pass back to the host when closing the modal view.
*/
closeModal(): void;
closeModal(context?: any): void;

public effectiveMinWidth: number;
public effectiveMinHeight: number;
Expand Down
4 changes: 2 additions & 2 deletions tns-core-modules/ui/core/view-base/view-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,10 +949,10 @@ export abstract class ViewBase extends Observable implements ViewBaseDefinition
return parent && parent.showModal(...args);
}

public closeModal(): void {
public closeModal(...args): void {
const parent = this.parent;
if (parent) {
parent.closeModal();
parent.closeModal(...args);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tns-core-modules/ui/core/view/view-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,14 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
}
}

public closeModal() {
public closeModal(...args) {
let closeCallback = this._closeModalCallback;
if (closeCallback) {
closeCallback.apply(undefined, arguments);
} else {
let parent = this.parent;
if (parent) {
parent.closeModal();
parent.closeModal(...args);
}
}
}
Expand Down

0 comments on commit 1365f13

Please sign in to comment.