UNPKG

9.34 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var aureliaTemplating = require('aurelia-templating');
6var aureliaLogging = require('aurelia-logging');
7var aureliaPal = require('aurelia-pal');
8
9var 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
25var 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/******************************************************************************
61Copyright (c) Microsoft Corporation.
62
63Permission to use, copy, modify, and/or distribute this software for any
64purpose with or without fee is hereby granted.
65
66THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
67REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
68AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
69INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
70LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
71OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
72PERFORMANCE OF THIS SOFTWARE.
73***************************************************************************** */
74
75var __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
86function 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}
107function waitForDocumentElement(selector, options) {
108 return waitFor(function () { return document.querySelector(selector); }, options);
109}
110function waitForDocumentElements(selector, options) {
111 return waitFor(function () { return document.querySelectorAll(selector); }, options);
112}
113
114var 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}());
123var 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
219function configure(config) {
220 config.globalResources([CompileSpy, ViewSpy]);
221}
222
223exports.CompileSpy = CompileSpy;
224exports.ComponentTester = ComponentTester;
225exports.StageComponent = StageComponent;
226exports.ViewSpy = ViewSpy;
227exports.configure = configure;
228exports.waitFor = waitFor;
229exports.waitForDocumentElement = waitForDocumentElement;
230exports.waitForDocumentElements = waitForDocumentElements;
231//# sourceMappingURL=aurelia-testing.js.map