Skip to content

Commit 91f3bd1

Browse files
author
VladimirAmiorkov
committed
fix(hmr): close modal views during livesync #7668
chore: add unit tests to test that modal views are closed during hmr
1 parent 589bb43 commit 91f3bd1

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

tests/app/livesync/livesync-modal-view-page.css

Whitespace-only changes.

tests/app/livesync/livesync-modal-view-page.scss

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { View, ShowModalOptions } from "tns-core-modules/ui/core/view";
2+
const LIVESYNC_FOLDER = "livesync/";
3+
const buttonPageModuleName = `${LIVESYNC_FOLDER}livesync-button-page`;
4+
5+
export function onLoaded(args) {
6+
const view = args.object as View;
7+
8+
let options: ShowModalOptions = {
9+
context: "context",
10+
closeCallback: () => console.log("modal view closeCallback raised."),
11+
animated: false
12+
};
13+
14+
view.showModal(buttonPageModuleName, options);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Page loaded="onLoaded">
2+
3+
</Page>

tests/app/livesync/livesync-tests.ts

+39
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ const buttonTsPageFileName = `${LIVESYNC_FOLDER}livesync-button-page.ts`;
3030
const buttonScssPageFileName = `${LIVESYNC_FOLDER}livesync-button-page.scss`;
3131
const labelPageModuleName = `${LIVESYNC_FOLDER}livesync-label-page`;
3232

33+
const modalViewPageModuleName = `${LIVESYNC_FOLDER}livesync-modal-view-page`;
34+
const modalViewXmlPageFileName = `${LIVESYNC_FOLDER}livesync-modal-view-page.xml`;
35+
const modalViewJsPageFileName = `${LIVESYNC_FOLDER}livesync-modal-view-page.js`;
36+
const modalViewTsPageFileName = `${LIVESYNC_FOLDER}livesync-modal-view-page.ts`;
37+
const modalViewScssPageFileName = `${LIVESYNC_FOLDER}livesync-modal-view-page.scss`;
38+
const modalViewCssFileName = `${LIVESYNC_FOLDER}livesync-modal-view-page.css`;
39+
3340
const green = new Color("green");
3441

3542
export function setUp() {
@@ -111,6 +118,26 @@ export function test_onLiveSync_ModuleContext_MarkupHtml_ScriptTs_StyleScss_File
111118
]);
112119
}
113120

121+
export function test_onLiveSync_ModalViewClosed_MarkupXml() {
122+
_test_onLiveSync_ModalViewClosed({ type: "markup", path: modalViewXmlPageFileName });
123+
}
124+
125+
export function test_onLiveSync_ModalViewClosed_ScriptTs() {
126+
_test_onLiveSync_ModalViewClosed({ type: "script", path: modalViewTsPageFileName });
127+
}
128+
129+
export function test_onLiveSync_ModalViewClosed_ScriptJs() {
130+
_test_onLiveSync_ModalViewClosed({ type: "script", path: modalViewJsPageFileName });
131+
}
132+
133+
export function test_onLiveSync_ModalViewClosed_StyleCss() {
134+
_test_onLiveSync_ModalViewClosed({ type: "style", path: modalViewCssFileName });
135+
}
136+
137+
export function test_onLiveSync_ModalViewClosed_StyleScss() {
138+
_test_onLiveSync_ModalViewClosed({ type: "style", path: modalViewScssPageFileName });
139+
}
140+
114141
function _test_onLiveSync_ModuleContext_AppStyle(appStyleFileName: string, livesyncStyleFileName: string) {
115142
const pageBeforeNavigation = helper.getCurrentPage();
116143
const buttonPage = <Page>createViewFromEntry(({ moduleName: buttonPageModuleName }));
@@ -209,6 +236,18 @@ function _test_onLiveSync_ModuleReplace_Multiple(context: ModuleContext[]) {
209236
TKUnit.assertEqual(pageBeforeNavigation, pageAfterBackNavigation, "Pages are different!");
210237
}
211238

239+
function _test_onLiveSync_ModalViewClosed(context: ModuleContext) {
240+
const modalViewPage = <Page>createViewFromEntry(({ moduleName: modalViewPageModuleName }));
241+
helper.navigateWithHistory(() => modalViewPage);
242+
livesync({ type: context.type, path: context.path });
243+
244+
TKUnit.waitUntilReady(() => !!frame.topmost());
245+
const topmostFrame = frame.topmost();
246+
TKUnit.waitUntilReady(() => topmostFrame.currentPage && topmostFrame.currentPage.isLoaded && topmostFrame.canGoBack());
247+
248+
TKUnit.assertTrue(topmostFrame._getRootModalViews().length === 0);
249+
}
250+
212251
function livesync(context: ModuleContext) {
213252
const ls = (<any>global).__hmrSyncBackup || global.__onLiveSync;
214253
ls(context);

tns-core-modules/ui/core/view/view-common.ts

+20
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,31 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
152152
}
153153
}
154154

155+
public _closeAllModalViewsInternal(): boolean {
156+
if (_rootModalViews && _rootModalViews.length > 0) {
157+
_rootModalViews.forEach(v => {
158+
v.closeModal();
159+
});
160+
161+
return true;
162+
}
163+
164+
return false;
165+
}
166+
167+
public _getRootModalViews(): Array<ViewBase> {
168+
return _rootModalViews;
169+
}
170+
155171
public _onLivesync(context?: ModuleContext): boolean {
156172
if (traceEnabled()) {
157173
traceWrite(`${this}._onLivesync(${JSON.stringify(context)})`, traceCategories.Livesync);
158174
}
159175

176+
if (this._closeAllModalViewsInternal()) {
177+
return true;
178+
}
179+
160180
if (this._handleLivesync(context)) {
161181
return true;
162182
}

tns-core-modules/ui/core/view/view.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,18 @@ export abstract class View extends ViewBase {
610610
// Lifecycle events
611611
_getNativeViewsCount(): number;
612612

613+
/**
614+
* Internal method:
615+
* Closes all modal views. Should be used by plugins like `nativescript-angular` which implement their own `modal views` service.
616+
*/
617+
_closeAllModalViewsInternal(): boolean;
618+
619+
/**
620+
* Internal method:
621+
* Gets all modal views of the current view.
622+
*/
623+
_getRootModalViews(): Array<ViewBase>
624+
613625
_eachLayoutView(callback: (View) => void): void;
614626

615627
/**

0 commit comments

Comments
 (0)