1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var aureliaTemplating = require('aurelia-templating');
|
6 | var aureliaLogging = require('aurelia-logging');
|
7 | var aureliaPal = require('aurelia-pal');
|
8 |
|
9 | var CompileSpy = (function () {
|
10 | function CompileSpy(element, instruction) {
|
11 | aureliaLogging.getLogger('compile-spy').info(element.toString(), instruction);
|
12 | }
|
13 | Object.defineProperty(CompileSpy, "inject", {
|
14 | get: function () { return [aureliaPal.DOM.Element, aureliaTemplating.TargetInstruction]; },
|
15 | enumerable: false,
|
16 | configurable: true
|
17 | });
|
18 | CompileSpy.$resource = {
|
19 | type: 'attribute',
|
20 | name: 'compile-spy'
|
21 | };
|
22 | return CompileSpy;
|
23 | }());
|
24 |
|
25 | var ViewSpy = (function () {
|
26 | function ViewSpy() {
|
27 | this.logger = aureliaLogging.getLogger('view-spy');
|
28 | }
|
29 | ViewSpy.prototype._log = function (lifecycleName, context) {
|
30 | if (!this.value && lifecycleName === 'created') {
|
31 | this.logger.info(lifecycleName, this.view);
|
32 | }
|
33 | else if (this.value && this.value.indexOf(lifecycleName) !== -1) {
|
34 | this.logger.info(lifecycleName, this.view, context);
|
35 | }
|
36 | };
|
37 | ViewSpy.prototype.created = function (view) {
|
38 | this.view = view;
|
39 | this._log('created');
|
40 | };
|
41 | ViewSpy.prototype.bind = function (bindingContext) {
|
42 | this._log('bind', bindingContext);
|
43 | };
|
44 | ViewSpy.prototype.attached = function () {
|
45 | this._log('attached');
|
46 | };
|
47 | ViewSpy.prototype.detached = function () {
|
48 | this._log('detached');
|
49 | };
|
50 | ViewSpy.prototype.unbind = function () {
|
51 | this._log('unbind');
|
52 | };
|
53 | ViewSpy.$resource = {
|
54 | type: 'attribute',
|
55 | name: 'view-spy'
|
56 | };
|
57 | return ViewSpy;
|
58 | }());
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | var __assign = function() {
|
76 | __assign = Object.assign || function __assign(t) {
|
77 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
78 | s = arguments[i];
|
79 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
80 | }
|
81 | return t;
|
82 | };
|
83 | return __assign.apply(this, arguments);
|
84 | };
|
85 |
|
86 | function waitFor(getter, options) {
|
87 | if (options === void 0) { options = { present: true, interval: 50, timeout: 5000 }; }
|
88 | var timedOut = false;
|
89 | options = __assign({ present: true, interval: 50, timeout: 5000 }, options);
|
90 | function wait() {
|
91 | var element = getter();
|
92 | var found = element !== null && (!(element instanceof NodeList) &&
|
93 | !element.jquery || element.length > 0);
|
94 | if (!options.present === !found || timedOut) {
|
95 | return Promise.resolve(element);
|
96 | }
|
97 | return new Promise(function (rs) { return setTimeout(rs, options.interval); }).then(wait);
|
98 | }
|
99 | return Promise.race([
|
100 | new Promise(function (_, rj) { return setTimeout(function () {
|
101 | timedOut = true;
|
102 | rj(new Error(options.present ? 'Element not found' : 'Element not removed'));
|
103 | }, options.timeout); }),
|
104 | wait()
|
105 | ]);
|
106 | }
|
107 | function waitForDocumentElement(selector, options) {
|
108 | return waitFor(function () { return document.querySelector(selector); }, options);
|
109 | }
|
110 | function waitForDocumentElements(selector, options) {
|
111 | return waitFor(function () { return document.querySelectorAll(selector); }, options);
|
112 | }
|
113 |
|
114 | var StageComponent = (function () {
|
115 | function StageComponent() {
|
116 | }
|
117 | StageComponent.withResources = function (resources) {
|
118 | if (resources === void 0) { resources = []; }
|
119 | return new ComponentTester().withResources(resources);
|
120 | };
|
121 | return StageComponent;
|
122 | }());
|
123 | var ComponentTester = (function () {
|
124 | function ComponentTester() {
|
125 | this.resources = [];
|
126 | }
|
127 | ComponentTester.prototype.configure = function (aurelia) {
|
128 | return aurelia.use.standardConfiguration();
|
129 | };
|
130 | ComponentTester.prototype.bootstrap = function (configure) {
|
131 | this.configure = configure;
|
132 | };
|
133 | ComponentTester.prototype.withResources = function (resources) {
|
134 | this.resources = resources;
|
135 | return this;
|
136 | };
|
137 | ComponentTester.prototype.inView = function (html) {
|
138 | this.html = html;
|
139 | return this;
|
140 | };
|
141 | ComponentTester.prototype.boundTo = function (bindingContext) {
|
142 | this.bindingContext = bindingContext;
|
143 | return this;
|
144 | };
|
145 | ComponentTester.prototype.manuallyHandleLifecycle = function () {
|
146 | this._prepareLifecycle();
|
147 | return this;
|
148 | };
|
149 | ComponentTester.prototype.create = function (bootstrap) {
|
150 | var _this = this;
|
151 | return bootstrap(function (aurelia) {
|
152 | return Promise.resolve(_this.configure(aurelia)).then(function () {
|
153 | if (_this.resources) {
|
154 | aurelia.use.globalResources(_this.resources);
|
155 | }
|
156 | return aurelia.start().then(function () {
|
157 | _this.host = document.createElement('div');
|
158 | _this.host.innerHTML = _this.html;
|
159 | document.body.appendChild(_this.host);
|
160 | return aurelia.enhance(_this.bindingContext, _this.host).then(function () {
|
161 | _this.rootView = aurelia.root;
|
162 | _this.element = _this.host.firstElementChild;
|
163 | if (aurelia.root.controllers.length) {
|
164 | _this.viewModel = aurelia.root.controllers[0].viewModel;
|
165 | }
|
166 | return new Promise(function (resolve) { return setTimeout(function () { return resolve(); }, 0); });
|
167 | });
|
168 | });
|
169 | });
|
170 | });
|
171 | };
|
172 | ComponentTester.prototype.dispose = function () {
|
173 | if (this.host === undefined || this.rootView === undefined) {
|
174 | throw new Error('Cannot call ComponentTester.dispose() before ComponentTester.create()');
|
175 | }
|
176 | this.rootView.detached();
|
177 | this.rootView.unbind();
|
178 | return this.host.parentNode.removeChild(this.host);
|
179 | };
|
180 | ComponentTester.prototype._prepareLifecycle = function () {
|
181 | var _this = this;
|
182 | var bindPrototype = aureliaTemplating.View.prototype.bind;
|
183 | aureliaTemplating.View.prototype.bind = function () { };
|
184 | this.bind = function (bindingContext) { return new Promise(function (resolve) {
|
185 | aureliaTemplating.View.prototype.bind = bindPrototype;
|
186 | if (bindingContext !== undefined) {
|
187 | _this.bindingContext = bindingContext;
|
188 | }
|
189 | _this.rootView.bind(_this.bindingContext);
|
190 | setTimeout(function () { return resolve(); }, 0);
|
191 | }); };
|
192 | var attachedPrototype = aureliaTemplating.View.prototype.attached;
|
193 | aureliaTemplating.View.prototype.attached = function () { };
|
194 | this.attached = function () { return new Promise(function (resolve) {
|
195 | aureliaTemplating.View.prototype.attached = attachedPrototype;
|
196 | _this.rootView.attached();
|
197 | setTimeout(function () { return resolve(); }, 0);
|
198 | }); };
|
199 | this.detached = function () { return new Promise(function (resolve) {
|
200 | _this.rootView.detached();
|
201 | setTimeout(function () { return resolve(); }, 0);
|
202 | }); };
|
203 | this.unbind = function () { return new Promise(function (resolve) {
|
204 | _this.rootView.unbind();
|
205 | setTimeout(function () { return resolve(); }, 0);
|
206 | }); };
|
207 | };
|
208 | ComponentTester.prototype.waitForElement = function (selector, options) {
|
209 | var _this = this;
|
210 | return waitFor(function () { return _this.element.querySelector(selector); }, options);
|
211 | };
|
212 | ComponentTester.prototype.waitForElements = function (selector, options) {
|
213 | var _this = this;
|
214 | return waitFor(function () { return _this.element.querySelectorAll(selector); }, options);
|
215 | };
|
216 | return ComponentTester;
|
217 | }());
|
218 |
|
219 | function configure(config) {
|
220 | config.globalResources([CompileSpy, ViewSpy]);
|
221 | }
|
222 |
|
223 | exports.CompileSpy = CompileSpy;
|
224 | exports.ComponentTester = ComponentTester;
|
225 | exports.StageComponent = StageComponent;
|
226 | exports.ViewSpy = ViewSpy;
|
227 | exports.configure = configure;
|
228 | exports.waitFor = waitFor;
|
229 | exports.waitForDocumentElement = waitForDocumentElement;
|
230 | exports.waitForDocumentElements = waitForDocumentElements;
|
231 |
|