UNPKG

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