UNPKG

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