|
| 1 | +//make sure you import mocha-config before angular2/core |
| 2 | +import {assert} from "./test-config"; |
| 3 | +import {Component, ComponentRef} from "angular2/core"; |
| 4 | +import {TestApp} from "./test-app"; |
| 5 | +import {Page} from "ui/page"; |
| 6 | +import {topmost} from "ui/frame"; |
| 7 | +import {ModalDialogHost, ModalDialogOptions, ModalDialogParams, ModalDialogService} from "../nativescript-angular/directives/dialogs"; |
| 8 | + |
| 9 | +@Component({ |
| 10 | + selector: "modal-comp", |
| 11 | + template: `<Label text="this is modal componenet"></Label>` |
| 12 | +}) |
| 13 | +export class ModalComponent { |
| 14 | + constructor(public params: ModalDialogParams) { |
| 15 | + // Close after a while |
| 16 | + setTimeout(() => { |
| 17 | + var result = this.params.context; |
| 18 | + this.params.closeCallback(result); |
| 19 | + }, 200); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +@Component({ |
| 24 | + selector: "fail-comp", |
| 25 | + providers: [ModalDialogService], |
| 26 | + template: `<Label text="This app is doomed"></Label>` |
| 27 | + |
| 28 | +}) |
| 29 | +export class FailComponenet { |
| 30 | + constructor(public service: ModalDialogService) { |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +@Component({ |
| 35 | + selector: "sucess-comp", |
| 36 | + directives: [ModalDialogHost], |
| 37 | + providers: [ModalDialogService], |
| 38 | + template: ` |
| 39 | + <GridLayout modal-dialog-host margin="20"> |
| 40 | + <Label text="Modal dialogs"></Label> |
| 41 | + </GridLayout>` |
| 42 | +}) |
| 43 | +export class SuccessComponenet { |
| 44 | + constructor(public service: ModalDialogService) { |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +describe('modal-dialog', () => { |
| 49 | + let testApp: TestApp = null; |
| 50 | + |
| 51 | + before(() => { |
| 52 | + return TestApp.create().then((app) => { |
| 53 | + testApp = app; |
| 54 | + }) |
| 55 | + }); |
| 56 | + |
| 57 | + after(() => { |
| 58 | + testApp.dispose(); |
| 59 | + }); |
| 60 | + |
| 61 | + afterEach(() => { |
| 62 | + var page = topmost().currentPage; |
| 63 | + if (page && page.modal) { |
| 64 | + console.log("Warning: closing a leftover modal page!"); |
| 65 | + page.modal.closeModal(); |
| 66 | + |
| 67 | + } |
| 68 | + testApp.disposeComponenets(); |
| 69 | + }); |
| 70 | + |
| 71 | + |
| 72 | + it("showModal throws when there is no modal-dialog-host", (done) => { |
| 73 | + testApp.loadComponent(FailComponenet) |
| 74 | + .then((ref) => { |
| 75 | + var service = <ModalDialogService>ref.instance.service; |
| 76 | + assert.throws(() => service.showModal(ModalComponent, {}), "No elementRef: Make sure you have the modal-dialog-host directive inside your component."); |
| 77 | + done(); |
| 78 | + }) |
| 79 | + .catch(done) |
| 80 | + }); |
| 81 | + |
| 82 | + it("showModal succeeds when there is modal-dialog-host", (done) => { |
| 83 | + testApp.loadComponent(SuccessComponenet) |
| 84 | + .then((ref) => { |
| 85 | + var service = <ModalDialogService>ref.instance.service; |
| 86 | + return service.showModal(ModalComponent, {}); |
| 87 | + }) |
| 88 | + .then((res) => done()) |
| 89 | + .catch(done) |
| 90 | + }); |
| 91 | + |
| 92 | + |
| 93 | + it("showModal passes modal params and gets result when resolved", (done) => { |
| 94 | + var context = { property: "my context" }; |
| 95 | + testApp.loadComponent(SuccessComponenet) |
| 96 | + .then((ref) => { |
| 97 | + var service = <ModalDialogService>ref.instance.service; |
| 98 | + return service.showModal(ModalComponent, { context: context }); |
| 99 | + }) |
| 100 | + .then((res) => { |
| 101 | + assert.strictEqual(res, context); |
| 102 | + done(); |
| 103 | + }) |
| 104 | + .catch(done); |
| 105 | + }) |
| 106 | +}); |
0 commit comments