-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathmodal-dialog.ts
112 lines (98 loc) · 3.81 KB
/
modal-dialog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// make sure you import mocha-config before @angular/core
import { assert } from "./test-config";
import { TestApp } from "./test-app";
import { Component, ViewContainerRef } from "@angular/core";
import { Page } from "ui/page";
import { topmost } from "ui/frame";
import { ModalDialogParams, ModalDialogService } from "nativescript-angular/directives/dialogs";
import { device, platformNames } from "platform";
const CLOSE_WAIT = (device.os === platformNames.ios) ? 1000 : 0;
@Component({
selector: "modal-comp",
template: `<Label text="this is modal component"></Label>`
})
export class ModalComponent {
constructor(public params: ModalDialogParams, private page: Page) {
page.on("shownModally", () => {
const result = this.params.context;
this.params.closeCallback(result);
});
}
}
@Component({
selector: "fail-comp",
providers: [ModalDialogService],
template: `<Label text="This app is doomed"></Label>`
})
export class FailComponent {
constructor(public service: ModalDialogService) {
}
}
@Component({
selector: "sucess-comp",
providers: [ModalDialogService],
template: `
<GridLayout margin="20">
<Label text="Modal dialogs"></Label>
</GridLayout>`
})
export class SuccessComponent {
constructor(public service: ModalDialogService, public vcRef: ViewContainerRef) {
}
}
describe("modal-dialog", () => {
let testApp: TestApp = null;
before((done) => {
TestApp.create([], [ModalComponent, FailComponent, SuccessComponent]).then((app) => {
testApp = app;
// HACK: Wait for the navigations from the test runner app
// Remove the setTimeout when test runner start tests on page.navigatedTo
setTimeout(done, 1000);
});
});
after(() => {
testApp.dispose();
});
afterEach(() => {
const page = topmost().currentPage;
if (page && page.modal) {
console.log("Warning: closing a leftover modal page!");
page.modal.closeModal();
}
testApp.disposeComponents();
});
it("showModal throws when there is no viewContainer provided", (done) => {
testApp.loadComponent(FailComponent)
.then((ref) => {
const service = <ModalDialogService>ref.instance.service;
assert.throws(() => service.showModal(ModalComponent, {}),
"No viewContainerRef: Make sure you pass viewContainerRef in ModalDialogOptions."
);
}).then(() => done(), err => done(err));
});
it("showModal succeeds when there is viewContainer provided", (done) => {
testApp.loadComponent(SuccessComponent)
.then((ref) => {
const service = <ModalDialogService>ref.instance.service;
const comp = <SuccessComponent>ref.instance;
return service.showModal(ModalComponent, { viewContainerRef: comp.vcRef });
})
.then((res) => setTimeout(done, CLOSE_WAIT), err => done(err)); // wait for the dialog to close in IOS
});
it("showModal passes modal params and gets result when resolved", (done) => {
const context = { property: "my context" };
testApp.loadComponent(SuccessComponent)
.then((ref) => {
const service = <ModalDialogService>ref.instance.service;
const comp = <SuccessComponent>ref.instance;
return service.showModal(ModalComponent, {
viewContainerRef: comp.vcRef,
context: context
});
})
.then((res) => {
assert.strictEqual(res, context);
setTimeout(done, CLOSE_WAIT); // wait for the dialog to close in IOS
}, err => done(err));
});
});