UNPKG

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