UNPKG

1.91 kBJavaScriptView Raw
1/* global mocha: true */
2
3'use strict';
4
5var scriptsToLoad = [
6 '../lib/localstorage-memory.js'
7];
8
9// Thanks to http://engineeredweb.com/blog/simple-async-javascript-loader/
10function asyncLoadScript(url, callback) {
11
12 // Create a new script and setup the basics.
13 var script = document.createElement('script');
14 var firstScript = document.getElementsByTagName('script')[0];
15
16 script.async = true;
17 script.src = url;
18
19 // Handle the case where an optional callback was passed in.
20 if ('function' === typeof(callback)) {
21 script.onload = function () {
22 callback();
23
24 // Clear it out to avoid getting called more than once or any
25 // memory leaks.
26 script.onload = script.onreadystatechange = undefined;
27 };
28 script.onreadystatechange = function () {
29 if ('loaded' === script.readyState || 'complete' === script.readyState) {
30 script.onload();
31 }
32 };
33 }
34
35 // Attach the script tag to the page (before the first script) so the
36 // magic can happen.
37 firstScript.parentNode.insertBefore(script, firstScript);
38}
39
40function startTests() {
41
42 function onReady() {
43 var runner = mocha.run();
44 window.results = {
45 lastPassed: '',
46 passed: 0,
47 failed: 0,
48 failures: []
49 };
50
51 runner.on('pass', function (e) {
52 window.results.lastPassed = e.title;
53 window.results.passed++;
54 });
55
56 runner.on('fail', function (e) {
57 window.results.failed++;
58 window.results.failures.push({
59 title: e.title,
60 message: e.err.message,
61 stack: e.err.stack
62 });
63 });
64
65 runner.on('end', function () {
66 window.results.completed = true;
67 window.results.passed++;
68 });
69 }
70
71 function loadNext() {
72 if (scriptsToLoad.length) {
73 var script = scriptsToLoad.shift();
74 asyncLoadScript(script, loadNext);
75 } else {
76 onReady();
77 }
78 }
79
80 loadNext();
81}
82
83startTests();