1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var aurelia_templating_1 = require("aurelia-templating");
|
4 | var wait_1 = require("./wait");
|
5 | var StageComponent = (function () {
|
6 | function StageComponent() {
|
7 | }
|
8 | StageComponent.withResources = function (resources) {
|
9 | if (resources === void 0) { resources = []; }
|
10 | return new ComponentTester().withResources(resources);
|
11 | };
|
12 | return StageComponent;
|
13 | }());
|
14 | exports.StageComponent = StageComponent;
|
15 | var ComponentTester = (function () {
|
16 | function ComponentTester() {
|
17 | this.resources = [];
|
18 | }
|
19 | ComponentTester.prototype.configure = function (aurelia) {
|
20 | return aurelia.use.standardConfiguration();
|
21 | };
|
22 | ComponentTester.prototype.bootstrap = function (configure) {
|
23 | this.configure = configure;
|
24 | };
|
25 | ComponentTester.prototype.withResources = function (resources) {
|
26 | this.resources = resources;
|
27 | return this;
|
28 | };
|
29 | ComponentTester.prototype.inView = function (html) {
|
30 | this.html = html;
|
31 | return this;
|
32 | };
|
33 | ComponentTester.prototype.boundTo = function (bindingContext) {
|
34 | this.bindingContext = bindingContext;
|
35 | return this;
|
36 | };
|
37 | ComponentTester.prototype.manuallyHandleLifecycle = function () {
|
38 | this._prepareLifecycle();
|
39 | return this;
|
40 | };
|
41 | ComponentTester.prototype.create = function (bootstrap) {
|
42 | var _this = this;
|
43 | return bootstrap(function (aurelia) {
|
44 | return Promise.resolve(_this.configure(aurelia)).then(function () {
|
45 | if (_this.resources) {
|
46 | aurelia.use.globalResources(_this.resources);
|
47 | }
|
48 | return aurelia.start().then(function () {
|
49 | _this.host = document.createElement('div');
|
50 | _this.host.innerHTML = _this.html;
|
51 | document.body.appendChild(_this.host);
|
52 | return aurelia.enhance(_this.bindingContext, _this.host).then(function () {
|
53 | _this.rootView = aurelia.root;
|
54 | _this.element = _this.host.firstElementChild;
|
55 | if (aurelia.root.controllers.length) {
|
56 | _this.viewModel = aurelia.root.controllers[0].viewModel;
|
57 | }
|
58 | return new Promise(function (resolve) { return setTimeout(function () { return resolve(); }, 0); });
|
59 | });
|
60 | });
|
61 | });
|
62 | });
|
63 | };
|
64 | ComponentTester.prototype.dispose = function () {
|
65 | if (this.host === undefined || this.rootView === undefined) {
|
66 | throw new Error('Cannot call ComponentTester.dispose() before ComponentTester.create()');
|
67 | }
|
68 | this.rootView.detached();
|
69 | this.rootView.unbind();
|
70 | return this.host.parentNode.removeChild(this.host);
|
71 | };
|
72 | ComponentTester.prototype._prepareLifecycle = function () {
|
73 | var _this = this;
|
74 |
|
75 | var bindPrototype = aurelia_templating_1.View.prototype.bind;
|
76 |
|
77 | aurelia_templating_1.View.prototype.bind = function () { };
|
78 | this.bind = function (bindingContext) { return new Promise(function (resolve) {
|
79 | aurelia_templating_1.View.prototype.bind = bindPrototype;
|
80 | if (bindingContext !== undefined) {
|
81 | _this.bindingContext = bindingContext;
|
82 | }
|
83 | _this.rootView.bind(_this.bindingContext);
|
84 | setTimeout(function () { return resolve(); }, 0);
|
85 | }); };
|
86 |
|
87 | var attachedPrototype = aurelia_templating_1.View.prototype.attached;
|
88 |
|
89 | aurelia_templating_1.View.prototype.attached = function () { };
|
90 | this.attached = function () { return new Promise(function (resolve) {
|
91 | aurelia_templating_1.View.prototype.attached = attachedPrototype;
|
92 | _this.rootView.attached();
|
93 | setTimeout(function () { return resolve(); }, 0);
|
94 | }); };
|
95 |
|
96 | this.detached = function () { return new Promise(function (resolve) {
|
97 | _this.rootView.detached();
|
98 | setTimeout(function () { return resolve(); }, 0);
|
99 | }); };
|
100 |
|
101 | this.unbind = function () { return new Promise(function (resolve) {
|
102 | _this.rootView.unbind();
|
103 | setTimeout(function () { return resolve(); }, 0);
|
104 | }); };
|
105 | };
|
106 | ComponentTester.prototype.waitForElement = function (selector, options) {
|
107 | var _this = this;
|
108 | return wait_1.waitFor(function () { return _this.element.querySelector(selector); }, options);
|
109 | };
|
110 | ComponentTester.prototype.waitForElements = function (selector, options) {
|
111 | var _this = this;
|
112 | return wait_1.waitFor(function () { return _this.element.querySelectorAll(selector); }, options);
|
113 | };
|
114 | return ComponentTester;
|
115 | }());
|
116 | exports.ComponentTester = ComponentTester;
|